Android Bitmap Resize resizeBitmap(Context context, int resId, int w, int h)

Here you can find the source of resizeBitmap(Context context, int resId, int w, int h)

Description

resize Bitmap

Declaration

public static final Bitmap resizeBitmap(Context context, int resId,
            int w, int h) 

Method Source Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.util.Base64;
import android.view.View;

public class Main{
    public static final Bitmap resizeBitmap(Context context, int resId,
            int w, int h) {
        Bitmap source = decodeFromResource(context.getResources(), resId);
        return ThumbnailUtils.extractThumbnail(source, w, h);
    }/*w  w w. j  a va  2 s  . co  m*/
    public static final Bitmap decodeFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = inSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    public static final Bitmap decodeFromResource(Resources res, int resId) {
        return BitmapFactory.decodeResource(res, resId);
    }
    public static final int inSampleSize(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) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }

            final float totalPixels = width * height;
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
}

Related

  1. manageBitmapRotatio(int photoW, int photoH, Bitmap bitMap, int rotation)
  2. readBitmapAutoSize(String filePath, int outWidth, int outHeight)
  3. resize(Bitmap bitmap, int newWidth, int newHeight)
  4. resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  5. resizeAndCropCenter(final Bitmap bitmap, final int size)
  6. resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle)
  7. resizeDownBySideLength(Bitmap bitmap, int maxLength, boolean recycle)
  8. resizeDrawable(Context context, int resId, int w, int h)
  9. resizePreviewBitmap(Bitmap bm, int width, int height)