Android 中的摄像头方向无法随显示改变?

Camera orientation can not change with display in Android?

我知道已经有这样的问题,但是 none 解决了。我得到了一些帮助并找出了根据显示器更改相机所需的基本代码。

   cameraInfo=new Camera.CameraInfo();
                camera.getCameraInfo(camId,cameraInfo);
                int angle=0;
                int rotation = getActivity().getWindowManager().getDefaultDisplay()
                        .getRotation();
                switch(rotation)
                {
                    case Surface.ROTATION_0:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 0");
                        camera.setDisplayOrientation(90);
                        break;
                    case Surface.ROTATION_90:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 90");
                        camera.setDisplayOrientation(0);

                        break;
                    case Surface.ROTATION_180:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 180");
                        camera.setDisplayOrientation(0);

                        break;
                    case Surface.ROTATION_270:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 270");
                        camera.setDisplayOrientation(180);
                        break;
                    default:
                        camera.setDisplayOrientation(0);
                        break;
                }

            }

但是,我无法获得关于设备旋转的正确显示预览。我尝试了几种不同的角度组合。但是,none 给了我正确的输出。我附上我的预览图片。请解释问题的解决方案。另外,我想知道我的设备什么位置对应角度0、90、180、270。也就是说,0对应纵向还是横向?

图片: 景观在这一个中倒转了。当 ROTATION_180 时,我尝试将旋转设置为 180。这样做没有成功,因为另一个方向(横向)变成倒转

倒置方向没有任何作用。期望的结果是通过正确的预览来降低点击按钮。我不知道为什么这不起作用。我在每次旋转中尝试了几个值,但 none 解决了这个问题。

我是初学者,所以请详细说明。谢谢

你应该检查 CameraInfo.orientation for the device. Note that this property gives rotation from the natural display orientation, given for the device by Display.getRotation()

如果您的自定义相机应用使用 android.hardware.camera2 API,此补偿将自动发生。

如果您的自定义相机 activity 没有锁定屏幕方向,那么它将受到 "reverse landscape" 故障的影响,需要 special treatment 其中涉及 OrientationEventListener.

您可以在预览前像这样旋转图像:

  public static Bitmap rotateImage(Bitmap bitmapSrc) {
    Matrix matrix = new Matrix();
    matrix.postRotate(previewRotation);
    return Bitmap.createBitmap(bitmapSrc, 0, 0,
            bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

"previewRotation" 可以从:

    private static void setCameraDisplayOrientation(Context mContext, android.hardware.Camera.CameraInfo info) {
    int rotation = ((CameraActivity) mContext).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;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        previewRotation = (info.orientation + degrees) % 360;
        previewRotation = (360 - previewRotation) % 360;  // compensate the mirror
    } else {  // back-facing
        previewRotation = (info.orientation - degrees + 360) % 360;
    }
    mCameraInstance.setDisplayOrientation(previewRotation);
}