I have written an Android activity that captures a photo programatically. I want to save the image as a JPEG with the correct EXIF orientation data (just like the native Android Camera app does automatically).
Here is the method for actually taking the photo (I removed the try/catch blocks):
private void takePhoto() {
camera = Camera.open();
SurfaceTexture dummySurfaceTexture = new SurfaceTexture(0);
camera.setPreviewTexture(dummySurfaceTexture);
camera.startPreview();
camera.takePicture(null, null, jpgCallback);
}
...and the callback:
private Camera.PictureCallback jpgCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
releaseCamera();
savePhoto(data);
};
The photo is taken properly, but my problem is that the EXIF data shows that the orientation is set to "Image Orientation: Top, Left-Hand" regardless of the orientation of the device, so that when I upload the photo it appears upside down or rotated.
Do I really need to capture the device orientation manually (roll, pitch, azimuth) and write the EXIF orientation myself? How does the Camera app automatically write this data correctly? Does anyone know of a way to set this attribute correctly?
EDIT: I can't use the screen orientation as the Activity is locked to portrait mode.
To change the photo's perspective, tap Transform . Drag the dots to the edges of your desired photo or tap Auto. To rotate a photo 90 degrees, tap Rotate . To make minor adjustments to straighten the photo, use the dial above Rotate .
The EXIF orientation value is used by Photoshop and other photo editing software to automatically rotate photos, saving you a manual task.
To fix the EXIF orientation, open the image in an image editing program. Rotate the image to the correct orientation, then save the file and reupload your image. As an alternative, you can simply remove all EXIF data from images in Windows and macOS.
You don't have to write the EXIF orientation yourself, but you do need to tell the camera subsystem your device's orientation before you take a picture. It doesn't have access to that information on its own. Once set, the camera subsystem will either set the EXIF field or rotate the image data to orient the image correctly (which is done depends on your specific device).
To tell the camera the orientation you want for your still pictures, use Camera.Parameters.setRotation():
There is reference code in the developer docs for how to use it correctly, which is a bit tricky because the value you set depends on 1) the orientation of your camera sensor and 2) the orientation of your UI, relative to the device's normal orientation. I've copied the sample code here, which uses an OrientationEventListener and assumes you have a Camera.Parameters object called mParameters:
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
Then before your takePicture call, you need to call Camera.setParameters(mParameters).
In your specific case, you might want to query the orientation right before you take the picture, and use the logic in the sample code to calculate the rotation. And then get the camera parameters with Camera.getParameters(), call setRotation, and then call Camera.setParameters().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With