2017-03-02 291 views
0

如何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

請發表您迄今嘗試過的內容。發佈您的代碼而不是鏈接。 –

回答

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

謝謝你的確切解釋 – 1234567

相關問題