简体   繁体   English

如何检查照片是在Android的横向还是纵向模式下拍摄的?

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

I'm a newbie in Android. 我是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. 因为当我检查LogCat时,我可以看到名为“ CameraEngine”的标签,它的旋转角度为:0或90。如果可以通过代码获取这种相机信息,那将会很酷。

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. 您无法在Android中检查照片是否是使用PhoneGape 2.9及更高版本专门拍摄的。

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. 因为您必须从存储在所拍摄照片中的EXIF属性中提取此信息,而android甚至不会写出此属性的方向。

you can refer to this reply to the question bellow for more information : canvas drawImage() with photos taken on iphone in landscape are rotated 您可以参考以下关于问题的更多信息的回复: 旋转带有iphone上横向拍摄的照片的canvas drawImage()

This should do what you need. 这应该做您需要的。 It returns the orientation as an angle (0/90/180/270): 它以角度(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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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