0

当我使用自定义照相机拍摄照片时,我无法更改图片方向。我想手动处理方向。我不知道如何调用Camera.PictureCallback时如何旋转onConfigurationchanged中的图片。我没有拍摄照片时成功更改了方向。我没有在任何图像视图中设置图像,而是在表面视图中显示它。这是我设置的FrameLayout和表面为它的代码:Android如何在拍摄照片时根据方向旋转照片自定义照相机

Framelayout camera_view = (FrameLayout) findViewById(R.id.camera_view); 
CameraSurfaceCreater mCameraView = new CameraSurfaceCreater(this, mCamera, CameraSetter.this);//create a SurfaceView to show camera data 
      camera_view.addView(mCameraView);//add the SurfaceView to the layout 

时拍摄照片:

Camera.PictureCallback mPicture = new Camera.PictureCallback() { 
      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 

       pictaken = true; 
      } 
}; 

和onConfiguration更改:

@Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      if(!isPictureTaken){ 
       setCameraDisplayOrientation(CameraSetter.this, 0, mCamera); 
      }else { 
       //dont know how to rotate a photo when picture is taken 
      } 

     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
      if(!isPictureTaken){ 
       setCameraDisplayOrientation(CameraSetter.this, 0, mCamera); 
      } 

     } 
    } 

任何帮助表示赞赏。是否必须旋转framelayout或拍摄照片后用户更改方向后必须执行的操作。

回答

0

。利用OrientationEventListener的如下:

OrientationEventListener mOrientationEventListener = new OrientationEventListener(mApplication, 
      SensorManager.SENSOR_DELAY_NORMAL) { 

     @Override 
     public void onOrientationChanged(int orientation) { 

      if ((orientation == ORIENTATION_UNKNOWN) || (mCamera == null)) { 
       return; 
      } 

      Log.e("current_ori", "" + orientation); 

      Camera.Parameters params = mCamera.getParameters(); 
      Camera.CameraInfo info = new Camera.CameraInfo(); 

      Camera.getCameraInfo(cameraId, info); 

      orientation = (orientation + 45)/90 * 90; 

      int rotation = 0; 

      if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 

       //Below one line will rotate image to portrait 
       // rotation = (info.orientation - orientation + 360) % 360; 

       //Below code makes image exactly how it has been captured(with mirror) 

       if (orientation == 360 || orientation == 0 || orientation == 180) { 
        rotation = 270; 
       } else if (orientation == 90 || orientation == 270) { 
        rotation = 90; 
       } 

      } else { 
       /* 
       * back-facing camera 
       */ 
       //Below one line will rotate image to portrait 
       //rotation = (info.orientation + orientation) % 360; 

       //Below line makes image exactly how it has been captured 
       rotation = 90; 

      } 

      params.setRotation(rotation); 

      if (null == mCamera) { 
       return; 
      } 

      mCamera.setParameters(params); 
     } 
    }; 

不要忘了启用和禁用OrientationEventListener:

启用:

if (mOrientationEventListener.canDetectOrientation()) { 
     mOrientationEventListener.enable(); 
    } 

禁用:

if (mOrientationEventListener.canDetectOrientation()) { 
     mOrientationEventListener.disable(); 
    } 
+0

感谢您的回答。但我应该在代码中添加此OrientationEventListener?或者我应该删除onconfigurationchanged? – exceptionnotgood

+0

您可以在onCreate()中添加此代码,但请确保在相机初始化后使用此代码。从onResume()启用此功能并从onPause()中禁用。 –

+0

这有一个问题,即当您拍摄照片并且设备移动到风景照片时,它将被移除,并且会再次打开相机。 – exceptionnotgood

相关问题