Bitmap To Small - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

Bitmap To Small

Demo Code


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

public class Main {
    public static Bitmap BitmapToSmall(Bitmap bitMap) {
        //   KB/*from w ww .  j av a  2  s .c  om*/
        double maxSize = 200.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;
            //    1.bitmap
            bitMap = zoomImage(bitMap, bitMap.getWidth() / Math.sqrt(i),
                    bitMap.getHeight() / Math.sqrt(i));
        }
        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