2012-04-21 56 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