jdamcd / android-crop

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

Image Rotation #258

Open prowsandy opened 7 years ago

prowsandy commented 7 years ago

Is there any way to make the image to be cropped should be rotate first?

Thanks

SourceCipher commented 7 years ago

Agree. If the image is in portrait mode, it will be rotated. I am trying to solve this problem as the same happens when I simply add uri into imageview

power7714 commented 6 years ago

@prowsandy @gwidaz Using android.media.ExifInterface you can fix the image rotation via button click. You could essentially modify the com.soundcloud.android.crop.CropImageActivity to include a "Fix Rotation" button. But this is how you would do it. This is what I use in one of my in-house apps.

public void fixImageRotation(){
    imageview.buildDrawingCache();
    Bitmap original = imageview.getDrawingCache();
    //fileUri is the uri of the selected image
    imageview.setImageBitmap(imageOreintationValidator(original, fileUri.getPath()));
}

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) {

    ExifInterface ei;
    try {
        ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                bitmap = rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                bitmap = rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                bitmap = rotateImage(bitmap, 270);
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

private Bitmap rotateImage(Bitmap source, float angle) {

    Bitmap bitmap = null;
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return bitmap;
}

Works instantly when the button is clicked and flawlessly.

Wiktorl4z commented 5 years ago

Solution written by @power7714 is working only when you want to rotate your image after editing.. Basically this solution not show how to rotate image to display it correctly in crop-edit mode.. Lib is deprecated if you are looking for nice lib to use I personally recommend this one: https://github.com/Yalantis/uCrop Best