After taking a photo while the device is in portrait using the Camera API and a preview SurfaceView
, the image that is created is rotated 90 degrees. I understand this is an issue affecting some android phones including the Samsung Galaxy S5 I'm developing on - there are several stack overflow questions about that - see here and here. It was suggested the image file will contain EXIF metadata that will state the orientation it was rotated by so you can use that to rotate the image back to obtain the correct orientation. I've done just that, but for some reason the ExifInterface.TAG_ORIENTATION
is 1
- ORIENTATION_NORMAL
. Therefore I cannot determine how much the image needs to be rotated to ensure it will work on all devices. Even though that solution seems to have worked for others. What have I done wrong, or how else could this issue be resolved?
Some pseudocode:
//in onCreate
camera = getCameraInstance();
setCameraDisplayOrientation(this, cameraID, camera);
//in OnClickListener
camera.takePicture(null, null, picCallback);
//callback:
onPictureTaken {
//create a File using getExternalStoragePublicDirectory(PICTURES) + "appname" - create directory if doesn't exist
//write file to disk via FileOutputStream
//attempt to correct the image orientation if needed
Bitmap correctBitmap = getCorrectOrientationBitmap(picFile.getAbsolutePath());
}
public Bitmap getCorrectOrientationBitmap(String photoFilePath) {
// Read EXIF Data
ExifInterface exif = null;
try {
exif = new ExifInterface(photoFilePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
//problem: orientation is 1 - ORIENTATION_NORMAL despite the fact the image is rotated to the right 90 degrees
//rotate the image based on EXIF orientation
}
//later on
myImageView.setImageBitmap(correctBitmap);
//shows image rotated to the right because getCorrectOrientationBitmap didn't rotate it