read Bitmap Auto Size - Android Graphics

Android examples for Graphics:Bitmap Size

Description

read Bitmap Auto Size

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
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 readBitmapAutoSize(String filepath) {
        Bitmap bitmap = null;/*w ww.  j  a v  a2 s  . c  o m*/

        bitmap = readBitmapAutoSize(filepath, 800, 480);

        if (bitmap == null)
            return null;
        int degree = getBitmapDegree(filepath);
        if (degree == 0)
            return bitmap;
        bitmap = rotateBitmapByDegree(bitmap, degree);
        return bitmap;
    }

    public static Bitmap readBitmapAutoSize(String filePath, int outWidth,
            int outHeight) {
        FileInputStream fs = null;
        BufferedInputStream bs = null;
        try {
            fs = new FileInputStream(filePath);
            bs = new BufferedInputStream(fs);
            BitmapFactory.Options options = setBitmapOption(filePath,
                    outWidth, outHeight);
            return BitmapFactory.decodeStream(bs, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bs.close();
                fs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static int getBitmapDegree(String filepath) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(filepath);
            @SuppressWarnings("static-access")
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    exifInterface.ORIENTATION_NORMAL);
            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;
            default:
                break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
        Bitmap retBitmap = null;
        // ??????????????????????
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        try {

            retBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
        // ???????????????????
        if (retBitmap == null) {
            retBitmap = bitmap;
        }
        // ??????
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return retBitmap;
    }

    private static BitmapFactory.Options setBitmapOption(String file,
            int width, int height) {

        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, opt);

        int outWidth = opt.outWidth; 
        int outHeight = opt.outHeight;
        opt.inDither = false;
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inSampleSize = 1;
        if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) {
            int sampleSize = (outWidth / width + outHeight / height) / 2;
            opt.inSampleSize = sampleSize;
        }

        opt.inJustDecodeBounds = false;
        return opt;
    }
}

Related Tutorials