Guest User

Untitled

a guest
Feb 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. fun rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap? {
  2. val matrix = Matrix()
  3.  
  4. when (orientation) {
  5. ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1F, 1F)
  6. ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180F)
  7. ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
  8. matrix.setRotate(180F)
  9. matrix.postScale(-1F, 1F)
  10. }
  11. ExifInterface.ORIENTATION_TRANSPOSE -> {
  12. matrix.setRotate(90F)
  13. matrix.postScale(-1F, 1F)
  14. }
  15. ExifInterface.ORIENTATION_ROTATE_90 -> {
  16. matrix.setRotate(90F)
  17. }
  18. ExifInterface.ORIENTATION_TRANSVERSE -> {
  19. matrix.setRotate(-90F)
  20. matrix.postScale(-1F, 1F)
  21. }
  22. ExifInterface.ORIENTATION_ROTATE_270 -> {
  23. matrix.setRotate(-90F)
  24. }
  25. else -> return bitmap
  26. }
  27.  
  28. return try {
  29. val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
  30. bitmap.recycle()
  31. rotatedBitmap
  32. } catch (e: Exception) {
  33. null
  34. }
  35. }
Add Comment
Please, Sign In to add comment