Picture orientation from gallery/camera intent [du

2019-01-13 15:31发布

I'm getting picture to my app from camera / gallery intent. In many phones picture that I read from the intent / Uri is already rotated to correct orientation. For example N1, Legend, Desire that is the case.

But then on some phones ( for example Milestone1, GalaxyS) the picture is always in landscape more no matter which way the picture was taken. This means that in my application portrait picture is presented wrong way to the user. I tried to read EXIF info of the picture but orientation tag is always 0. There has to be a way to find out the right orientation of the picture because in Milestone1 the gallery application shows the portrait pictures correctly.

How do I know if I need to rotate the picture myself before showing it to the user?

Thank you for you help!

12条回答
你好瞎i
2楼-- · 2019-01-13 15:32

You have to create the gallery object using contentresolver first and then pass the uri created to the image capture intent. Then you can look at the exif data, NOT the gallery orientation data.

查看更多
乱世女痞
3楼-- · 2019-01-13 15:34

The simple exiftodegree answers only consistently work when you have taken a photo and i use it for such. For those of you experiencing issues with choosing a photo and getting the correct orientation, see my answer here: Image Orientation - Android

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-13 15:35
Matrix matrix = new Matrix();

ExifInterface exifReader = new ExifInterface(filePath);

int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

      // Do nothing. The original image is fine.

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

       matrix.postRotate(90);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

       matrix.postRotate(180);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

      matrix.postRotate(270);

}
查看更多
Deceive 欺骗
5楼-- · 2019-01-13 15:35

Use it this way!

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}

And in the code,

Bitmap newImage = Bitmap.createBitmap(actualImage, 0, 0, width, height, matrix, true);

Simple!

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-13 15:36
        int rotate = 0;
        try {
            File imageFile = new File(sourcepath);
            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;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
查看更多
相关推荐>>
7楼-- · 2019-01-13 15:39

What I am doing : first check the orientation of image taken by camera using its meta data information , and If we found this in portrait then we have to rotate the image by 90 and display otherwise only display.

For getting the Information about orientation of image we can use Exif interface. That's It!

查看更多
登录 后发表回答