gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:39 43 4
gpt4 key购买 nike

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

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

预设:

  • 相机2接口(interface)
  • 仅肖像照片

最佳答案

在您的 captureBuilder 中,您有一个参数可以在拍摄之前设置图像的“方向”:CaptureRequest.JPEG_ORIENTATION

Android Developer website say:

The orientation for a JPEG image.

The clockwise rotation angle in degrees, relative to the orientation to the camera, that the JPEG picture needs to be rotated by, to be viewed upright.

Camera devices may either encode this value into the JPEG EXIF header, or rotate the image data to match this orientation. When the image data is rotated, the thumbnail data will also be rotated.

Note that this orientation is relative to the orientation of the camera sensor, given by 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;
}

关于android - 前置摄像头 - 保存前正确镜像和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211633/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com