Android Bitmap Crop resizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle)

Here you can find the source of resizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle)

Description

resize Down If Too Big

Declaration

public static Bitmap resizeDownIfTooBig(Bitmap bitmap, int targetSize,
            boolean recycle) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

public class Main {
    public static Bitmap resizeDownIfTooBig(Bitmap bitmap, int targetSize,
            boolean recycle) {
        int srcWidth = bitmap.getWidth();
        int srcHeight = bitmap.getHeight();
        float scale = Math.max((float) targetSize / srcWidth,
                (float) targetSize / srcHeight);
        if (scale > 0.5f)
            return bitmap;
        return resizeBitmapByScale(bitmap, scale, recycle);
    }//from ww w  .j  a v a  2s .  c  om

    public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale,
            boolean recycle) {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);
        if (width == bitmap.getWidth() && height == bitmap.getHeight())
            return bitmap;
        Bitmap target = Bitmap.createBitmap(width, height,
                getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG
                | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }

    private static Bitmap.Config getConfig(Bitmap bitmap) {
        Bitmap.Config config = bitmap.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }
        return config;
    }
}

Related

  1. cutStretchImage(Bitmap image, int xsize, int ysize)
  2. cutImg(Bitmap bitmap, int newWidth, int newHeight)
  3. cropCenter(Bitmap bitmap, boolean recycle)
  4. cropCenterBitmap(Bitmap bitmap, int newWidth, int newHeight)
  5. resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  6. resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)
  7. cropImage(Context context, Uri imageuri, Uri tempUri)
  8. getCroppedBitmap(Bitmap bitmap)