Reads bitmap from given Uri and fixes orientation of the image so that it always is displayed in a portrait orientation - Android android.graphics

Android examples for android.graphics:Bitmap Load Save

Description

Reads bitmap from given Uri and fixes orientation of the image so that it always is displayed in a portrait orientation

Demo Code


//package com.java2s;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

import android.provider.MediaStore;

public class Main {
    public static Bitmap readBitmapFromUriInPortraitMode(Context context,
            Uri imageURI, boolean reduceSize) {
        Bitmap bm = null;//  w  w  w . java 2  s .  co m
        BitmapFactory.Options options = new BitmapFactory.Options();

        if (reduceSize)
            options.inSampleSize = 5;

        AssetFileDescriptor fileDescriptor = null;

        try {
            fileDescriptor = context.getContentResolver()
                    .openAssetFileDescriptor(imageURI, "r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        finally {
            try {
                bm = BitmapFactory.decodeFileDescriptor(
                        fileDescriptor.getFileDescriptor(), null, options);
                fileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ExifInterface exif = null;

        try {
            if (getRealPathFromUri(context, imageURI) != null)
                exif = new ExifInterface(getRealPathFromUri(context,
                        imageURI));
            else
                exif = new ExifInterface(imageURI.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }

        int exifOrientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        int rotate = 0;

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }
        // Check if image has to be rotated, if so reproduce bitmap and rotate it
        if (rotate != 0) {
            int w = bm.getWidth();
            int h = bm.getHeight();

            //Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bm = Bitmap.createBitmap(bm, 0, 0, w, h, mtx, false);
            bm = bm.copy(Bitmap.Config.ARGB_8888, true);
        }

        return bm;
    }
    public static Bitmap readBitmapFromUriInPortraitMode(Context context,
            Uri imageURI) {
        // Doesn't reduce size of the image by default
        return readBitmapFromUriInPortraitMode(context, imageURI, false);
    }

    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj,
                    null, null, null);

            if (cursor == null)
                return null;

            if (cursor.getColumnIndex(MediaStore.Images.Media.DATA) != -1) {
                int columnIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }

            return null;
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
}

Related Tutorials