我想在将前置摄像头的图像保存到 SD 卡之前对其进行镜像。问题是在索尼 Xperia Z5 等一些设备上,它在镜像后也会将图像旋转 90 度。我不能使用 ExifInterface 来获取方向,因为它需要一个文件路径,在我的情况下我还没有保存它。
是否有机会获得特定设备的方向,以便我可以正确旋转它们?
预设:
- 相机2 API
- 只有肖像图片
我想在将前置摄像头的图像保存到 SD 卡之前对其进行镜像。问题是在索尼 Xperia Z5 等一些设备上,它在镜像后也会将图像旋转 90 度。我不能使用 ExifInterface 来获取方向,因为它需要一个文件路径,在我的情况下我还没有保存它。
是否有机会获得特定设备的方向,以便我可以正确旋转它们?
预设:
在您的 captureBuilder 中,您有一个参数可以在拍摄之前设置图像的“方向”: CaptureRequest.JPEG_ORIENTATION
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;
}