相机在纵向模式下自动旋转90度

时间:2014-01-13 11:21:58

标签: android camera

我从这个链接开发了一个相机应用程序 http://developer.android.com/guide/topics/media/camera.html 我按照教程,但只要我将屏幕方向设置为纵向模式,我的相机预览就会自动旋转90度。怎么解决?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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