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

标签 android orientation 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)

指定设置Int中的Rotation,横向和纵向分别使用什么值

最佳答案

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

关于android - 如何将 MediaRecorder 方向设置为横向或纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553164/

相关文章:

java - 在早期 Android 系统中使用旋转 vector

ios - 如何更改 imagePicker 中的图像方向(swift3)

Android:方向改变时的后退按钮动画

Android MediaRecorder 录制循环回自身

android - 如何改进将输出 APK 从 Android 项目复制到新文件夹的任务?

android - TimePicker 小部件未获取当前时间

android - 类型 容器 'Android Dependencies' 引用不存在的库 android-support-v7-appcompat/bin/android-support-v7-appcompat.jar

.net - 如何判断照片是横向拍摄还是纵向拍摄? JPEG .NET 元数据方向

javascript - 使用MediaRecorder录制5秒的音频片段,然后上传到服务器

node.js - ffmpeg : Bitstream not supported by this decoder