强制相机总是在Android的[复制]开在纵向模式强制相机总是在Android的[复制]开在纵向模式

2019-05-13 15:06发布

这个问题已经在这里有一个答案:

  • Android相机在人像上SurfaceView 6回答

是否有可能在android系统仅在纵向模式下打开相机意图,那就是即使用户翻转装置,相机仍然会在纵向模式下,

我尝试了以下的回答: 强制人像模式 ,但没有工作,谢谢

Answer 1:

试试吧 -

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();
      }
}


文章来源: Force a camera to always open in portrait mode in android [duplicate]