我的相机功能已从Camera更新为Camera2 API。
[当我使用前置摄像头捕获图像并将其显示在imageview中时。图像的方向改变了。然后,我使用此代码来更改图像的方向。
int jpegOrientation =
(ORIENTATIONS.get(rotation) + characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) + 270) % 360;
capturebuilder.set(CaptureRequest.JPEG_ORIENTATION, jpegOrientation);
以上代码适用于我测试过的android版本(即版本5,9)。当我在Android版本10中运行相同的代码时,它不起作用。下面是图片
请任何人可以帮助我解决这个问题
private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) {
if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0;
int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);
// Round device orientation to a multiple of 90
deviceOrientation = (deviceOrientation + 45) / 90 * 90;
// Reverse device orientation for front-facing cameras
boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
if (facingFront) deviceOrientation = -deviceOrientation;
// Calculate desired JPEG orientation relative to camera orientation to make
// the image upright relative to the device orientation
int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;
return jpegOrientation;
}
要了解更多详细信息,请关注此职位:https://medium.com/@kenodoggy/solving-image-rotation-on-android-using-camera2-api-7b3ed3518ab6