简体   繁体   English

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

[英]How to set MediaRecorder orientation to landscape or portrait

How to set MediaRecorder orientation to landscape or portrait 如何将MediaRecorder方向设置为横向或纵向

I have been trying out the MediaRecorder class in Android 我一直在尝试Android中的MediaRecorder类

I had a look at this code 我看了一下这段代码

http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/ 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) 我看了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 它指定将Int Rotation设置为Int,分别将横向和纵向使用什么值

int: the angle to be rotated clockwise in degrees. int:顺时针旋转的角度,以度为单位。 The supported angles are 0, 90, 180, and 270 degrees. 支撑角度为0、90、180和270度。

You can take the refernce from the below for MediaRecorder. 您可以从下面参考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 以下是用于camera1API

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

Below is for camera2API 以下是用于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 以下是camera1API和camera2API的共同点

The int camera_orientation of the camera image. 相机图像的int camera_orientation 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. 它应该是0、90、180或270。例如,假设设备的屏幕自然很高。 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. 如果摄像头传感器的顶部与屏幕的右边缘自然对齐,则该值应为90。如果前置摄像头传感器的顶部与屏幕的右侧对齐,则该值应为是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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM