相机有时记录颠倒

时间:2013-05-07 08:07:32

标签: 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度时,我需要做什么,以防止相机上下颠倒录制。我试图评论方向听众,但仍然没有运气。 此外,我不知道它是否重要,但这发生在片段中。片段的Activity,没有onconfigurationchanged set。

1 个答案:

答案 0 :(得分:1)

setOrientationHint功能有效,但我不得不追加更多视频,当发生这种情况时,视频的构图矩阵就会丢失。 我已经在清单中进行了更改,从SensorLandscape到Landscape,这就是我解决这个问题的方法。