1

我想在保存到SD卡之前镜像前置摄像头的图像。事情是在像索尼Xperia Z5这样的设备上,镜像后也会将图像旋转90度。 我不能使用ExifInterface来获取方向,因为它需要一个文件路径,在我的情况下我还没有保存它。前置摄像头 - 镜像并在保存前正确旋转

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

预设:

  • Camera2阿比
  • 只有肖像照片
+0

让你找到了解决办法,I'm面临同样的问题;( – Andrea

回答

1

在你captureBuilder,你有一个参数来设置图像的 “方向” 是抽放前: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; 
} 
+0

日Thnx为您回应的事情是,我从不同的设备得到相同的旋转,他们都是270的前置摄像头。但是只有一个设备在向位图添加镜像矩阵之后向图像添加了90度:( – Maxi

+0

检查最后的编辑,也许它会帮助您 –

+0

您是否修复了它? –