如何将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,我们应该分别使用Landscape和Portrait的值
答案 0 :(得分:2)
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;
}
}