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

问题描述:

如何MediaRecorder方向设置为横向或纵向如何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)

它指定设置旋转的诠释,我们应该用什么样的价值观的横向和纵向分别

+0

请发表您迄今尝试过的内容。发布您的代码而不是链接。 –

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

谢谢你的确切解释 – 1234567