首页 > 解决方案 > 在 android 版本 10 中使用 camera2 api 捕获时出现图像方向问题

问题描述

我已将我的相机功能从 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 中运行相同的代码时,它不起作用。下面是图片 在此处输入图像描述

拜托,任何人都可以帮助我解决这个问题

标签: androidandroid-permissionsandroid-camera2android-camera-intent

解决方案


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


推荐阅读