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

相關問題