Faisal Shaikh
Faisal Shaikh

Reputation: 4138

How to check whether the image is Portrait or Landscape in Android?

Currently, I am working on app which is related to handle pictures. I am modifying the imageview depending on the whether the image is portrait or landscape.

I know I can figure out image is portrait or landscape by comparing the height and width of images.

But the problem I am facing is some images are portrait but the width of image is more than height. Here are those images:

image1

image2

Above images are portrait but if you calculate height and width you will find that width is more than height.

Is there any method in Android which returns whether the image is portrait or landscape?

Upvotes: 3

Views: 5242

Answers (3)

chargerstriker
chargerstriker

Reputation: 496

I had the same problem. I load a bitmap into the image view, I read the bitmap from the file, and then see if the width is bigger than the height.

if(bitmap.getWidth() > bitmap.getHeight()){
                //meaning the image is landscape view
                view.setLayoutParams(new FrameLayout.LayoutParams(400, ViewPager.LayoutParams.WRAP_CONTENT));
            }else{
                view.setLayoutParams(new FrameLayout.LayoutParams(250, ViewPager.LayoutParams.WRAP_CONTENT));
            }

Upvotes: 1

John O'Reilly
John O'Reilly

Reputation: 10330

If I'm understanding question correctly, one approach is to do something like following using Picasso

            Picasso.with(this).load(url).into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    <compare width/height of bitmap to check aspect ratio of image>
                }

Upvotes: 0

JamesSwinton
JamesSwinton

Reputation: 92

This method will return the Orientation & rotation required for your image, which you should be able to use to determine whether the image is portrait or landscape.

public 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.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

Upvotes: 0

Related Questions