如何将 MediaRecorder 方向设置为横向或纵向

How to set MediaRecorder orientation to landscape or portrait

如何将 MediaRecorder 方向设置为横向或纵向

我一直在 Android

中试用 MediaRecorder class

我看过这段代码

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

我想将正在录制的视频的方向设置为纵向或横向

如何做到这一点

我看过https://developer.android.com/reference/android/media/MediaRecorder.html#setOrientationHint(int)

指定在Int中设置Rotation,Landscape和Portrait分别使用什么值

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

MediaRecorder 可以参考下面的内容

您需要获取当前摄像头方向,然后添加逻辑以根据前置摄像头或后置摄像头设置方向:

以下为camera1API

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

以下为camera2API

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

以下是 camera1API 和 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;
    }
}

将方向设置为横向

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;
}

将方向设置为纵向

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;
  }
}