2

I am building an android application that uses the camera capabilities. The issue I am having is the image data (byte[]) that I get from the front camera comes back upside down on my samsung s7 and nexus phone. It appears correct in the preview, but after I save the data as is and then display the images in a gallery they are all upside down. I know I can just flip the image data before saving but I have tested the code on a blu C 5.0 HD running 4.4 (kitkat) and the image data on that phone comes in oriented the right way. So alwyas flipping the image will cause the error on other devices. I am told the issue is because when the new samsung and nexus phones are built, the front facing cameras are built upside down to save room. I am not sure if this is correct,but if so, if I flip all images it will mess up the phones that have the correct orientation of the camera. So is there a way to detect the orientation of the image data before saving the image?

Here is the code I am using:

mCamera.takePicture(null, null, mPicture);

Callback:

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

        processCameraCallback(data);
    }
};

Processing Data:

    public void processCameraCallback(byte[] data) {
    confirmPhoto(true);
    //Make a new empty picture file
    try {
        pictureFile = Utils.createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        Log.e(TAG, "Failed to create photo file: " + ex.toString());
        confirmPhoto(false);
        return;
    }

    //Write the file to the storage
    try {
        FileOutputStream fos;
        if (pictureFile != null) {

            fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
        confirmPhoto(false);
        return;
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
        confirmPhoto(false);
        return;
    }
}
BB Developer
  • 374
  • 3
  • 18
  • Did you ever figure this out? I'm having the same issue with an S7 using the native camera app, also compensating for the orientation returned in the EXIF data. – Eric B. Sep 09 '16 at 21:18

1 Answers1

5

Use this code to adjust camera orientation:

private  int detectCameraDisplayOrientation(Activity activity,
                                            Camera.CameraInfo info) {

    int rotation = activity.getWindowManager().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 (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;
    }
   return result;
}

This is from camera api sample and works correctly on nexus 5x.(with upside down camera)

after that just call camera.setDisplayOrientation(displayOrientation); and it will save picture correctly.

Raiv
  • 5,731
  • 1
  • 33
  • 51
  • that code is detecting whether or not the entire phone is rotated. My issue is that if I take a normal upriht image with the front facing camera the image data, is coming back with the image upside down. I have the samsung s7 edge and the nexus 5, both having that issue. I also have a blu C 5.0 HD running 4.4 (kitkat) and the image data on that phone comes in oriented the right way. – BB Developer May 09 '16 at 23:16
  • this code is setting proper rotation of camera image based on phone rotation. Compensating camera upside-down position. Have you tried it? If yes and it hasn't work - ok... If not - your comment seems strange. Camera orientation itself is in the `cameraInfo.orientation` field, if you need only it. – Raiv May 10 '16 at 11:17
  • Sorry, I did try your solution and it did not fix it. – BB Developer May 11 '16 at 00:20
  • 5
    camera.setDisplayOrientation only fix the problem in preview, but images are still saved in the wrong orientation. – Tim Autin Jun 17 '16 at 08:26
  • Yes, this is hardware dependent thing. Look here - http://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image afaik if image is saved in wrong orientation, it should write proper exif tag. – Raiv Jun 19 '16 at 05:25