Android Bitmap Crop resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)

Here you can find the source of resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)

Description

resize Down To Pixels

Declaration

public static Bitmap resizeDownToPixels(Bitmap bitmap,
            int targetPixels, 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 resizeDownToPixels(Bitmap bitmap,
            int targetPixels, boolean recycle) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scale = (float) Math.sqrt((double) targetPixels
                / (width * height));/*from  w w w . j  av  a 2 s.  c  om*/
        if (scale >= 1.0f)
            return bitmap;
        return resizeBitmapByScale(bitmap, scale, recycle);
    }

    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. cutImg(Bitmap bitmap, int newWidth, int newHeight)
  2. cropCenter(Bitmap bitmap, boolean recycle)
  3. cropCenterBitmap(Bitmap bitmap, int newWidth, int newHeight)
  4. resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  5. resizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle)
  6. cropImage(Context context, Uri imageuri, Uri tempUri)
  7. getCroppedBitmap(Bitmap bitmap)