jdamcd / android-crop

Android library project for cropping images
4.54k stars 1.08k forks source link

Image cropped is rotated. #46

Open maxouille opened 10 years ago

maxouille commented 10 years ago

Hi !

I found your lib and it's a very good work. Thank you very much.

However, i have a little problem. When my image is cropped, result picture is rotated (90 on the right).

Is a known issue or it's just me ?

Thx :p

akohout commented 10 years ago

I have the same issue

anxiaoyi commented 10 years ago

I also have the same issue

ghost commented 10 years ago

Same issue here

baba43 commented 9 years ago

+1

brandon515 commented 9 years ago

+1

HughJeffner commented 9 years ago

I tried the fix referenced but I got horrible quality output image.

apinder commented 9 years ago

It should use the Exif orientation tag to orientate the image correctly. If you're viewing the cropped image later on you'll need to use the ExifInterface and rotate the image accordingly. The image is more than likely rotated by the camera rather than android-crop.

However, if you use the same input URI as the output URI the Exif orientation data will not get copied, the only solution to this is it to use a different URI for the output URI than the input. I've raised an issue here regarding this, it should be a simple fix but as the project is built with Gradle + Android Tools it's hard for me to do the fix without first learning a new IDE and build tool.

jdamcd commented 9 years ago

Thanks @apinder. I'll take a proper look at this one (and the PRs) when I'm back at work in the new year.

akohout commented 9 years ago

Any news here?

naixx commented 9 years ago

+1

zplesac commented 9 years ago

+1

rawGH commented 9 years ago

+1

jdamcd commented 9 years ago

This might do it: https://github.com/jdamcd/android-crop/tree/exif-same-uri

rawGH commented 9 years ago

Does not work for me. Tried on LG G3 Lolipop and a samsung phone android 4.4.4. Behaviour is the same:

  1. original image is taken by internal camera, and it is rotated, but includes exif rotation info.
  2. image is correctly shown in the crop activity.
  3. image is stored after cropping.

Resulting image has no exif data and is incorrectly rotated.

Edit: input and output URIs are different.

Edit2: I was hacking the code too much so I deleted everything and made a new project with android studio, clean crop lib download. The difference is that now the images are not shown correctly even in the crop activity. Lolipop LG G3.

andhie commented 9 years ago

In CropUtil#getExifRotation and CropUtil#saveExifRotation is poorly named, causing a bug where wrong exif value is saved. i dont think its appropriate to call the result of getExifRotation as exifRotation as its actually the photo rotation in degrees. When you call the saveExifRotation and pass in the rotation (in degrees) it save the wrong value. It needs to be translated back into the correct exif values (0, 1, 3, 6, 8)

public static boolean saveExifRotation(File file, int exifRotation) {
    if (file == null) return false;
    try {
        ExifInterface exifDest = new ExifInterface(file.getAbsolutePath());
        int exifValue;
        switch (exifRotation) {
            case 0:
                exifValue = ExifInterface.ORIENTATION_NORMAL;
                break;
            case 90:
                exifValue = ExifInterface.ORIENTATION_ROTATE_90;
                break;
            case 180:
                exifValue = ExifInterface.ORIENTATION_ROTATE_180;
                break;
            case 270:
                exifValue = ExifInterface.ORIENTATION_ROTATE_270;
                break;
            default:
                exifValue = ExifInterface.ORIENTATION_UNDEFINED;
                break;
        }
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(exifValue));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        Log.e("Error saving Exif rotation data", e);
        return false;
    }
}
mafiu commented 9 years ago

My behavior is the same as described by @rawGH I try a lot of things but nothing works. I'm using a Samgung Galaxy S4 with Android 5.0.2

naveenchoudhary947 commented 9 years ago

any solution yet ?

kazemimehdi commented 9 years ago

I solved this issue temporarily with using landscape screen orientation for CropImageActivity. However, this is a frustrating bug. I hope android-crop contributors consider it.

misaka-10032 commented 9 years ago

I think it's an issue of ImageView#setImageURI. That function doesn't consider exif of the image. Please refer to this solution.

Moretax commented 9 years ago

Is there any solution already? I am not able to make it work both same and different source and destination URIs.

misaka-10032 commented 9 years ago

@Moretax What's your problem? My solution is change ImageView#setImageURI to something as is mentioned in this post.

Moretax commented 9 years ago

The problem with this approach is that I always get 0 in this line:

exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

Either portrait and landscape.

misaka-10032 commented 9 years ago

@Moretax Then it should be. I tested on Nexus 5 and Samsung Note 3, they both work fine. Nexus 5 seems to always take a picture without orientation, while samsung sometimes have an orientation and sometimes doesn't. I've read some part of the code and find that the cropped image have copied the exif from the photo that has been taken. It seems to be ok. I don't know your phone model and the exact scenario that makes you feel like it's rotated. I'm not a maintainer; I just use this library and think it works just fine.

ivanviragine commented 9 years ago

At the end... is there a solution?

fabdarice commented 9 years ago

Anyone tried JohnWowUs's fixed? Anything managed to fix that issue?

rodolfopc commented 9 years ago

I was having the same error, but I started to receive the cropped image with "Uri image = Crop.getOutput(intent);" and this worked.

davidesoldan commented 8 years ago

It seem to me that android-crop clears the exif IfD1 information tags (thumbnail information tags). This shouldn't be a problem because we are not working with a thumbnail but only with a part of the original image. Actually trying to open the cropped image with gimp or other tools all works fine. Maybe it could be a problem with android image view? If you try to load the cropped image into the image view with a library like Picasso, the cropped image is displayed correctly!

stevyhacker commented 8 years ago

I have this issue only on samsung phones when taking pictures from the front camera, I added the exif orientation fix it works for the back camera images but not for the ones taken with the front camera. Also images from the gallery work fine, and on other phones it works properly.

gentooise commented 8 years ago

Same issue here, simply solved by performing rotation before starting crop activity:

        // Crop does not return proper exif info
        // Need to rotate before cropping, saving to another temp file
        Uri imageUri = ...;
        Bitmap bm = getBitmapFromUri(imageUri, ...);
        int rotation = getRotation(imageUri, ...);
        bm = rotate(bm, rotation);

        Uri source = saveBitmap(bm, getTempFile(...), ...);
        Uri destination = Uri.fromFile(getTempFile(...));

        Crop.of(source, destination).asSquare().start(...);
leo9223 commented 8 years ago

I am facing the same issue on Samsung Tab E but not on Nexus 5 emulator :( don't know what to do

stevyhacker commented 8 years ago

@leo9223 you can try to have different code for samsung devices and the others, for example if device is from samsung rotate the picture and if it isn't go straight to cropping.

Lawgrin commented 8 years ago

I have forked this and implemented the fix along with added a specify min size Please see here

alicansoysal commented 8 years ago

is there any solution yet?

alikamts commented 8 years ago

Same here +1 solution?

sideprojecteu commented 8 years ago

+1

thameemulansari commented 7 years ago

@jdamcd @Lawgrin any update on the rotation fix ?

https://github.com/jdamcd/android-crop/pull/221

Please advise . Looking for the fix to retain orientation in android?