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 crop(Bitmap srcBmp, int side) {

    Bitmap dstBmp;/*from  w  w w . j  ava 2 s  . c  o m*/

    if (srcBmp.getWidth() >= srcBmp.getHeight()) {

        dstBmp = Bitmap.createBitmap(srcBmp, srcBmp.getWidth() / 2 - srcBmp.getHeight() / 2, 0,
                srcBmp.getHeight(), srcBmp.getHeight());

    } else {

        dstBmp = Bitmap.createBitmap(srcBmp, 0, srcBmp.getHeight() / 2 - srcBmp.getWidth() / 2,
                srcBmp.getWidth(), srcBmp.getWidth());
    }

    return Bitmap.createScaledBitmap(dstBmp, side, side, true);
}

From source file:Main.java

@SuppressWarnings("unused")
private static Bitmap getBitmap(InputStream fs) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 1;// w  ww . j  ava  2  s  . com
    Bitmap imgBitmap = BitmapFactory.decodeStream(fs, null, opts);
    if (imgBitmap != null) {
        int width = imgBitmap.getWidth();
        int height = imgBitmap.getHeight();
        imgBitmap = Bitmap.createScaledBitmap(imgBitmap, width, height, true);
    }
    return imgBitmap;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap src, int max) {
    if (src == null)
        return null;

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

    if (width > height) {
        rate = max / (float) width;
        height = (int) (height * rate);
        width = max;//ww w .  j  a v a 2  s . c o  m
    } else {
        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 getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;//from w  w  w  .  j  a  v  a  2s.  co  m

    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;

    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    //final int color = 0xffa19774;
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(Color.parseColor("#BAB399"));

    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap source, int maxWidth, int maxHeight) {
    int outWidth;
    int outHeight;
    int inWidth = source.getWidth();
    int inHeight = source.getHeight();
    if (inWidth > inHeight) {
        outWidth = maxWidth;/*from   w w w . j  a va2 s  .c o  m*/
        outHeight = (inHeight * maxWidth) / inWidth;
    } else {
        outHeight = maxHeight;
        outWidth = (inWidth * maxHeight) / inHeight;
    }

    return Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
}

From source file:Main.java

public static Bitmap trimBitmap(Bitmap bitmap) {
    // Sanity check
    if (bitmap == null)
        return null;

    // Create smaller bitmap if bitmap too big
    int scale = 1;
    Bitmap scaleBitmap = bitmap;/*from   w  w w .j  a  v  a2s  . com*/
    if (bitmap.getWidth() > 500) {
        scale = 8;
        scaleBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / scale, bitmap.getHeight() / scale,
                false);
    }

    // Find bounds
    int[] bounds = findBoundsOfVisibleBitmap(scaleBitmap);

    // Get real bitmap bound
    int left = bounds[0] * scale;
    int top = bounds[1] * scale;
    int right = bounds[2] * scale;
    int bottom = bounds[3] * scale;

    int width = right - left;
    int height = bottom - top;

    // Trim bitmap
    return Bitmap.createBitmap(bitmap, left, top, width, height);
}

From source file:Main.java

public static Bitmap resizePreservingRatio(Bitmap original, int newWidth, int newHeight) {
    if (original == null)
        return null;

    int originalWidth = original.getWidth();
    int originalHeight = original.getHeight();

    if (newWidth / (float) originalWidth < newHeight / (float) originalHeight) {
        newHeight = originalHeight * newWidth / originalWidth;
    } else {/* w  w w . j av a2 s .c  o m*/
        newWidth = originalWidth * newHeight / originalHeight;
    }

    return Bitmap.createScaledBitmap(original, newWidth, newHeight, true);
}

From source file:Main.java

public static Bitmap scaleBitmap(int maxWidth, int maxHeight, Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float scale = 1;

    if (height > width) {
        if (height > maxHeight) {
            scale = (float) maxHeight / (float) height;
        }//from w  w  w. j  a  v  a 2 s. c  o m
    } else {
        if (width > maxWidth) {
            scale = (float) maxWidth / (float) width;
        }
    }

    width = (int) (width * scale);
    height = (int) (height * scale);

    return Bitmap.createScaledBitmap(bitmap, width, height, false);
}

From source file:Main.java

public static Bitmap blurLight(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE_LIGHT);
    int height = Math.round(image.getHeight() * BITMAP_SCALE_LIGHT);

    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_LIGHT);
    theIntrinsic.setInput(tmpIn);//from  w  w w. j a v a  2  s . com
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

From source file:Main.java

public static Bitmap scaleToFillBitmap(Bitmap dst, InputStream is) {
    Bitmap src = BitmapFactory.decodeStream(is);

    float scaled = 1.0f;
    if ((float) dst.getWidth() / (float) src.getWidth() < (float) dst.getHeight() / (float) src.getHeight()) {
        scaled = (float) dst.getHeight() / (float) src.getHeight();
    } else {/*from   w  w w .  j  ava2 s . c  o  m*/
        scaled = (float) dst.getWidth() / (float) src.getWidth();
    }

    Bitmap bmpScaled = Bitmap.createScaledBitmap(src, (int) Math.ceil(src.getWidth() * scaled),
            (int) Math.ceil(src.getHeight() * scaled), true);

    int offsetX = 0;
    int offsetY = 0;
    offsetX = bmpScaled.getWidth() - dst.getWidth() != 0 ? (bmpScaled.getWidth() - dst.getWidth()) / 2 : 0;
    offsetY = bmpScaled.getHeight() - dst.getHeight() != 0 ? (bmpScaled.getHeight() - dst.getHeight()) / 2 : 0;

    return Bitmap.createBitmap(bmpScaled, offsetX, offsetY, dst.getWidth(), dst.getHeight());
}