thieumao

Xoay ảnh Image

Dec 4th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public static Bitmap getRotateBitmap(File imageFile) throws IOException {
  2. InputStream inputStream = new FileInputStream(imageFile);
  3. Bitmap bmpSource = BitmapFactory.decodeStream(inputStream);
  4. inputStream.close();
  5.  
  6. ExifInterface ei = new ExifInterface(imageFile.getCanonicalPath());
  7. int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  8. float angle = 0;
  9. switch (orientation) {
  10. case ExifInterface.ORIENTATION_ROTATE_90:
  11. angle = 90;
  12. break;
  13. case ExifInterface.ORIENTATION_ROTATE_180:
  14. angle = 180;
  15. break;
  16. case ExifInterface.ORIENTATION_ROTATE_270:
  17. angle = 270;
  18. break;
  19. // etc.
  20. }
  21. Bitmap bmpRotate;
  22. if (angle == 0) {// do not change bitmap
  23. bmpRotate = bmpSource;
  24. } else {
  25. bmpRotate = rotateImage(bmpSource, angle);
  26. bmpSource.recycle();// free memory
  27. }
  28.  
  29. return bmpRotate;
  30. }
  31.  
  32. public static Bitmap rotateImage(Bitmap source, float angle) {
  33. Bitmap retVal;
  34.  
  35. Matrix matrix = new Matrix();
  36. matrix.postRotate(angle);
  37. retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
  38.  
  39. return retVal;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment