android 解决Camera预览时成像角度错误(与镜像的不符)

android 解决Camera预览时成像角度错误(与镜像的不符)

问题描述:手机竖直,当启用前摄像头预览实时的影像时,发现实际影像旋转了90度,如下图
这里写图片描述
解决办法:

当调用Camera.open(frontIndex)之后,调用如下函数即可解决问题。

/**
     * @param activity 相机显示在的Activity
     * @param cameraId 相机的ID
     * @param camera   相机对象
     */
    public 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:
                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);
    }

注:在preview 期间不允许调用该方法

猜你喜欢

转载自blog.csdn.net/zmyhh323/article/details/80348890