Android Camera recording video but plays upside down

后端 未结 10 1574
南旧
南旧 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.

提交回复
热议问题