Search code examples
androidorientationmediarecorder

How to set MediaRecorder orientation to landscape or portrait


How to set MediaRecorder orientation to landscape or portrait

I have been trying out the MediaRecorder class in Android

I had a look at this code

http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/

I want to set the orientation of the video being recorded to either portrait or Landscape

How can this be done

I Had a look at https://developer.android.com/reference/android/media/MediaRecorder.html#setOrientationHint(int)

It specifies to set the Rotation in Int, what values should we use for Landscape and Portrait Respectively


Solution

  • int: the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees.

    You can take the refernce from the below for MediaRecorder.

    You need to get the current camera orientation and then add the logic to set the orientation based on the front camera or back camera:

    Below is for camera1API

    Camera.CameraInfo camera_info = new Camera.CameraInfo()
    int camera_orientation = camera_info.orientation;
    

    Below is for camera2API

    CameraCharacteristics characteristics;
    CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
    characteristics = manager.getCameraCharacteristics(cameraIdS);
    int camera_orientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
    

    Below is common for both camera1API and camera2API

    The int camera_orientation of the camera image. The value is the angle that the camera image needs to be rotated clockwise so it shows correctly on the display in its natural orientation. It should be 0, 90, 180, or 270. For example, suppose a device has a naturally tall screen. The back-facing camera sensor is mounted in landscape. You are looking at the screen. If the top side of the camera sensor is aligned with the right edge of the screen in natural orientation, the value should be 90. If the top side of a front-facing camera sensor is aligned with the right of the screen, the value should be 270.

    private int getDeviceDefaultOrientation() {
        WindowManager windowManager = (WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE);
        Configuration config = getResources().getConfiguration();
        int rotation = windowManager.getDefaultDisplay().getRotation();
        if( ( (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
                config.orientation == Configuration.ORIENTATION_LANDSCAPE )
                || ( (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&    
                config.orientation == Configuration.ORIENTATION_PORTRAIT ) ) {
            return Configuration.ORIENTATION_LANDSCAPE;
        }
        else { 
            return Configuration.ORIENTATION_PORTRAIT;
        }
    }
    

    Set orientation to landscape

    int device_orientation = getDeviceDefaultOrientation();
    int result;
    if (device_orientation == Configuration.ORIENTATION_PORTRAIT) {
      // should be equivalent to onOrientationChanged(270)
      if (camera_controller.isFrontFacing()) {
        result = (camera_orientation + 90) % 360;
      } else {
        result = (camera_orientation + 270) % 360;
      }
    } else {
      // should be equivalent to onOrientationChanged(0)
      result = camera_orientation;
    }
    

    Set orientation to portrait

    int device_orientation = getDeviceDefaultOrientation();
    int result;
    if (device_orientation == Configuration.ORIENTATION_PORTRAIT) {
      // should be equivalent to onOrientationChanged(0)
      result = camera_orientation;
    } else {
      // should be equivalent to onOrientationChanged(90)
      if (camera_controller.isFrontFacing()) {
        result = (camera_orientation + 270) % 360;
      } else {
        result = (camera_orientation + 90) % 360;
      }
    }