【问题标题】:Camera automatically get rotated by 90 degree in portrait mode人像模式下相机自动旋转 90 度
【发布时间】:2014-01-13 11:21:57
【问题描述】:

我正在通过此链接开发相机应用程序 http://developer.android.com/guide/topics/media/camera.html 我按照教程进行操作,但是一旦我将屏幕方向设置为纵向模式,我的相机预览就会自动旋转 90 度。如何解决?

【问题讨论】:

  • 只有在设置屏幕方向时才会出现?
  • 如果我们不设置屏幕旋转那么只会出现这个问题..

标签: android camera


【解决方案1】:

在包含 Camera 实例的 surfaceView 上,您必须实现一些方法。其中之一是surfaceChanged。在该方法中,您应该按如下方式更新方向:

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
               int height) {

   ...
   this.setCameraDisplayOrientation(this.cameraId, this.mCamera);
   ...    
}

以及实现:

   /**
     * Calling this method makes the camera image show in the same orientation as the display. 
     * NOTE: This method is not allowed to be called during preview.
     * 
     * @param cameraId
     * @param camera
     */
    @SuppressLint("NewApi")
    public void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
         int rotation = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).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 (Utils.hasGingerbread()) {
             android.hardware.Camera.CameraInfo info =
                     new android.hardware.Camera.CameraInfo();
             android.hardware.Camera.getCameraInfo(cameraId, info);
             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;
             }
         } else {
             // on API 8 and lower devices
             if (context.getResources().getConfiguration().orientation !=Configuration.ORIENTATION_LANDSCAPE) {
                 result = 90;
             } else {
                 result = 0;
             }
         }
         try {
             camera.setDisplayOrientation(result);
         } catch (Exception e) {
             // may fail on old OS versions. ignore it.
             e.printStackTrace();
         }
     }

【讨论】:

    【解决方案2】:

    旋转将取决于设备。我已经遇到过这个问题,我找到的解决方案是使用this method

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多