I am loading an image from gallery through bitmap api but the orientation of the image comes different than the one i saved it. Somewhere on stackoverflow i read that i need to get the oreintation value and then rotate the matrix. I tried this but my orientation is coming as 0 from the exif interface.
int desiredImageWidth = 310; // pixels
int desiredImageHeight = 518; // pixels
Log.i("FA", "Image Loading "+strFileName);
BitmapFactory.Options o = new BitmapFactory.Options();
Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(strFileName, o),
desiredImageWidth,
desiredImageHeight,
false);
Bitmap finalBmp = getCorrectOrientedBitmap(strFileName,newImage);
Bitmap myBitmap32 = finalBmp.copy(Bitmap.Config.ARGB_8888, true);
Mat matImg = Utils.bitmapToMat(myBitmap32);
My main orientation function is
private Bitmap getCorrectOrientedBitmap(String filename,Bitmap Source) throws IOException{
Bitmap rotatedBitmap = null;
ExifInterface exif = new ExifInterface(filename);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
Log.i("FA", "orientation "+orientation);
switch(orientation)
{
case 6:
matrix.postRotate(90);
break;
case 3:
matrix.postRotate(180);
break;
case 8:
matrix.postRotate(270);
break;
}
rotatedBitmap = Bitmap.createBitmap(Source, 0, 0, Source.getWidth(), Source.getHeight(), matrix, true);
return rotatedBitmap;
}
If you’re getting
0back fromgetAttributeInt, then I think that just means your “rotated” image doesn’t have any orientation info stored in it. You could independently double check with an online tool or download a good image viewer to find out.Have you tried this code on another device? Is it possible that your test device doesn’t save the orientation tag?
This page has some nice sample code to do what you’re trying to do, maybe you could find some improvements as well there.