Android camera2 photo rotation angle, and mirror image

1. Rotate the photo by 90 degrees.

Path: packages\apps\ Camera2
found three CaptureRequest.JPEG_ORIENTATION, modified to solve:

src/com/android/camera/one/v2/SimpleOneCameraFactory.java:199:                rootBuilder.setParam(CaptureRequest.JPEG_ORIENTATION,
src/com/android/camera/one/v2/OneCameraImpl.java:401:                builder.set(CaptureRequest.JPEG_ORIENTATION,
src/com/android/camera/one/v2/OneCameraZslImpl.java:824:                builder.set(CaptureRequest.JPEG_ORIENTATION,

rootBuilder.setParam(CaptureRequest.JPEG_ORIENTATION, 90);    //旋转90度

2. The preview is rotated 90 degrees

src/com/android/camera/TextureViewHelper.java

In the constructor:

mPreview.setRotation(90);

3. Take a photo and save the image

Path: packages\apps\Camera2\src\com\android\camera

diff --git a/apps/Camera2/src/com/android/camera/Storage.java b/apps/Camera2/src/com/android/camera/Storage.java
index 6023f2b11..6eab98b9d 100755
--- a/apps/Camera2/src/com/android/camera/Storage.java
+++ b/apps/Camera2/src/com/android/camera/Storage.java
@@ -42,6 +42,10 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
+import android.graphics.Matrix;
+import com.android.camera.util.CameraUtil;
+import java.io.ByteArrayOutputStream;
+

 import javax.annotation.Nonnull;

@@ -277,12 +281,35 @@ public class Storage {
     public static Uri updateImage(Uri imageUri, ContentResolver resolver, String title, long date,
            Location location, int orientation, ExifInterface exif,
            byte[] jpeg, int width, int height, String mimeType) throws IOException {
+
+        int i = 2;
+
+        if(i == 1){  //源码
         Log.d(TAG, "updateImage:" + width + "x" + height + ",jpeg:" + (jpeg == null ? 0 : jpeg.length)
                 + ",orientation:" + orientation);
         String path = generateFilepath(title, mimeType);
         writeFile(path, jpeg, exif);
         return updateImage(imageUri, resolver, title, date, location, orientation, jpeg.length, path,
                 width, height, mimeType);
+
+        }else { //镜像再存盘
+        Bitmap bitmap = CameraUtil.makeBitmap(jpeg, width * height);
+        Matrix m = new Matrix();
+        m.postScale(-1, 1);  // 镜像水平翻转
+        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
+        byte[] jpegData = baos.toByteArray();
+
+        Log.d(TAG, "updateImage:" + width + "x" + height + ",jpeg:" + (jpeg == null ? 0 : jpeg.length)
+                + ",orientation:" + orientation);
+        String path = generateFilepath(title, mimeType);
+        writeFile(path, jpegData, exif);
+        return updateImage(imageUri, resolver, title, date, location, orientation, jpegData.length, path,
+                 width, height, mimeType);
+        }
     }

Recently, Android camera2 is used as a custom camera, and the basic process from opening the camera to previewing is not described much.

Please refer to the article https://github.com/gengqifu/361Camera to know

Today I will talk in detail about the problem of the rotation angle that I encountered during the driving process.

Go directly to the code:

//初始化传感器定位

orientationEventListener = object : OrientationEventListener(mActivity) {
            override fun onOrientationChanged(orientation: Int) {
                Log.e("orientation", "orientation=$orientation")
                sensorOrientation = when (orientation) {
                    -1 -> ORIENTATION_VERTICAL
                    in 70..134 -> 90
                    in 135..224 -> 180
                    in 225..280 -> 270
                    else -> 0 //这里应该是281-69的范围
                }
            }
        }
        orientationEventListener.enable()

This is the callback code for sensor positioning in the camera, which is kotlin code.

The code rotates within 360 degrees on a vertical plane

Normally, there should be 4 intervals such as 45-134 135-224 225-314 315-45

Every 90 degrees is an average score, but in actual development, we judge that when taking pictures with a horizontal screen, the mobile phone almost has to be rotated 90 degrees to indicate that the user is a horizontal screen

So I increased the angle range where the orientation is 0 and reduced the range where the orientation is 90 and 270 (the range determined to be a horizontal screen photo).

Therefore, you can adjust your range to meet your needs according to your needs.

The above is how to deal with when our mobile phone takes pictures vertically and horizontally?
insert image description here
You can see that there is an orientation of -1 in my log, which means that the phone has been placed horizontally at this time

At this time, if you rotate left and right in the horizontal plane, the orientation will not change, it will be -1

Therefore, when we are at -1, in fact, the default is to take pictures with the vertical screen, which is a point that is easy to ignore.

val captureBuilder = cameraDevice?.createCaptureRequest(
                CameraDevice.TEMPLATE_STILL_CAPTURE
            )?.apply {
                if (::previewImageReaderSurface.isInitialized) {
                    addTarget(previewImageReaderSurface)
                }
                set(
                    CaptureRequest.JPEG_ORIENTATION,
                    CameraUtil.getJpegOrientation(
                        CameraConfig.getCurrentCameraCameraCharacteristics(),
                        sensorOrientation
                    )
                )
                if (lightIsOpened()) {
                    set(CaptureRequest.CONTROL_AE_MODE, 
                       CaptureRequest.CONTROL_AE_MODE_ON)
                    set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH)
                } else {
                    set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
                }
                set(
                    CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH
                )
                set(
                    CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
                )
                //锁定焦点
                set(
                    CaptureRequest.CONTROL_AF_TRIGGER,
                    CameraMetadata.CONTROL_AF_TRIGGER_START
                )
                //手机水平拍摄的时候  设置成VERTICAL
                if (sensorOrientation == ORIENTATION_VERTICAL) {
                    set(CaptureRequest.JPEG_ORIENTATION, 90)
                }

            }

This is to set the orientation of the output picture to 90 when judging that the phone is horizontal before taking pictures.

I tested the camera that comes with the phone, and it does the same when shooting horizontally.

Guess you like

Origin blog.csdn.net/xiaowang_lj/article/details/131822084