Example usage for android.graphics Bitmap createScaledBitmap

List of usage examples for android.graphics Bitmap createScaledBitmap

Introduction

In this page you can find the example usage for android.graphics Bitmap createScaledBitmap.

Prototype

public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) 

Source Link

Document

Creates a new bitmap, scaled from an existing bitmap, when possible.

Usage

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap original, int width, int height) {
    return Bitmap.createScaledBitmap(original, width, height, false);
}

From source file:Main.java

/**
 * Draw the favicon with dominant color.
 * @param context Context used to create the intent.
 * @param favicon favicon bitmap./* ww  w. j a  va 2 s.co  m*/
 * @param canvas Canvas that holds the favicon.
 */
private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    int faviconSize = iconBounds.width() / 3;
    Bitmap scaledFavicon = Bitmap.createScaledBitmap(favicon, faviconSize, faviconSize, true);
    canvas.drawBitmap(scaledFavicon, iconBounds.exactCenterX() - scaledFavicon.getWidth() / 2.0f,
            iconBounds.exactCenterY() - scaledFavicon.getHeight() / 2.0f, null);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
    int width = bitmap.getWidth(), height = bitmap.getHeight();
    Bitmap b = Bitmap.createScaledBitmap(
            Bitmap.createScaledBitmap(bitmap, (int) (width / SCALE_RATIO), (int) (height / SCALE_RATIO), false),
            width, height, false);/*from  ww w  . j a v  a2s  .  c  o m*/
    return blurBitmap(b, DEFAULT_BLUR_RADIUS, context);
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int radius) {
    int width = Math.round(sentBitmap.getWidth());
    int height = Math.round(sentBitmap.getHeight());
    sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }//from www  .  j  ava2  s. com

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int a[] = new int[wh];
    int rsum, gsum, bsum, asum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][4];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum, aoutsum;
    int rinsum, ginsum, binsum, ainsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = ainsum = routsum = goutsum = boutsum = aoutsum = rsum = gsum = bsum = asum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            sir[3] = 0xff & (p >> 24);

            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            asum += sir[3] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                ainsum += sir[3];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                aoutsum += sir[3];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];
            a[yi] = dv[asum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            asum -= aoutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            aoutsum -= sir[3];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            sir[3] = 0xff & (p >> 24);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            ainsum += sir[3];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            asum += ainsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            aoutsum += sir[3];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            ainsum -= sir[3];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = ainsum = routsum = goutsum = boutsum = aoutsum = rsum = gsum = bsum = asum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];
            sir[3] = a[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;
            asum += a[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                ainsum += sir[3];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                aoutsum += sir[3];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            pix[yi] = (dv[asum] << 24) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            asum -= aoutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            aoutsum -= sir[3];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];
            sir[3] = a[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            ainsum += sir[3];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            asum += ainsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            aoutsum += sir[3];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            ainsum -= sir[3];

            yi += w;
        }
    }
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);
    return (bitmap);
}

From source file:Main.java

/** Returns a scaled version of the given Bitmap. One of the returned Bitmap's width and height will be equal to size, and the other
 * dimension will be equal or less.//from w w w .  j av  a 2 s .  co m
 */
public static Bitmap createScaledBitmap(Bitmap bitmap, int size) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int scaledWidth = size, scaledHeight = size;
    if (height < width) {
        scaledHeight = (int) (size * 1.0f * height / width);
    } else if (width < height) {
        scaledWidth = (int) (size * 1.0f * width / height);
    }
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, false);
    return scaledBitmap;
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap srcBmp, int iWidth, int iHeight) {
    float fWidth = srcBmp.getWidth();
    float fHeight = srcBmp.getHeight();

    if (fWidth > iWidth) {
        float mWidth = (float) (fWidth / 100);
        float fScale = (float) (iWidth / mWidth);
        fWidth *= (fScale / 100);//ww w . jav  a2  s.c  o m
        fHeight *= (fScale / 100);
    } else if (fHeight > iHeight) {
        float mHeight = (float) (fHeight / 100);
        float fScale = (float) (iHeight / mHeight);
        fWidth *= (fScale / 100);
        fHeight *= (fScale / 100);
    }
    return Bitmap.createScaledBitmap(srcBmp, (int) fWidth, (int) fHeight, true);
}

From source file:Main.java

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 */// w  w  w .j  a v  a  2s  .com
private static Bitmap scaleBitmapDown(@NonNull Bitmap bitmap) {
    final int CALCULATE_BITMAP_MIN_DIMENSION = 100;
    final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());

    if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) {
        // If the bitmap is small enough already, just return it
        return bitmap;
    }

    final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
    return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio),
            Math.round(bitmap.getHeight() * scaleRatio), false);
}

From source file:Main.java

public static Bitmap resize(Bitmap src, int max, boolean isKeep) {
    if (!isKeep)/*from   ww  w. j a  v a2s  .co m*/
        return resizeBitmap(src, max);

    int width = src.getWidth();
    int height = src.getHeight();
    float rate = 0.0f;

    if (width > height) {
        if (max > width) {
            rate = max / (float) width;
            height = (int) (height * rate);
            width = max;
        }
    } else {
        if (max > height) {
            rate = max / (float) height;
            width = (int) (width * rate);
            height = max;
        }
    }

    return Bitmap.createScaledBitmap(src, width, height, true);
}

From source file:Main.java

public static Bitmap resizeImage(Bitmap image, int maxWidth, int maxHeight) {
    Log.d(TAG, "[AirImagePickerUtils] Entering resizeImage: " + String.valueOf(maxWidth) + " x "
            + String.valueOf(maxHeight));
    Bitmap result = image;//  www . j  a v a  2  s.c om
    // make sure that the image has the correct height
    if (image.getWidth() > maxWidth || image.getHeight() > maxHeight && maxWidth != -1 && maxHeight != -1) {
        float reductionFactor = Math.max(Float.valueOf(image.getWidth()) / maxWidth,
                Float.valueOf(image.getHeight()) / maxHeight);

        result = Bitmap.createScaledBitmap(image, (int) (image.getWidth() / reductionFactor),
                (int) (image.getHeight() / reductionFactor), true);
        Log.d(TAG, "[AirImagePickerUtils] resized image to: " + String.valueOf(result.getWidth()) + " x "
                + String.valueOf(result.getHeight()));
    }
    Log.d(TAG, "[AirImagePickerUtils] Exiting resizeImage");
    return result;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);//from  w w w .  j av  a2  s  .co m
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}