Android camera preview horizontal screen vertical screen -- display

The camera is in a fixed position on the device, whether the device is a phone, tablet, or computer. When the device orientation changes, the camera orientation changes. A common layout display ratio is 4:3.

  • For the front camera, 图像缓冲区逆时针旋转(from the sensor's natural orientation)
  • For the rear camera, 图像缓冲区顺时针旋转(from the sensor's natural orientation)

camera direction

  • front camera

insert image description here

  • The image must be rotated 270 degrees counterclockwise so that the orientation of the preview matches the device orientation:

  • The rear camera will generate an image buffer with the same orientation as the upper buffer, but at 90 degrees. As a result, the buffer is rotated 90 degrees clockwise. SENSOR_ORIENTATION

Because the camera image sensor is in the sensor's natural orientation (landscape), the image buffer must be rotated the specified degree for the camera preview to display upright in the device's natural orientation. For the front camera, the rotation is counterclockwise; for the rear camera, clockwise. SENSOR_ORIENTATION

If you are interested in a detailed explanation, click on the official website to view

The following is code analysis

camera direction

The front/back preview layout is given a fixed width and height (4:3), in order to prevent the preview from stretching

           //判断是前置还是后置
        if (Utils.isCurOriLand(this)){
    
    //横屏
            if (MainActivity3.getMode()==0){
    
    //后置
                surfaceView.getLayoutParams().width=640;
                surfaceView.getLayoutParams().height=480;
            }else {
    
    //前置
                surfaceView.getLayoutParams().width=480;
                surfaceView.getLayoutParams().height=640;
            }
        }else {
    
    //竖屏
            if (MainActivity3.getMode()==0){
    
    //后置
                surfaceView.getLayoutParams().width=480;
                surfaceView.getLayoutParams().height=640;
            }else {
    
    //前置
                surfaceView.getLayoutParams().width=640;
                surfaceView.getLayoutParams().height=480;
            }
        }

camera angle

  • int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

  • rotation =0 (Surface.ROTATION_0), as shown below
    insert image description here

  • rotation =1 (Surface.ROTATION_90), as shown below

preview angle

/**
     * 设置 摄像头的角度
     * @param activity 上下文
     * @param cameraId 摄像头ID(假如手机有N个摄像头,cameraId 的值 就是 0 ~ N-1)
     * @param camera   摄像头对象
     */
    public static void setCameraDisplayOrientation(Activity activity,
                                                   int cameraId, Camera camera) {
    
    

        Camera.CameraInfo info = new Camera.CameraInfo();
        //获取摄像头信息
        Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        //获取摄像头当前的角度
        int degrees = 0;
        switch (rotation) {
    
    
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                if (MainActivity3.getMode()==0){
    
    //后置
                    degrees = 90;
                }else {
    
    
                    degrees = 270;
                }
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }
        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    
    
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
        } else {
    
    
            // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
    }

  • Vertical screen rear effect picture
  • Vertical screen front effect picture

Guess you like

Origin blog.csdn.net/afufufufu/article/details/128331237