繁体   English   中英

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

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

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

我一直在尝试Android中的MediaRecorder类

我看了一下这段代码

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设置为Int,分别将横向和纵向使用什么值

int:顺时针旋转的角度,以度为单位。 支撑角度为0、90、180和270度。

您可以从下面参考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的共同点

相机图像的int camera_orientation 该值是相机图像需要顺时针旋转以使其以自然方向正确显示在显示屏上的角度。 它应该是0、90、180或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;
    }
}

将方向设置为横向

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

暂无
暂无

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

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