Skip to content

Instantly share code, notes, and snippets.

@vbuberen
Last active September 4, 2018 12:35
Show Gist options
  • Save vbuberen/1522f7ed77fdb72996dbb936913bbbc1 to your computer and use it in GitHub Desktop.
Save vbuberen/1522f7ed77fdb72996dbb936913bbbc1 to your computer and use it in GitHub Desktop.
Image rotation restore for devices with physically rotated camera
@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