0

我創建了相機活動並將其方向固定爲清單中的縱向。方向鎖定時監聽設備方向更改

<activity 
      android:name=".ui.activity.CameraActivity" 
      android:screenOrientation="portrait" 
      > 

當我通過媒體錄像機拍攝視頻時,我需要了解方向以將視頻保存在正確的位置。

mediaRecorder.setOrientationHint(orentation);

我是從這個函數

public int setCameraDisplayOrientation(Activity activity, 
              int cameraId, android.hardware.Camera camera) { 
     android.hardware.Camera.CameraInfo info = 
       new android.hardware.Camera.CameraInfo(); 
     android.hardware.Camera.getCameraInfo(cameraId, info); 
     int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
     int degrees = 0; 
     switch (rotation) { 
      case Surface.ROTATION_0: 
       degrees = 0; 
       break; 
      case Surface.ROTATION_90: 
       degrees = 90; 
       break; 
      case Surface.ROTATION_180: 
       degrees = 180; 
       break; 
      case Surface.ROTATION_270: 
       degrees = 270; 
       break; 
     } 

     int result; 
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
     } else { // back-facing 
      result = (info.orientation - degrees + 360) % 360; 
     } 

     Camera.Parameters params = camera.getParameters(); 
     params.setRotation(result); 
     camera.setParameters(params); 
     camera.setDisplayOrientation(result); 
     return result; 
    } 

如果不從清單這種方法工作得很好固定的方向變得取向,但我需要解決的方向我的影片只保存在肖像狀態。

幫我確定風景模式。

回答

0

就可以解決這個問題,通過編程方式更改活動的方向在視頻模式下:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

只是檢測相機模式(圖像/視頻),並改變方向相應