Last active
September 4, 2018 12:35
-
-
Save vbuberen/1522f7ed77fdb72996dbb936913bbbc1 to your computer and use it in GitHub Desktop.
Image rotation restore for devices with physically rotated camera
This file contains 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
@Throws(IOException::class) | |
private fun restoreBitmapRotation(bitmapFile: File): Bitmap { | |
val exif = ExifInterface(Uri.fromFile(bitmapFile).path) | |
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) | |
val rotationAngle = when (orientation) { | |
ExifInterface.ORIENTATION_ROTATE_90 -> 90f | |
ExifInterface.ORIENTATION_ROTATE_180 -> 180f | |
ExifInterface.ORIENTATION_ROTATE_270 -> 270f | |
else -> 0f | |
} | |
val imageToRotate = BitmapFactory.decodeFile(bitmapFile.absolutePath) | |
val matrix = Matrix() | |
matrix.postRotate(rotationAngle) | |
return Bitmap.createBitmap(imageToRotate, 0, 0, imageToRotate.width, imageToRotate.height, matrix, true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment