Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems - exif oriented image #126

Closed
MFlisar opened this issue Oct 9, 2015 · 12 comments
Closed

Problems - exif oriented image #126

MFlisar opened this issue Oct 9, 2015 · 12 comments

Comments

@MFlisar
Copy link

MFlisar commented Oct 9, 2015

Sometimes they are not placed correctly, instead of centered they are wrongly placed on top, probably becasue they are not rotated as they should be. I use the images in a ViewPager and the auto rotation does sometimes work, sometimes not...

My solution looks like following and is working always, don't know why yours does not (I only use images from the sd card, so they are either from the media store directly or they have an uri created from a path - this is for all hidden images):

if (Temp.ROTATE_BIG_IMAGE_MANUALLY)
    ivImage.setOrientation(((Image) mMediaFile).getRotation());
else
    ivImage.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
ivImage.setMinScale(0.1f);
ivImage.setMaxScale(4f);
ivImage.setOnClickListener(this);
ivImage.setImage(ImageSource.uri(mMediaFile.getUri()));
ivImage.setOnImageEventListener(this);

With following getRotation function:

public int getRotation()
{
    int exifOrientationInDegress = 0;
    if (isFromMediaStore())
    {
        final String[] columns = { MediaStore.Images.Media.ORIENTATION };
        final Cursor cursor = MainApp.get().getContentResolver().query(getUri(), columns, null, null, null);
        if (cursor != null)
        {
            if (cursor.moveToFirst())
            {
                exifOrientationInDegress = cursor.getInt(0);
            }
            cursor.close();
        }
    }
    else
    {
        ExifInterface exifReader = null;
        try
        {
            exifReader = new ExifInterface(getRealPath());
            exifOrientationInDegress = MediaUtil.convertExifOrientationToDegrees(exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0));
        }
        catch (IOException e)
        {
            L.e(this, e);
        }
    }
    return exifOrientationInDegress;
}

If I manually rotate the image, everything is working fine

@Uscher1
Copy link

Uscher1 commented Oct 9, 2015

EXIF orientation is handled in Release 3.4.0 when image is from media store, but not when image comes from file. Please handle also this case with ExifInterface as suggested here. Thank you.

@davemorrissey
Copy link
Owner

I think the problem @MFlisar reported will be fixed by PR #129. EXIF orientation has always been supported for files, and still is, but for images that are small enough not to require subsampling, this bug has been introduced in 3.4.0.

@davemorrissey
Copy link
Owner

Fixed in release 3.4.1

@Shusshu
Copy link

Shusshu commented Oct 21, 2015

I just encountered this problem on 3.4.1 with a local file

@Uscher1
Copy link

Uscher1 commented Oct 23, 2015

I confirm problem is not fixed in 3.4.1. Please reopen issue.

@davemorrissey
Copy link
Owner

Can you please provide:

  • Your code
  • The original image
  • A screenshot
  • Affected devices and Android versions

@Shusshu
Copy link

Shusshu commented Oct 26, 2015

    image.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
    image.setQuickScaleEnabled(true);
String fileUri = "/sdcard/xxx/xx/xx/img.jpg"; //the content of this string looks like this
if (fileUri.startsWith("/")) {
    fileUri = Constants.FILE_SCHEME + fileUri; // I have to add file:// otherwise it won't work
}
image.setImage(ImageSource.uri(Uri.parse(fileUri)));

Screenshot
http://imgur.com/seVttbj

original img
http://imgur.com/HTCcQxq

android 5.x at least, sony experia & emulator genymotion

@Uscher1
Copy link

Uscher1 commented Oct 26, 2015

In my sample code below I fix the problem with a workaround that sets the orientation explicitely.

            ImageSource imgSource = null;
            try
            {
                imgSource = ImageSource.uri(file.getPath());
            }
            catch (Exception ignored)
            {
            }
            if (imgSource != null)
            {
                // In subsampling-scale-image-view Release 3.4.1 the orientation is not properly
                // handled. Workaround: We handle EXIF angle manually.
                imgView.setOrientation(getMediaRotationAngle(file));
                txtView.setVisibility(View.GONE);
                imgView.setImage(imgSource);
            }

Retrieve orientation...

    private int getMediaRotationAngle(File file)
    {
        // Create EXIF reader
        ExifInterface exifReader;
        try
        {
            exifReader = new ExifInterface(file.getPath());
        }
        catch (Exception e)
        {
            return 0; // something is wrong with the file
        }

        // Retrieve rotation
        int exifOrientation = 0;
        int rotation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (rotation == ExifInterface.ORIENTATION_ROTATE_90)
        {
            exifOrientation = 90;
        }
        else if (rotation == ExifInterface.ORIENTATION_ROTATE_180)
        {
            exifOrientation = 180;
        }
        else if (rotation == ExifInterface.ORIENTATION_ROTATE_270)
        {
            exifOrientation = 270;
        }

        return exifOrientation;
    }

Original image - It is not correcty displayed here, too. Click on it and you see the correct orientation where the tire is vertical.
issue126-origimg

Screenshot (without workaround) - Wrong orientation. Tire should be vertical.
issue126-screenshot

Latest test was done on LG Nexus 5 and Android 6 (API 23), but also happened before Android 6.

@Shusshu
Copy link

Shusshu commented Oct 28, 2015

@Uscher1 I just tried with image.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); and it works.

@davemorrissey shouldn't it be the default behaviour?

@davemorrissey
Copy link
Owner

EXIF orientation is only enabled if you use that setting, as the wiki says. The default has always been to display the image in its native orientation because EXIF support was added some time after I released this library. Changing the default would be backwardly incompatible and lead to unexpected problems for some developers, as well as making the initialisation slightly slower.

It looks like all the comments on this bug since I closed it don't actually relate to the original bug, and the problems appear to be caused by the USE_EXIF setting not being enabled.

@Uscher1
Copy link

Uscher1 commented Oct 28, 2015

I expected EXIF to be considered by default. But I see using ORIENTATION_USE_EXIF does the trick.

@Xlythe
Copy link

Xlythe commented Jun 19, 2017

I'm still seeing bugs with ORIENTATION_USE_EXIF, when I use subsamplingScaleImageView.setImage(ImageSource.uri(...));. It works if I check the Exif rotation values of the file myself and set the orientation manually.

Note that the uris are of the type "content://mms/part/[id]". I didn't test if it's specific to mms uris or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants