简体   繁体   中英

How to check whether the photo was taken in landscape or portrait mode in Android?

I'm a newbie in Android. I'm trying to make an application with the photo capturing feature inside. The problem is the photo rotation is not right. If I take the photo in landscape mode, it will be good, but in portrait mode the photo rotation will be wrong. My question is: can I check whether the photo is taken in landscape/portrait mode? Because as I checked on the LogCat I can see the tag named "CameraEngine" and it says rotation: 0 or 90. It will be cool if I can get that kind of camera information by code.

You can compare image width and height:

Bitmap bmp = your photo;

if(bmp.getWidth() > bmp.getHeight())
{
   // landscape
}else
{
   // portrait
}

You cannot check in Android if the photo was taken specially with PhoneGape 2.9 and beyond.

Because you have to extract this information from EXIF property stored in the taken photo and android doesn't even write out the orientation to this property.

you can refer to this reply to the question bellow for more information : canvas drawImage() with photos taken on iphone in landscape are rotated

This should do what you need. It returns the orientation as an angle (0/90/180/270):

private int getOrientation(Uri aUri, ContentResolver aRslv) {
    Cursor _cursor = aRslv.query(aUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);

    if (_cursor != null) {
        try {

            if (_cursor.moveToFirst()) {
                return _cursor.getInt(0);
            } else {
                return -1;
            }
        } finally {
            _cursor.close();
        }
    } else {
        return 0;
    }
}

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