0

Je suis coincé à un point où je ne peux pas changer l'orientation de l'image lorsque j'ai pris la photo en utilisant un appareil photo personnalisé. Je veux gérer manuellement l'orientation. Je ne sais pas comment faire pivoter une image dans onConfigurationchanged lorsque Camera.PictureCallback est appelée. J'ai changé avec succès l'orientation quand la photo n'est pas prise. Je ne règle pas l'image dans n'importe quelle vue d'image à la place je la montre en vue extérieure. Voici mon code de mise framelayout et surface pour elle:Android comment faire pivoter une photo en fonction de l'orientation lors de la prise de vue caméra personnalisée

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 

lorsque la photo est prise:

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

       pictaken = true; 
      } 
}; 

et onConfiguration Changé:

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

     } 
    } 

Toute aide est appréciée. Dois-je faire pivoter framelayout ou ce que je dois faire après la prise de photo et l'utilisateur change l'orientation.

Répondre

0

Faire usage de OrientationEventListener comme suit:

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

Ne pas oublier d'activer et de désactiver OrientationEventListener:

Pour activer:

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

Pour désactiver:

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

Merci pour votre réponse. mais où dois-je ajouter ce OrientationEventListener dans le code? ou devrais-je supprimer onconfigurationchanged? – exceptionnotgood

+0

vous pouvez ajouter ce code dans votre onCreate() mais assurez-vous de l'utiliser après l'initialisation de la caméra. Activez ceci à partir de onResume() et de Disable à partir de onPause(). –

+0

Cela a un problème que lorsque vous prenez une photo et que l'appareil est déplacé vers l'image paysage est supprimé et il allume à nouveau l'appareil photo. – exceptionnotgood