Created
February 14, 2018 02:14
-
-
Save babedev/b7304be8bcd71de9af44e5c6a8d1cee5 to your computer and use it in GitHub Desktop.
Rotate bitmap on Android
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
fun rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap? { | |
val matrix = Matrix() | |
when (orientation) { | |
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1F, 1F) | |
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180F) | |
ExifInterface.ORIENTATION_FLIP_VERTICAL -> { | |
matrix.setRotate(180F) | |
matrix.postScale(-1F, 1F) | |
} | |
ExifInterface.ORIENTATION_TRANSPOSE -> { | |
matrix.setRotate(90F) | |
matrix.postScale(-1F, 1F) | |
} | |
ExifInterface.ORIENTATION_ROTATE_90 -> { | |
matrix.setRotate(90F) | |
} | |
ExifInterface.ORIENTATION_TRANSVERSE -> { | |
matrix.setRotate(-90F) | |
matrix.postScale(-1F, 1F) | |
} | |
ExifInterface.ORIENTATION_ROTATE_270 -> { | |
matrix.setRotate(-90F) | |
} | |
else -> return bitmap | |
} | |
return try { | |
val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) | |
bitmap.recycle() | |
rotatedBitmap | |
} catch (e: Exception) { | |
null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment