android - 相机记录有时倒置

标签 android camera orientation landscape mediarecorder

当我横向拍摄时,如果它处于 0 度,它可以拍摄出很好的视频,但是如果我将手机旋转超过 180 度,它就会倒置录制,我该如何更改? 当我开始录制时,我执行以下代码:

myMediaRecorder.setPreviewDisplay(mySurfaceHolder.getSurface());

        if (rotationInDegreeValue == 90) {
            LogService.log(TAG, "set orientation hint : " + 0);
            myMediaRecorder.setOrientationHint(0);
        } else if (rotationInDegreeValue == 270) {
            LogService.log(TAG, "set orientation hint : " + 180);// 180
            myMediaRecorder.setOrientationHint(180);
        }

        myMediaRecorder.prepare();
        myMediaRecorder.start();

然后,我有一个定向监听器:

orientationListener = new OrientationEventListener(getActivity(), SensorManager.SENSOR_DELAY_UI) {

        @Override
        public void onOrientationChanged(int orientation) {
            LogService.log(TAG, "onOrientationChanged");

            if ((myCamera == null)) {
                return;
            }
            Log.d(TAG, "orientation changed : " + orientation);
            if (((orientation < 45) || (orientation > 315) || ((orientation < 225) && (orientation > 135))) && !isRecording) {
                if (!isAlertShown && !isUserListDisplayed) {
                    // AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
                    // alertDialogBuilder.setTitle("Orientation").setMessage("Return to landscape").setCancelable(false);
                    // orientationAlert = alertDialogBuilder.create();
                    // orientationAlert.show();
                    isAlertShown = true;
                }

            } else {
                if (isAlertShown) {
                    // orientationAlert.hide();
                    // orientationAlert.dismiss();
                    isAlertShown = false;
                }
            }
            rotateCamera();
        }
    };

这是 RotateCamera 函数:

private void rotateCamera() {

    LogService.log(TAG, "rotateCamera()");

    int cameraId = CameraInfo.CAMERA_FACING_BACK;

    if (isUsingBackCam) {
        cameraId = CameraInfo.CAMERA_FACING_FRONT;
    }
    android.hardware.Camera.CameraInfo myCameraInfo = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, myCameraInfo);
    Display display;
    display = getActivity().getWindow().getWindowManager().getDefaultDisplay();

    int rotation = display.getRotation();
    int degrees = 0;

    Point size = new Point();
    display.getSize(size);
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        rotationInDegreeValue = 0;
        SCREEN_HEIGHT = size.x;

        break;
    case Surface.ROTATION_90:
        degrees = 90;
        rotationInDegreeValue = 90;
        SCREEN_HEIGHT = size.y;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        rotationInDegreeValue = 180;
        break;
    case Surface.ROTATION_270:
        rotationInDegreeValue = 270;
        degrees = 270;
        SCREEN_HEIGHT = size.y;
        break;
    }
    int result;
    if (myCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (myCameraInfo.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = ((myCameraInfo.orientation - degrees) + 360) % 360;
    }
    if (!isRecording) {
        try {
            myCamera.setDisplayOrientation(result);
        } catch (Exception e) {
            LogService.err(TAG, e.getMessage(), e, 1);
        }
    }
}

当我将手机旋转 180 度时,我必须做什么才能阻止相机倒置录制。我试图注释掉定位监听器,但仍然没有成功。 另外,我不知道这是否重要,但这发生在 fragment 中。 fragment 的 Activity,没有设置 onconfigurationchanged。

最佳答案

setOrientationHint 函数有效,但我必须附加更多视频,发生这种情况时,视频的组成矩阵会丢失。 我已经更改了 list ,从 SensorLandscape 到 Landscape,这就是我解决这个问题的方法。

关于android - 相机记录有时倒置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16414201/

相关文章:

android - 实时查询Android Camera2的相机参数

iphone - 支持多界面,但主屏幕只有单一界面,在 iOS8 + iPhone 中不工作

IOS:在 UINavigationController 中锁定方向

android - 火力地堡 : How to check file already exist before Upload files

android - 如何在 Android 中创建快速或慢速相机闪光灯闪烁信号?

android - 损坏的 ActionBar 外观

android - 从已经运行的预览中录制视频

iphone - UIImagePickerController 图像方向

java - 在 android studio 中加载完整属性面板时出错

Java (Android) : Ive created a bunch of views using a loop, 并希望删除其中之一