Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置Android照片EXIF方向_Android_Camera_Android Camera_Exif_Android Orientation - Fatal编程技术网

设置Android照片EXIF方向

设置Android照片EXIF方向,android,camera,android-camera,exif,android-orientation,Android,Camera,Android Camera,Exif,Android Orientation,我写了一个Android活动,通过编程捕捉照片。我想用正确的EXIF方向数据将图像保存为JPEG格式(就像本机Android Camera应用程序自动执行的那样) 以下是实际拍摄照片的方法(我删除了try/catch块): …以及回调: private Camera.PictureCallback jpgCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera came

我写了一个Android活动,通过编程捕捉照片。我想用正确的EXIF方向数据将图像保存为JPEG格式(就像本机Android Camera应用程序自动执行的那样)

以下是实际拍摄照片的方法(我删除了try/catch块):

…以及回调:

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

        releaseCamera();
        savePhoto(data);
};
照片拍摄正确,但我的问题是EXIF数据显示,无论设备的方向如何,方向都设置为“图像方向:左上方”,因此上传照片时,它会出现倒置或旋转

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


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

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

ExifInterface exif;
        try {
            exif = new ExifInterface(filePath);

            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, 0);
            Log.d("EXIF", "Exif: " + orientation);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                matrix.postRotate(180);
                Log.d("EXIF", "Exif: " + orientation);
            } else if (orientation == 8) {
                matrix.postRotate(270);
                Log.d("EXIF", "Exif: " + orientation);
            }
要告诉相机静止图片的方向,请使用:

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

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


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

我已经阅读了此答案,但无法使用,因为我的活动已锁定为纵向模式。
 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);
}