get Compress Bitmap - Android Graphics

Android examples for Graphics:Bitmap Read

Description

get Compress Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {

    public static Bitmap getCompressBitmap(String picPath, int toWidth,
            int toHeight) {
        if ("".equals(picPath))
            return null;
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;/*from   ww  w.  j  a va 2  s.  c  o m*/
        Bitmap comBitmap = BitmapFactory.decodeFile(picPath, op);
        int srcWidth = op.outWidth;
        int srcHeight = op.outHeight;
        int wRadio = 1, hRadio = 1;

        if (srcWidth < srcHeight) {
            if (srcWidth > toWidth || srcHeight > toHeight) {
                wRadio = (int) Math.ceil(op.outWidth / toWidth);
                hRadio = (int) Math.ceil(op.outHeight / toHeight);
            }

        } else {
            if (srcWidth > toWidth || srcHeight > toHeight) {
                wRadio = (int) Math.ceil(op.outWidth / toHeight);
                hRadio = (int) Math.ceil(op.outHeight / toWidth);
            }

        }

        if (wRadio >= 1 && hRadio >= 1) {
            if (wRadio > hRadio) {
                op.inSampleSize = wRadio;
            } else {
                op.inSampleSize = hRadio;
            }
        }
        op.inJustDecodeBounds = false;
        comBitmap = BitmapFactory.decodeFile(picPath, op);
        return comBitmap;
    }
}

Related Tutorials