Created
May 1, 2022 04:41
-
-
Save arieftb/f8fe5fa49086e496d3d4ff301de8c45c to your computer and use it in GitHub Desktop.
Check auto rotation get image from camera and rotate to real orientation example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap | |
import android.graphics.Matrix | |
import android.media.ExifInterface | |
object BitmapImageRotation { | |
fun getRotatedImage(path: String, bitmap: Bitmap): Bitmap { | |
val exifInterface = ExifInterface(path) | |
return when (exifInterface.getAttributeInt( | |
ExifInterface.TAG_ORIENTATION, | |
ExifInterface.ORIENTATION_UNDEFINED | |
)) { | |
ExifInterface.ORIENTATION_ROTATE_90 -> { | |
rotateImage(bitmap, 90f) | |
} | |
ExifInterface.ORIENTATION_ROTATE_180 -> { | |
rotateImage(bitmap, 180f) | |
} | |
ExifInterface.ORIENTATION_ROTATE_270 -> { | |
rotateImage(bitmap, 270f) | |
} | |
else -> { | |
bitmap | |
} | |
} | |
} | |
private fun rotateImage(source: Bitmap, angle: Float): Bitmap { | |
val matrix = Matrix() | |
matrix.postRotate(angle) | |
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment