Picture orientation from gallery/camera intent

<>

This question already has an answer here:

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.


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

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

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


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.


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

}

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.


I already did that for a project, because I had the same problem (Android thinking you'll only do a picture on landscape). What I did was detecting the phone orientation at the time, and then rotate the image. It supposes the orientation is still the same when the intent is received, though.


This looks like the same problem that was solved in this question: Android: Bitmaps loaded from gallery are rotated in ImageView


Use it this way!

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}

And in the code,

Bitmap newImage = Bitmap.createBitmap(actualImage, 0, 0, width, height, matrix, true);

Simple!


What I am doing : first check the orientation of image taken by camera using its meta data information , and If we found this in portrait then we have to rotate the image by 90 and display otherwise only display.

For getting the Information about orientation of image we can use Exif interface. That's It!


The simple exiftodegree answers only consistently work when you have taken a photo and i use it for such. For those of you experiencing issues with choosing a photo and getting the correct orientation, see my answer here: Image Orientation - Android


Need Your Help

How can I scrape pages with dynamic content using node.js?

javascript node.js phantomjs web-crawler cheerio

I am trying to scrape a website but I don't get some of the elements, because these elements are dynamically created.