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

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

问题描述

我写了编程拍摄照片一个Android的活动。我想将图像保存为用正确的EXIF方向数据的JPEG(就像原生的Andr​​oid相机应用会自动执行)。

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

...和回调:

...and the callback:

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

要告诉相机你想要的方向为静态图片,使用<一个href=\"http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setRotation%28int%29\"相对=nofollow> Camera.Parameters.setRotation():

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

有参考code在开发者文档了解如何正确使用它,这是一个有点棘手,因为你设置的值取决于1)你的相机传感器的方向; 2)你的用户界面的方向,相对于装置的正常方向。我复制了这些样本code在这里,它使用一个 OrientationEventListener 并假设你有一个Camera.Parameters对象调用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).

在您的具体情况,您可能要查询的方向拍摄照片前权,并使用逻辑样品code来计算旋转。然后得到与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天全站免登陆