设置 Android 照片 EXIF 方向 [英] Setting Android Photo EXIF Orientation

查看:20
本文介绍了设置 Android 照片 EXIF 方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个以编程方式捕获照片的 Android 活动.我想将图像保存为具有正确 EXIF 方向数据的 JPEG(就像本机 Android 相机应用程序自动执行的操作一样).

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).

这是实际拍照的方法(我删除了 try/catch 块):

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);
}

...和回调:

private Camera.PictureCallback jpgCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        releaseCamera();
        savePhoto(data);
};

照片拍摄正确,但我的问题是 EXIF 数据显示方向设置为图像方向:顶部,左手",无论设备的方向如何,所以当我上传照片时出现颠倒或旋转.

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.

我真的需要手动捕获设备方向(滚动、俯仰、方位角)并自己编写 EXIF 方向吗?相机应用程序如何自动正确写入这些数据?有谁知道正确设置此属性的方法吗?

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?

我无法使用屏幕方向,因为 Activity 被锁定为纵向模式.

I can't use the screen orientation as the Activity is locked to portrait mode.

推荐答案

您不必自己编写 EXIF 方向,但您需要在拍照前告诉相机子系统您的设备方向.它自己无法访问该信息.设置后,相机子系统将设置 EXIF 字段或旋转图像数据以正确定位图像(这取决于您的特定设备).

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).

要告诉相机您想要拍摄静态图片的方向,请使用 Camera.Parameters.setRotation():

To tell the camera the orientation you want for your still pictures, use Camera.Parameters.setRotation():

开发者文档中有关于如何正确使用它的参考代码,这有点棘手,因为您设置的值取决于 1)相机传感器的方向和 2)您的 UI 的方向,相对于设备的正常方向.我在这里复制了示例代码,它使用 OrientationEventListener 并假设你有一个名为 mParameters 的 Camera.Parameters 对象:

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);
}

那么在你调用takePicture之前,你需要调用Camera.setParameters(mParameters).

Then before your takePicture call, you need to call Camera.setParameters(mParameters).

在您的特定情况下,您可能希望在拍照之前查询方向,并使用示例代码中的逻辑来计算旋转.然后用Camera.getParameters()获取相机参数,调用setRotation,再调用Camera.setParameters().

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().

这篇关于设置 Android 照片 EXIF 方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆