Android 10 solves the problem that the camera preview does not match the actual direction

  question:

         In Android 10, after rotating the screen orientation, the direction in which the camera captures the picture is inconsistent with the direction we previewed. How to solve this problem?

When we rotate the screen to vertical screen by default, the data collected by camera rotation is generally horizontal, and the direction of human preview is generally vertical. The collecting direction of the camera is fixed. No matter how we rotate the phone, it will collect in the fixed direction.

If we want the collected data to be previewed correctly, we need to rotate the preview screen according to the collection direction and screen rotation angle.

This is the initial orientation of our screen: landscape by default

 When our screen is rotated 270 degrees and the default is vertical screen, the photo direction

         The image data of the mobile phone camera (Camera) comes from the image sensor of the camera hardware. After this image sensor is fixed to the mobile phone, it will have a default viewing direction. This viewing direction happens to be the direction when the left side of the mobile phone is placed horizontally. Its coordinates The origin is the upper left corner of the phone when it is placed horizontally. The normal orientation of the mobile phone and the default viewing direction of the image sensor are as follows:

        From the diagram above, we can see that the viewing direction of the camera sensor is at an angle of 90 degrees to the normal direction of the phone. At this time, we need to consider rotating the camera 90 degrees to achieve the normal viewing direction. Logically speaking, when we rotate the camera and open the camera in the normal phone orientation, the preview image we see is vertical, and the actual image should be consistent with the preview. However, after taking a photo, when we open the system gallery, the actual image we see is horizontal, that is, the preview image is inconsistent with the orientation of the phone.

        This is because the camera that comes with the system rotates the data collected by the image sensor based on the current orientation of the phone screen at the bottom of the Android system. Therefore, no matter how we rotate the phone screen, the camera preview image we see is always "abnormal" . For a custom camera, if the image sensor image is not rotated, the actual image you see will be horizontal:

 solve:

In the source code path of android 10.1, the interface for camera rotation camera direction displays normally after modification.

1.Camera’s source code path for rotating the camera direction:

android/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp

android/hardware/interfaces/camera/device/3.4/default/ExternalCameraUtils.cpp

2. Solve the problem of inconsistency between the camera preview direction and the actual image direction:

diff --git a/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp b/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
index c2a49a3b5e..ea0a013c52 100755
--- a/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
+++ b/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
@@ -2889,7 +2889,7 @@ status_t ExternalCameraDeviceSession::initDefaultRequests() {
     UPDATE(md, ANDROID_JPEG_QUALITY, &jpegQuality, 1);
     UPDATE(md, ANDROID_JPEG_THUMBNAIL_QUALITY, &jpegQuality, 1);
 
-    const int32_t jpegOrientation = 0;
+    const int32_t jpegOrientation = 90;
     UPDATE(md, ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1);
 
     const uint8_t oisMode = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
diff --git a/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraUtils.cpp b/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraUtils.cpp
index e25deff797..f872f3dc75 100644
--- a/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraUtils.cpp
+++ b/android/hardware/interfaces/camera/device/3.4/default/ExternalCameraUtils.cpp
@@ -162,7 +162,7 @@ namespace {
     const int kDefaultJpegBufSize = 5 << 20; // 5MB
     const int kDefaultNumVideoBuffer = 4;
     const int kDefaultNumStillBuffer = 2;
-    const int kDefaultOrientation = 0; // suitable for natural landscape displays like tablet/TV
+    const int kDefaultOrientation = 90; // suitable for natural landscape displays like tablet/TV
                                        // For phone devices 270 is better
 } // anonymous namespace
 

        At this point, the camera displays normally and the preview is consistent with the actual image.

Guess you like

Origin blog.csdn.net/qq_53676406/article/details/131915974