0
votes

I make a custom camera on android and faced with the orientation problem. Activity with surfaceview is portrait orientation, because of this not work onConfigurationChanged listener, but I whant it. I want to save picture from camera always in normal orientation,but the images are saved with different orientations depending on how the orientation of the photo was taken.

code:

         public void onClickPicture(View view) {
              camera.takePicture(null, null, new Camera.PictureCallback() {
                  @Override
                  public void onPictureTaken(byte[] data, Camera camera) {
                       try { 
                          FileOutputStream fos = new FileOutputStream(photoFile);
                          fos.write(data);
                          fos.close();
                          Bitmap bm = BitmapFactory.decodeByteArray(data,0,data.length);
                          ExifInterface ei;
                              ei = new ExifInterface(photoFile.getAbsolutePath());
                              int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
                              switch (orientation) {
                                  case ExifInterface.ORIENTATION_NORMAL:
                                  //ALAWAYS THIS CASE
                                  break;
                                  case ExifInterface.ORIENTATION_ROTATE_90:
                                      bm = rotateImage(bm, 90);
                                  break;
                                  case ExifInterface.ORIENTATION_ROTATE_180:
                                      bm = rotateImage(bm, 180);
                                  break;
                                  case ExifInterface.ORIENTATION_ROTATE_270:
                                      bm = rotateImage(bm, 270);
                                  break;
                               }
                           } catch (Exception e) {
                              e.printStackTrace();
                     }
                 });
           }
1
If you post your custom camera code, people will be able to help you better. - Mahm00d

1 Answers

1
votes

You need to extend the OrientationEventListener and create an instance of it in your activity. After that you invoke enable method of this instance.
Whenever the orientation of your device changes, the method OrientationEventListener#onOrientationChanged(int) will be called. In this method you can calculate the new rotation to be set in camera's parameters via the Camera.Parameters#setRotation(int) method.