前置摄像头 - 保存前镜像和旋转正确

时间:2016-05-13 13:44:25

标签: android android-camera android-orientation device-orientation android-camera2

我希望在将前置摄像头保存到SD卡之前对其进行镜像。事情是在索尼Xperia Z5等设备上,它在镜像后将图像旋转90度。 我不能使用ExifInterface来获取方向,因为它需要一个文件路径,在我的情况下我还没有保存它。

有没有机会获得特定设备的方向,以便我可以正确旋转它们?

预设:

  • Camera2 Api
  • 仅限人像照片

1 个答案:

答案 0 :(得分:2)

在你的captureBuilder中,你有一个参数来设置" Orientation"在拍摄之前的图像:CaptureRequest.JPEG_ORIENTATION

Android Developer website say:

  

JPEG图像的方向。

     

相对于方向的顺时针旋转角度(以度为单位)   相机,JPEG图片需要旋转,是   直立。

     

相机设备可能会将此值编码为JPEG EXIF标头,   或旋转图像数据以匹配此方向。当图像   数据旋转后,缩略图数据也会旋转。

     

请注意,此方向与方向相关   摄像头传感器,由android.sensor.orientation提供。

您可以在CaptureBuilder中设置此参数:

 //To get the right orientation we must to get it in base of the sensor position.
 mSensorOrientation = getSensorOrientation();
 captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, mSensorOrientation);

从CameraCharacteristics获取您的传感器方向,您可以从CameraManager获取:

 public int getSensorOrientation() throws CameraAccessException {
    return mCameraManager.getCameraCharacteristics(mCameraId).get(
            CameraCharacteristics.SENSOR_ORIENTATION);
}

希望它能帮到你!

编辑: 我附上了一个我很久以前发现的方法来获得真实的"图片的方向,取决于您是否在正面相机,传感器设备方向以及您想要拍摄照片的方向。

   public static int sensorToDeviceRotation(boolean mirror, int deviceOrientation, int sensorOrientation) {

    // Reverse device orientation for front-facing cameras
    if (mirror) {
        deviceOrientation = -deviceOrientation;
    }
    // Calculate desired JPEG orientation relative to camera orientation to make
    // the image upright relative to the device orientation
    return (sensorOrientation + deviceOrientation + 360) % 360;
}
相关问题