Skip to content

Instantly share code, notes, and snippets.

@arieftb
Created May 1, 2022 04:41
Show Gist options
  • Save arieftb/f8fe5fa49086e496d3d4ff301de8c45c to your computer and use it in GitHub Desktop.
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
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