【问题标题】:Front camera - Mirror and Rotate correctly before saving前置摄像头 - 保存前正确镜像和旋转
【发布时间】:2016-05-13 13:44:25
【问题描述】:

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

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

预设:

  • Camera2 接口
  • 仅限肖像图片

【问题讨论】:

  • 你找到解决方案了吗,我也面临同样的问题 ;(

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


【解决方案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;
}

【讨论】:

  • 谢谢您的回复。问题是我从不同的设备得到相同的旋转。他们都是270的前置摄像头。但只有一个设备在将镜像矩阵添加到位图后向图像添加 90 度:(
  • 什么是设备方向?纵向方向是什么?
  • snesor 方向是你手机传感器方向的位置。你可以从 cameraCharacteristics 得到它。设备方向是您拍摄照片时设备的方向(0,90,180,270)。在您的情况下,对于纵向,它应该是 0(或者如果您的手机旋转了可能是 180)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
相关资源
最近更新 更多