简体   繁体   中英

How to find out if the image from the gallery was taken in portrait or landscape from camera

  • I have a image in gallery which was taken from camera
  • I am trying to find out if the image is portrait or landscape

I have the path obtained from below code as

/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg

Then i am using the ExifInterface to find the orientation:

 public static void getCameraPhotoOrientation(Activity activity, String imagePath) {



        String path =getPath(activity,imagePath);


        try {
            ExifInterface ei = new ExifInterface(path);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    Timber.d("Type-1");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    Timber.d("Type-2");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    Timber.d("Type-3");
                    break;
                case ExifInterface.ORIENTATION_UNDEFINED:
                    Timber.d("Type-4");
                    break;

            }
        }catch (IOException ex){
            ex.printStackTrace();
        }

    }

I always get orientation as ExifInterface.ORIENTATION_UNDEFINED


How to resolve this

I have a image in gallery which was taken from camera

There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.

I am trying to find out if the image is portrait or landscape

There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.

I have the path obtained from below code

That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri , use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri , then pass that InputStream to this ExifInterface constructor .

I always get orientation as ExifInterface.ORIENTATION_UNDEFINED

Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION .

If that tag does not exist, you could examine the width and height of the image and make a guess:

  • If the width is greater than the height, it might be landscape
  • If the height is greater than the width, it might be portrait

This will fail for square images, and this will fail for cropped images (eg, the photo was taken portrait, but the user cropped it such that the width is now greater than the height).

The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.

Try it by changing path accessing process like below

File imFile = new File(imagePath);
ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());

this will explain Android getAbsolutePath() not returning full path it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM