Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image from gallery rotates automatically - Android

In my android application i am loading image from device gallery.In that, i am facing issue regarding image orientation. When i load large resolution images from gallery, they are automatically rotated then display in my view. I tried various solution regarding this problem but couldn't get proper solution. I referred getOrientation() , and this links. I have tried both solutions but couldn't got desired result.The ExifInterface return proper data but then also they are not helpful as images are rotated because of their large resolution not because of camera orientation. Please help me to solve this solution.

Thank you.

like image 621
Zankhna Avatar asked Dec 17 '13 12:12

Zankhna


People also ask

Why are my photos rotating?

Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.

How do you auto-rotate photos in gallery?

Open your device's Settings app. . Select Accessibility. Select Auto-rotate screen.

What is Auto photo Orientation?

This means that most cameras store images' pixels exactly the same whether the camera is oriented in landscape or portrait mode. They just flip a bit to signal to the viewer whether to display the pixels as-is or to rotate them by 90 or 180 degrees when displaying the image.


1 Answers

This works well;

public class ExifUtils {

public static Bitmap getRotatedBitmap(Context context, Uri uri) {
    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        float width = (float) bitmap.getWidth();
        float height = (float) bitmap.getHeight();
        float max = Math.max(width / 1280.0f, height / 1280.0f);
        if (max > 1.0f) {
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) (width / max), (int) (height / max), false);
        }
        Bitmap rotateBitmap = rotateBitmap(bitmap,
                new ExifInterface(context.getContentResolver().openInputStream(uri))
                        .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1));
        if (rotateBitmap != bitmap) {
            bitmap.recycle();
        }
        return rotateBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private static Bitmap rotateBitmap(Bitmap bitmap, int i) {
    Matrix matrix = new Matrix();
    switch (i) {
        case 2:
            matrix.setScale(-1.0f, 1.0f);
            break;
        case 3:
            matrix.setRotate(180.0f);
            break;
        case 4:
            matrix.setRotate(180.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case 5:
            matrix.setRotate(90.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case 6:
            matrix.setRotate(90.0f);
            break;
        case 7:
            matrix.setRotate(-90.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case 8:
            matrix.setRotate(-90.0f);
            break;
        default:
            return bitmap;
    }
    try {
        Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        bitmap.recycle();
        return createBitmap;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
  }
}
like image 118
Ahmet B. Avatar answered Oct 19 '22 07:10

Ahmet B.