Android Camera recording video but plays upside down

后端 未结 10 1553
南旧
南旧 2020-12-10 04:42

I record a video using the below code and it records perfectly, but when it plays the video, it plays it upside down.

I tried settings mrec.setOrientationHint(

相关标签:
10条回答
  • 2020-12-10 05:03

    I have the same problem, I note that camera preview orientation angle and recording video angle aren't the same. So I use this method for change orientation on video recording:

    public static int getVideoOrientationAngle(Activity activity, int cameraId) { //The param cameraId is the number of the camera.
        int angle;
        Display display = activity.getWindowManager().getDefaultDisplay();
        int degrees = display.getRotation();
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        switch (degrees) {
            case Surface.ROTATION_0: 
                angle = 90; 
                break;
            case Surface.ROTATION_90:
                angle = 0;
                break;
            case Surface.ROTATION_180:
                angle = 270;
                break;
            case Surface.ROTATION_270:
                angle = 180;
                break;
            default:
                angle = 90;
                break;
        }
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
            angle = (angle + 180) % 360;
    
        return angle;
    }
    

    And that for change orientation on camera preview:

     public static int setCameraDisplayOrientation(Activity activity,
                                                  int cameraId, android.hardware.Camera camera) {
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.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);
        return result;
    }
    

    Note that's important to know if the camera is front or back.

    0 讨论(0)
  • 2020-12-10 05:04

    for portrait mode set your mediaRecorder.setOrientationHint(90); degree is same as your camera orientation myCamera.setDisplayOrientation(90);

    0 讨论(0)
  • 2020-12-10 05:06

    Use OrientationEventListener and track rotation value when device is rotated code here.This code apply rotation to camera but for recording you need apply rotation to MediaRecorder.When you start recording just mMediaRecorder.setOrientationHint(rotation) before mMediaRecorder.prepare(). This solve my problem.

    0 讨论(0)
  • 2020-12-10 05:08

    rotate your MediaRecorder like below corresponding the degree you used in Camera display orientation for front facing camera video recording

    Display display = ((WindowManager)getContext().getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            parameters.setPreviewSize(height, width);
            if(display.getRotation() == Surface.ROTATION_0)
            {
    
                mCamera.setDisplayOrientation(90);
                mMediaRecorder.setOrientationHint(270);
            }
    
            if(display.getRotation() == Surface.ROTATION_90)
            {
                mMediaRecorder.setOrientationHint(180);
            }
    
            if(display.getRotation() == Surface.ROTATION_180)
            {
                mMediaRecorder.setOrientationHint(270);
            }
    
            if(display.getRotation() == Surface.ROTATION_270)
            {
                mCamera.setDisplayOrientation(180);
                mMediaRecorder.setOrientationHint(0);
            }
    
    0 讨论(0)
提交回复
热议问题