2012-04-21 121 views
5

是否可以在android中以僅肖像模式打開相機意圖,即使用戶翻轉設備,相機仍將處於肖像模式,強制相機總是在肖像模式下打開在android中

我嘗試了以下的回答:Force Portrait Mode但沒有工作,感謝

+0

也看到http://stackoverflow.com/questions/3491961/android-capture-photo和HTTP ://stackoverflow.com/questions/2543059/android-camera-in-portrait-on-surfaceview/6762941#6762941 – EpicPandaForce 2016-02-09 13:54:11

回答

4

試試吧 -

private void 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.setDisplayOrientation(result); 
} 

private void initCamera() { 
    if(mCamera == null) { 
      mCamera = Camera.open(); 
      setCameraDisplayOrientation(activity, CameraInfo.CAMERA_FACING_BACK, mCamera); 
      mCamera.unlock(); 
     } 
} 
+1

'cameraID'僅在API Lv.9之上可用。來自Google的這些方法可能適用於API Lv.9以上的那些設備,對於那些低於9的設備,可以使用if語句查看是否需要'Build.VERSION.SDK_INT'大於'Build.VERSION_CODES.GINGERBREAD'。 – dumbfingers 2012-09-25 09:10:12

+1

工作於API Lv14及以上版本,崩潰於2.3.6 – 2013-04-01 07:29:48

相關問題