If the image (photograph) was taken by a program made by you, you must set Parameters.setRotation with the correct rotation value.
This, depending on the camera drive, rotates the image before saving or saves the rotation value in exif TAG_ORIENTATION.
Therefore, if TAG_ORIENTATION is null or zero, the image is in the correct orientation, otherwise you must rotate the image according to the value in TAG_ORIENTATION.
THE CODE
Get orientation from EXIF:
ExifInterface exif = null; try { exif = new ExifInterface(path); } catch (IOException e) { e.printStackTrace(); } int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Rotate the bitmap:
Bitmap bmRotated = rotateBitmap(bitmap, orientation);
Bitmap rotation method:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) { Matrix matrix = new Matrix(); switch (orientation) { case ExifInterface.ORIENTATION_NORMAL: return bitmap; case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: matrix.setScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_180: matrix.setRotate(180); break; case ExifInterface.ORIENTATION_FLIP_VERTICAL: matrix.setRotate(180); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_TRANSPOSE: matrix.setRotate(90); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_90: matrix.setRotate(90); break; case ExifInterface.ORIENTATION_TRANSVERSE: matrix.setRotate(-90); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_270: matrix.setRotate(-90); break; default: return bitmap; } try { Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); return bmRotated; } catch (OutOfMemoryError e) { e.printStackTrace(); return null; } }
Source - fooobar.com/questions/53665 / ...
source share