resample Bitmap via InputStream - Android Graphics

Android examples for Graphics:Bitmap Stream

Description

resample Bitmap via InputStream

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.InputStream;

public class Main {
    public static Bitmap resample(InputStream stream, int width,
            int height, boolean immutable) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(stream, null, options);
        options.inSampleSize = calculateInSampleSize(options, width, height);
        options.inJustDecodeBounds = false;
        options.inMutable = immutable;//from   w w w.j  av  a  2s. co  m
        return BitmapFactory.decodeStream(stream, null, options);
    }

    private static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width
                    / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio
                    : widthRatio;
        }
        return inSampleSize;
    }
}

Related Tutorials