In Android I have noticed that when I take a picture with the camera and map it to an ImageView, on some cameras (such as my physical phone), it's rotated 90 degrees (whereas on the emulator phones I use, they aren't rotated).
I tried this:
//once you have the main bitmap
ExifInterface ei = null;
try {
ei = new ExifInterface(imageFilepath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
bitmap = getRotatedBitmap(bitmap, orientation);
}
catch (IOException e) {
e.printStackTrace();
}
Now the rotate code:
public static Bitmap getRotatedBitmap(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) {
return bitmap;
}
}
Is this correct? Or is it horribly inefficient? Is there some smarter way to do this? If I apply the rotate code to a bitmap and then save it back to Storage, will it now be "incorrectly rotated" or do I have to rotate it back? I don't know what the accepted practice is for all this.