question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong picture orientation on Samsung devices

See original GitHub issue

Hi,

After taking a picture with this library I create a Bitmap and set it in an ImageView as follow:

 public void onPictureTaken(CameraView cameraView, byte[] data) {
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
     imageView.setImageBitmap(bitmap);
}

This works fine on a Nexus 5X, however on Samsung devices the picture orientation appears to be incorrectly rotated 90 degrees to the left when the device orientation is portrait. Is there a way to fix this issue?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:18
  • Comments:25 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
ivacfcommented, Dec 27, 2016

I don’t think this is a issue with the library anymore, the application should be in charge of rotating the image if necessary by reading the image Exif data. This can be easily achieved using the new Exif support library

The exif library allows you to easily find out how much you have to rotate the image before you display it to the user. This is how it could be done:

@Override
public void onPictureTaken(CameraView cameraView, byte[] data) {
            // Find out if the picture needs rotating by looking at its Exif data
            ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            int rotationDegrees = 0;
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotationDegrees = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotationDegrees = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotationDegrees = 270;
                    break;
            }
            // Create and rotate the bitmap by rotationDegrees
}

I think this issue can be closed now.

19reactions
MoustafaElsaghiercommented, Apr 22, 2018

Solved on Samsung and sony devices (tested devices).

I’ve solved this issue by checking if the width > height of the image then rotate by 90

private static int fixOrientation(Bitmap bitmap) {
        if (bitmap.getWidth() > bitmap.getHeight()) {
            return 90;
        }
        return 0;
    }

then I call this method to apply the rotation if needed

public static Bitmap flipIMage(Bitmap bitmap) {
        //Moustafa: fix issue of image reflection due to front camera settings
        Matrix matrix = new Matrix();
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

the reason why I set scale by (-1,1) because I use front camera and need to fix the refliction issue of camera.

hope this solves the issue you have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to auto rotate or fix screen rotation on your Galaxy phone
1 Swipe down twice from the top of the screen to open your quick settings · 2 Tap Portrait or Landscape to turn...
Read more >
Samsung S10 - photos show wrong EXIF-orientation ...
Lot of phones do ths. Change photo viewer, which rotate images with exif information. Reply Reply with quote Reply to thread ...
Read more >
SOLVED: Auto-Rotate Not Working On Samsung - YouTube
The main job of the Accelerometer Sensor in a Samsung device is to tell Android OS whether the screen should be in Portrait...
Read more >
Android camera/picture orientation issues with Samsung ...
Yes, a malicious user could quickly rotate the device to landscape and back, and you will report wrong orientation. But the camera sets...
Read more >
How to Fix It When Android Screen Won't Rotate - Lifewire
Turn on Auto rotate. · Don't touch the screen. · Restart your Android phone. · Allow Home screen rotation. · Update your Android....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found