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

Oriented Images Should Rotate Before Loading #9

Closed
titanwalking opened this issue Nov 5, 2019 · 1 comment
Closed

Oriented Images Should Rotate Before Loading #9

titanwalking opened this issue Nov 5, 2019 · 1 comment

Comments

@titanwalking
Copy link

titanwalking commented Nov 5, 2019

After taking a photo from my LG G7 and starting Croppy from the file Uri, CroppyActivity started with the image rotated.

You may consider checking ExifInterface orientation before loading bitmap.

Sample code in Java :

Bitmap rotateBitmap(@NonNull String src, Bitmap bitmap) {
            int orientation;

            try {
                orientation = getExifOrientation(src);
            } catch (IOException e) {
                e.printStackTrace();
                return bitmap;
            }

            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;
                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 null;
            }
        }

private int getExifOrientation(@NonNull String src) throws IOException {
            DebugLog.write();
            ExifInterface exifInterface = new ExifInterface(src);
            return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        }
@iammert
Copy link
Contributor

iammert commented Nov 6, 2019

Done. Use version 0.2

@iammert iammert closed this as completed Nov 6, 2019
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

2 participants