簡體   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