Sophie
Sophie

Reputation: 2634

Auto Image Rotated from Portrait to Landscape

I am taking photo and storing it into SD Card , and later viewing it from SD Card into ImageView, but getting as rotated ...

I am capturing it in Portrait mode , but getting resultant image in Landscape mode...

Is there something which I am missing ?

ExifUtil.java class found here

/**
 * Displaying captured image/video on the screen
 * */
private void previewMedia(boolean isImage) {
    // Checking whether captured media is image or video
    if (isImage) {
        imgPreview.setVisibility(View.VISIBLE);

        final Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        Bitmap orientedBitmap = ExifUtil.rotateBitmap(filePath, bitmap);

        imgPreview.setImageBitmap(orientedBitmap);
    } else {
        imgPreview.setVisibility(View.GONE);
    }
}

but still showing rotated image in a ImageView ...

Upvotes: 4

Views: 3788

Answers (2)

Atul O Holic
Atul O Holic

Reputation: 6792

If the image(photo) were taken by a program made by you, you must set Parameters.setRotation with the correct rotation value.

This, depending of camera drive, rotates the image before save or save the rotation value to exif TAG_ORIENTATION.

Therefore, if TAG_ORIENTATION is null or zero, the image are in the correct orientation, otherwise you must rotate image according the value in TAG_ORIENTATION.

CODE

Get orientation from EXIF:

ExifInterface exif = null;
try {
    exif = new ExifInterface(path);
} catch (IOException e) {
    e.printStackTrace();
}  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED);

Get bitmap rotated:

Bitmap bmRotated = rotateBitmap(bitmap, orientation); 

Method to rotate bitmap:

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        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 bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return bmRotated;
    }
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

Source - https://stackoverflow.com/a/20480741/3036759

Upvotes: 4

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

You need to use EXIF with ORIENTATION_UNDEFINED to get right orientation.

ExifInterface exif = null;
try {
    exif = new ExifInterface(path);
} catch (IOException e) {
    e.printStackTrace();
}  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED);

And rotate the Bitmap :

Bitmap bmRotated = rotateBitmap(bitmap, orientation);  

Reference link

Upvotes: 1

Related Questions