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

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

问题描述

我写了一个Android活动,以程序化的方式捕捉照片。我想使用正确的EXIF方向数据将图像保存为JPEG(就像Android Camera应用自动执行的操作一样)。

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?

编辑:我无法使用屏幕方向,因为活动被锁定为纵向模式。

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:

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天全站免登陆