Android Bitmap Decode decodeImageFile(String strImagePath, BitmapFactory.Options options, boolean checkOrientation)

Here you can find the source of decodeImageFile(String strImagePath, BitmapFactory.Options options, boolean checkOrientation)

Description

decode Image File

Declaration

public static Bitmap decodeImageFile(String strImagePath,
            BitmapFactory.Options options, boolean checkOrientation) 

Method Source Code

//package com.java2s;

import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.media.ExifInterface;

public class Main {
    public static Bitmap decodeImageFile(String strImagePath,
            BitmapFactory.Options options, boolean checkOrientation) {
        if (checkOrientation == false) {
            return BitmapFactory.decodeFile(strImagePath, options);
        } else {// ww  w .jav a2 s .  co  m
            Bitmap bm = BitmapFactory.decodeFile(strImagePath, options);
            int degree = getExifDegree(strImagePath);
            return getRotatedBitmap(bm, degree);
        }
    }

    public static int getExifDegree(String filepath) {
        int degree = 0;
        ExifInterface exif;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }

        if (exif != null) {
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, -1);

            if (orientation != -1) {
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                }
            }
        }
        return degree;
    }

    public static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
        if (degrees != 0 && bitmap != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) bitmap.getWidth() / 2,
                    (float) bitmap.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0,
                        bitmap.getWidth(), bitmap.getHeight(), m, true);
                if (bitmap.equals(b2)) {
                    bitmap.recycle();
                    bitmap = b2;
                }
            } catch (OutOfMemoryError ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            }
        }
        return bitmap;
    }
}

Related

  1. decodeBitmap(String path, int displayWidth, int displayHeight)
  2. decodeBitmap(String path, int maxImageSize)
  3. decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight)
  4. decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act)
  5. decodeBitmapFromAssets(Context context, String fileName)
  6. decodeSampledBitmapFromByte(byte[] res, int reqWidth, int reqHeight)
  7. decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight)
  8. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)