1

当我在横向时,如果它是在 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 度时,我该怎么做才能阻止相机倒置。我试图注释掉方向监听器,但仍然没有运气。另外,我不知道它是否重要,但这发生在一个片段中。片段的Activity,没有设置onconfigurationchanged。

4

1 回答 1

1

setOrientationHint 函数有效,但我不得不附加更多视频,当这种情况发生时,视频的合成矩阵会丢失。我在清单中进行了更改,从 SensorLandscape 更改为 Landscape,这就是我解决此问题的方法。

于 2013-05-07T12:13:47.273 回答