Android 相机预览 横屏竖屏 -- 显示

相机在设备上处于固定位置,无论设备是否 是手机、平板电脑或计算机。当设备方向更改时, 相机方向更改。常见的布局显示比率是 4:3。

  • 对于前置摄像头,图像缓冲区逆时针旋转(从 传感器的自然方向)
  • 对于后置摄像头,图像缓冲区顺时针旋转(从 传感器的自然方向)

相机方向

  • 前摄像头

在这里插入图片描述

  • 图像必须逆时针旋转 270 度,以便预览的 方向与设备方向匹配:

  • 后置摄像头将生成具有相同方向的图像缓冲区 作为上面的缓冲区,但是是 90 度。结果, 缓冲液顺时针旋转 90 度。SENSOR_ORIENTATION

因为相机图像传感器在 传感器的自然方向(横向),图像缓冲区必须旋转 相机预览指定的度数 以设备的自然方向直立显示。对于前置摄像头, 旋转是逆时针的;对于后置摄像头,顺时针方向。SENSOR_ORIENTATION

如果有兴趣了解详细的解释,点击官网查看

下面是代码分析

相机方向

前/后置预览布局 给固定的宽高(4:3),为了防止预览拉伸

           //判断是前置还是后置
        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;
            }
        }

摄像机的角度

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

  • rotation =0(Surface.ROTATION_0),如下图
    在这里插入图片描述

  • rotation =1(Surface.ROTATION_90), 如下图

预览角度

/**
     * 设置 摄像头的角度
     * @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);
    }

  • 竖屏后置效果图
  • 竖屏前置效果图

猜你喜欢

转载自blog.csdn.net/afufufufu/article/details/128331237