jdamcd / android-crop

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

Cropped image is rotated sideways while the original image is not. #140

Open MaxHastings opened 9 years ago

MaxHastings commented 9 years ago

The original image is rotated correctly while cropping it. However, the finished cropped image is rotated 90 degrees. I believe this bug only happens when the image is a certain aspect ratio. This seems to be happening on many different Android versions if not all.

rondelrosario commented 9 years ago

I have the same issue. This is very inconsistent as I have rotated it previously to handle it before and in another build I didn't need to.

MaxHastings commented 9 years ago

Yeah I hope it gets fixed... It's a great library other than this bug...

sergii-frost commented 9 years ago

Got same issue on Samsung Galaxy S4 with Android 4.4.2 At same time works perfectly on Nexus 5 with Android 5.1.1 Library version is 1.0.0

sergii-frost commented 9 years ago

Possible fix is provided here https://github.com/jdamcd/android-crop/pull/146

KillerMeng commented 9 years ago

In fact,your original image 's TAG_ORIENTATION are not 0 ,and the android-crop did not rotate to 0 degrees at last.

abist commented 9 years ago

The library actually uses the exif data to rotate, but it doesn't seem to work. What I did to rectify it was taking the data from onActivityResult and rotating it myself. Here's how to do it - http://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image

It fixed the problem.

kohchihao commented 9 years ago

is there any fix??? im in need for this library to work .

huyhu commented 9 years ago

i have the same issue for Samsung Galaxy S3, LG G2 and LG G3

zhenleiji commented 9 years ago

I have the same issue for Sony Xperia Z2. When I worked with image, I had a lot of problem regarding rotation. After extensive research I found the solution for rotations issue. See below:

/**
 * Rotate a bitmap based on orientation metadata.
 * src - image path
 */
public static Bitmap rotateBitmap(String src) {
        Bitmap bitmap = BitmapFactory.decodeFile(src);
        try {
            ExifInterface exif = new ExifInterface(src);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            Matrix matrix = new Matrix();
            switch (orientation) {
                case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                    matrix.setScale(-1, 1);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    matrix.setRotate(180);
                    break;
                case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                    matrix.setRotate(180);
                    matrix.postScale(-1, 1);
                    break;
                case ExifInterface.ORIENTATION_TRANSPOSE:
                    matrix.setRotate(90);
                    matrix.postScale(-1, 1);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    matrix.setRotate(90);
                    break;
                case ExifInterface.ORIENTATION_TRANSVERSE:
                    matrix.setRotate(-90);
                    matrix.postScale(-1, 1);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    matrix.setRotate(-90);
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                case ExifInterface.ORIENTATION_UNDEFINED:
                default:
                    return bitmap;
            }

            try {
                Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                bitmap.recycle();
                return oriented;
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
sydneyagcaoili commented 9 years ago

I want a fix too

MaxHastings commented 9 years ago

@zhenleiji Thanks your code seems to work perfectly :100:

rawGH commented 9 years ago

@sergii-frost Possible fix is provided here #146 >> this works for me (Samsung).

sergii-frost commented 9 years ago

@rawGH thank you, this workaroud works currently.

rafaelekol commented 9 years ago

I have same issue on Samsung S4 with Android 5. When image is taken in portrait mode. I tried to get exif.orientation from original image and I get it correct. But when I sent image to crop and receive it back its get turned CCW 90. It would be cool to pass and receive image original orientation to crop library.

FKSI commented 9 years ago

Any fix? I've got the same issues and none of the fix mentionned above worked for me :(. I don't have any issues with my Sony Xperia Z1.

sandrocsimas commented 9 years ago

Need this fix too =(

Sandrichka22 commented 9 years ago

This happens to all my images also, if they have an orientation different than 0. Non of the above solutions works for me. I liked this library more than any other else, but I can't use it with this bug present.. Someone please share if you have a solution

sergii-frost commented 9 years ago

@sandro-csimas @Sandrichka22 thanks to @rawGH and #146 I simply created my own ImageUtils class which looks like this: https://gist.github.com/sergii-frost/04204f8fb708ee6f60ef

And then just before setting your processed image to your ˆmageView (e.g. previewImage) you can simply do like this:

ImageUtils.normalizeImageForUri(this, uri);
previewImage.setImageURI(uri);

Hope this helps ;)

Sandrichka22 commented 9 years ago

@sergii-frost yes, it did help. Thank you very much. I am going to go with this solution. Thx

amitclickapps commented 9 years ago

This issue is much common in Samsung devices. The images choosen from Gallery or camera are rotated and when sent to Cropper for cropping it rotates. So there is a simple solution for this that i have opted. Before calling the method Crop.of(source, dest).asSquare() use below method to get the real path of the Gallery image or camera image and then send that path as source to the Crop lib method. It works like charm :)

http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework

/**

/**

/**

/**

/**

Sandrichka22 commented 9 years ago

Thank you for your solution as well. I still think that I would prefer @sergii-frost's solution because it works with Uri and not file path. I think it's better if you manipulate Uri. Still, I think that this problem should be solved in the upcoming releases of the library because it's a bug that it's obvious and in my opinion important. As for the devices that the bug is experienced on, I have tested with Samsung and LG devices so far. Thank you for your feedback again.

ncornette commented 9 years ago

Hi, I created a branch with added camera shoot feature in the example app. The purpose is to help fixing the orientation issue especially on Samsung devices...

Feel free to test or merge this branch to speed up development !

https://github.com/ncornette/android-crop/tree/example-shoot