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

本文详细介绍了摄像头在不同设备上的方向调整与图像旋转原理。针对前后置摄像头在横竖屏模式下如何设置图像预览的宽高比,以及如何根据设备方向调整摄像头预览角度进行了深入解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相机在设备上处于固定位置,无论设备是否 是手机、平板电脑或计算机。当设备方向更改时, 相机方向更改。常见的布局显示比率是 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);
    }

  • 竖屏后置效果图
  • 竖屏前置效果图
android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了; 需要申请的permission,在AndroidManifest.xml中添加: 主要功能,打开前camera private Camera openFrontFacingCameraGingerbread() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { cam = Camera.open(camIdx); mCurrentCamIndex = camIdx; } catch (RuntimeException e) { Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } 根据打开时的横方向来调整preview角度 //根据横自动调节preview方向,Starting from API level 14, this method can be called when preview is active. private 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(); //degrees the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. //The starting position is 0 (landscape). int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; 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); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值