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 getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) {
    Bitmap bitmap;/*from w ww. ja v  a 2 s  .  c om*/
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    if (scaleBitmap) {
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
    }
    return bitmap;
}

From source file:Main.java

/**
 * Since max texture size is 4Kx4K, we have to size it down. Also, no new phones have screens wider than 1440px, but there will be no difference for 1080p width, so we resize to that
 * @param toResize - bitmap to resize//  w ww .ja  v  a2  s  .c  om
 * @return resized bitmap
 */
public static Bitmap resizeForPreview(Bitmap toResize) {
    final int maxSize = 1024;
    int width = toResize.getWidth();
    int height = toResize.getHeight();
    int newWidth = width, newHeight = height;

    if (width > height && width > maxSize) {
        newWidth = maxSize;
        newHeight = (height * maxSize) / width;
    } else if (height > maxSize) {
        newHeight = maxSize;
        newWidth = (width * maxSize) / height;
    }

    return Bitmap.createScaledBitmap(toResize, newWidth, newHeight, false);
}

From source file:Main.java

public static Bitmap scaleBitmap(int idstWidth, int idstHeight, Bitmap srcBitmap, int scaleStyle) {
    if (null == srcBitmap) {
        return null;
    }/*from w  w w  . ja  v a  2 s .co  m*/

    int width = srcBitmap.getWidth();
    int height = srcBitmap.getHeight();

    Bitmap bitmap = null;

    try {
        switch (scaleStyle) {
        case INT_NO_SCLAE: {
            bitmap = srcBitmap;
            break;
        }
        case INT_FULL_SCLAE: {
            bitmap = Bitmap.createScaledBitmap(srcBitmap, idstWidth, idstHeight, true);
            break;
        }
        case INT_UN_FULL_SCLAE: {
            if (width > idstWidth && height > idstHeight) {
                float scale = 0;
                float scaleWidth = (float) width / (float) idstWidth;
                float scaleHeight = (float) height / (float) idstHeight;
                if (scaleWidth > scaleHeight) {
                    scale = scaleWidth;
                } else {
                    scale = scaleHeight;
                }
                int tmpWidth = (int) ((float) width / scale);
                int tmpHigth = (int) ((float) height / scale);
                bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, tmpHigth, true);
            } else if (width <= idstWidth && height > idstHeight) {
                int tmpWidth = (int) ((float) idstHeight * (float) width / (float) height);
                bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, idstHeight, true);
            } else if (width > idstWidth && height <= idstHeight) {
                int tmpHeight = (int) ((float) idstWidth * (float) height / (float) width);
                bitmap = Bitmap.createScaledBitmap(srcBitmap, idstWidth, tmpHeight, true);
            } else {
                float scale = 0;
                float scaleWidth = (float) idstWidth / (float) width;
                float scaleHeight = (float) idstHeight / (float) height;
                if (scaleWidth > scaleHeight) {
                    scale = scaleHeight;
                } else {
                    scale = scaleWidth;
                }
                int tmpWidth = (int) ((float) width * scale);
                int tmpHigth = (int) ((float) height * scale);
                bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, tmpHigth, true);
            }
            break;
        }
        default: {
            break;
        }
        }
    } catch (OutOfMemoryError e) {
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
    return newBitmap;
}

From source file:Main.java

/**
 * Create bitmap image from resource/*from  w w w. j a  v a2s  .c o  m*/
 *
 * @param context
 * @param iconName
 * @param widthDip
 * @param heightDip
 * @return
 */
public static Bitmap resizeMapIcons(Context context, String iconName, int widthDip, int heightDip) {
    int widthPx = (int) dipToPixels(context, widthDip);
    int heightPx = (int) dipToPixels(context, heightDip);

    Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(),
            context.getResources().getIdentifier(iconName, "drawable", context.getPackageName()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, widthPx, heightPx, false);
    return resizedBitmap;
}

From source file:Main.java

public static byte[] ImageToByte(ImageView image) {
    Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
    Bitmap resized = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.PNG, 100, baos);

    return baos.toByteArray();
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap input, int destWidth, int destHeight) {
    int srcWidth = input.getWidth();
    int srcHeight = input.getHeight();
    boolean needsResize = false;
    float p;/*from  w  w  w .j a  v a 2s  . c o m*/
    if (srcWidth > destWidth || srcHeight > destHeight) {
        needsResize = true;
        if (srcWidth > srcHeight && srcWidth > destWidth) {
            p = (float) destWidth / (float) srcWidth;
            destHeight = (int) (srcHeight * p);
        } else {
            p = (float) destHeight / (float) srcHeight;
            destWidth = (int) (srcWidth * p);
        }
    } else {
        destWidth = srcWidth;
        destHeight = srcHeight;
    }
    if (needsResize) {
        Bitmap output = Bitmap.createScaledBitmap(input, destWidth, destHeight, true);
        return output;
    } else {
        return input;
    }
}

From source file:Main.java

/**
 * http://stackoverflow.com/questions/2067955/fast-bitmap-blur-for-android-sdk
 * http://incubator.quasimondo.com/// w  w  w  .  ja  v  a  2s .  co  m
 *
 * @param sentBitmap
 * @param scale
 * @param radius
 * @return
 */
public static Bitmap fastBlur(Bitmap sentBitmap, float scale, int radius) {
    int width = Math.round(sentBitmap.getWidth() * scale);
    int height = Math.round(sentBitmap.getHeight() * scale);
    sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);

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

    if (radius < 1) {
        return null;
    }
    int w = img.getWidth();
    int h = img.getHeight();

    int[] pix = new int[w * h];
    img.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 rsum, gsum, bsum, 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][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 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);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

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

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

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

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

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

            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);

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

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

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

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

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

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 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];

            rbs = r1 - Math.abs(i);

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

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

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

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

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

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

            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];

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

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

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

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

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

            yi += w;
        }
    }

    img.setPixels(pix, 0, w, 0, 0, w, h);

    return img;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w w  w.j  av  a 2  s .  c  o  m*/
    BitmapFactory.decodeResource(context.getResources(), resId, options);

    // Calculate inSampleSize
    float sampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    Log.d("----> Sample Size:", sampleSize + "");
    if (sampleSize < 1) { // return a scaled UP version of image
        Bitmap bg = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bg, (int) (bg.getWidth() / sampleSize),
                (int) (bg.getHeight() / sampleSize), true);
        bg.recycle();
        return scaledBitmap;
    } else { // return a scaled DOWN version of image
        options.inSampleSize = Math.round(sampleSize);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }
}

From source file:Main.java

/**
 * https://futurestud.io/blog/how-to-blur-images-efficiently-with-androids-renderscript
 * https://developer.xamarin.com/recipes/android/other_ux/drawing/blur_an_image_with_renderscript/
 *
 * @param context//from ww  w  . j  a  va  2  s  .c  o  m
 * @param image      original bitmap
 * @param width      output bitmap width
 * @param height     output bitmap height
 * @param blurRadius blur radius ;  blurRadius in section (0,25]
 * @return
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(Context context, Bitmap image, int width, int height, int blurRadius) {

    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(blurRadius);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    rs.destroy();
    return outputBitmap;
}