Shrink Bitmap To smaller size - Android Graphics

Android examples for Graphics:Bitmap Size

Description

Shrink Bitmap To smaller size

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Main {
    public static Bitmap BitmapToLittle(Bitmap bitMap) {
        //   KB//from  w  w w.  j a v  a  2 s  . co  m
        double maxSize = 40.00;
        //bitmapbitmap  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        //KB
        double mid = b.length / 1024;
        //bitmap   
        if (mid > maxSize) {
            //bitmap 
            double i = mid / maxSize;
            System.out.println("before:" + bitMap.getWidth() + ","
                    + bitMap.getHeight());
            //    1.bitmap
            bitMap = zoomImage(bitMap, bitMap.getWidth() / i,
                    bitMap.getHeight() / i);
            System.out.println("after:" + bitMap.getWidth() + ","
                    + bitMap.getHeight());

        }
        return bitMap;
    }

    public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
            double newHeight) {
        // 
        float width = bgimage.getWidth();
        float height = bgimage.getHeight();
        // matrix
        Matrix matrix = new Matrix();
        // 
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                (int) height, matrix, true);
        return bitmap;
    }
}

Related Tutorials