Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picture orientation from gallery/camera intent [duplicate]

I'm getting picture to my app from camera / gallery intent. In many phones picture that I read from the intent / Uri is already rotated to correct orientation. For example N1, Legend, Desire that is the case.

But then on some phones ( for example Milestone1, GalaxyS) the picture is always in landscape more no matter which way the picture was taken. This means that in my application portrait picture is presented wrong way to the user. I tried to read EXIF info of the picture but orientation tag is always 0. There has to be a way to find out the right orientation of the picture because in Milestone1 the gallery application shows the portrait pictures correctly.

How do I know if I need to rotate the picture myself before showing it to the user?

Thank you for you help!

like image 345
Juhani Avatar asked Dec 23 '10 09:12

Juhani


People also ask

Why does an image captured using camera intent gets rotated?

Answer: Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.

How do I fix the camera orientation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

Why do my pictures turn sideways on Samsung?

To adjust the screen rotation settings: Swipe down from the top of the screen to open the Quick settings panel. Look for the screen orientation icon. Depending on your settings, you may need to look for the Portrait, Landscape, or Auto Rotate icon.


7 Answers

Florian answer is working for gallery images.

The following code works for captured images, havn't tried with gallery images but i believe it should work. Hope this helps anybody.

Code :

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
     int rotate = 0;
     try {
         context.getContentResolver().notifyChange(imageUri, null);
         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }


         Log.v(TAG, "Exif orientation: " + orientation);
     } catch (Exception e) {
         e.printStackTrace();
     }
    return rotate;
 }

EDIT: As can be read in the comments, some devices do not support Exif information, havn't checked which but i think HTC doesn't. be sure to check what devices and create an alternative.

like image 90
eric.itzhak Avatar answered Oct 17 '22 22:10

eric.itzhak


The following method returns the orientation of an image in degrees. It works with images from the gallery. Return values for:

  • normal landscape: 0
  • normal portrait: 90
  • upside-down landscape: 180
  • upside-down portrait: 270
  • image not found: -1

The code:

public static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);

    try {
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return -1;
        }
    } finally {
        cursor.close();
    }
}
like image 28
Florian Fankhauser Avatar answered Oct 17 '22 22:10

Florian Fankhauser


        int rotate = 0;
        try {
            File imageFile = new File(sourcepath);
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
like image 34
vineet pal Avatar answered Oct 17 '22 23:10

vineet pal


This is the best answer I have found, by ramaral. https://stackoverflow.com/a/20480741/2258389
Works great for gallery or camera

like image 33
Jason Avatar answered Oct 17 '22 23:10

Jason


You have to create the gallery object using contentresolver first and then pass the uri created to the image capture intent. Then you can look at the exif data, NOT the gallery orientation data.

like image 20
Eric Novins Avatar answered Oct 17 '22 22:10

Eric Novins


Matrix matrix = new Matrix();

ExifInterface exifReader = new ExifInterface(filePath);

int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

      // Do nothing. The original image is fine.

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

       matrix.postRotate(90);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

       matrix.postRotate(180);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

      matrix.postRotate(270);

}
like image 29
Anh Duy Avatar answered Oct 18 '22 00:10

Anh Duy


I have the same problem with S1, and I noted even if you open the image using stock gallery app it will open in landscape mode(the phone doesn’t know the correct orientation for photo).
And if you rotate the phone to portrait in camera app the icons (take pic, focus and settings) will not rotated, try that in S2 or any device support landscape/portrait camera those icons will be rotated.

What I am sure the stock camera app doesn’t support taking photos in prorate mode.

like image 23
JafarKhQ Avatar answered Oct 17 '22 23:10

JafarKhQ