Héctor
Héctor

Reputation: 26054

Get orientation Exif tag from photo

I'm trying to detect the orientation of a photo taken by native camera. This is my code:

ExifInterface exifInterface = new ExifInterface(photoPath);
String orientation = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);

if (Configuration.ORIENTATION_PORTRAIT == Integer.parseInt(orientation)) {
    Log.d(TAG, "Portrait! " + orientation);
} else {
    Log.d(TAG, "Landscape! " + orientation);
}

However, if I take a photo in portrait mode, Landscape! 6 is printed. And if I take it in landscape mode, Portrait! 1 is printed.

Compile and target SDK versions are 21. In android.content.res.Configuration class there are these two constants:

public static final int ORIENTATION_PORTRAIT = 1; 
public static final int ORIENTATION_LANDSCAPE = 2;

Why am I getting 1 when landscape and 6 when portrait?

Upvotes: 0

Views: 413

Answers (1)

Ram Prakash Bhat
Ram Prakash Bhat

Reputation: 1308

Use "ExifInterface.getAttributeInt() or getAttribute()..."

This method is to get correct value of image in which angle you need to rotate.

 - "ExifInterface.getAttributeInt" will returns angle of rotation you
   need to make,not portrait or landscape

See the following snippet for your  understanding.

    ExifInterface exif = null;
           try {
               exif = new ExifInterface(path);
           } catch (IOException e) {
               e.printStackTrace();
           }
           try {
               switch (exif
                       .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1)) {
                   case ExifInterface.ORIENTATION_ROTATE_180:
                       return 180;
                   case ExifInterface.ORIENTATION_ROTATE_90:
                       return 90;
                   case ExifInterface.ORIENTATION_ROTATE_270:
                       return 270;
                   case ExifInterface.ORIENTATION_NORMAL:
                       return 0;
                   default:
                       return 0;
               }
           } catch (NullPointerException e) {
               e.printStackTrace();
           }
           return 0;

Upvotes: 1

Related Questions