Example usage for android.graphics Bitmap createBitmap

List of usage examples for android.graphics Bitmap createBitmap

Introduction

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

Prototype

public static Bitmap createBitmap(int width, int height, @NonNull Config config) 

Source Link

Document

Returns a mutable bitmap with the specified width and height.

Usage

From source file:Main.java

public static Bitmap getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) {
    Bitmap bitmap;/* w  w w  .  j a va  2s  .c  o  m*/
    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

public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx) {
    Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            Bitmap.Config.ARGB_8888);/*  w ww  .ja  v a  2  s.c  om*/
    Canvas canvas = new Canvas(roundCornerBitmap);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    int color = 0xff424242;
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return roundCornerBitmap;
}

From source file:Main.java

public static Bitmap getMosaic(Bitmap bmpOriginal) {

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

    int sizeW = width / STEP;
    int sizeH = height / STEP;

    int resultW = sizeW * STEP;
    int resultH = sizeH * STEP;

    Bitmap bmpMosaic = Bitmap.createBitmap(resultW, resultH, Bitmap.Config.RGB_565);

    int col;/*from w w  w . j  a v a 2 s  .c  o  m*/
    for (int i = 0; i < sizeH; i++)
        for (int j = 0; j < sizeW; j++) {
            col = computeAverageColor(j, i, bmpOriginal);
            fillRigeon(col, j, i, bmpMosaic);
        }

    return bmpMosaic;
}

From source file:Main.java

public static Bitmap makeRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);//from w  w w.j  a  v a  2s.  co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap addLabelToBitmap(Bitmap src, String label) {
    float densityFactor = Resources.getSystem().getDisplayMetrics().density;
    final float textPadding = src.getWidth() * 0.05f;

    Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);//from  w  w w  . ja v  a  2 s. c  o m
    textPaint.setTextSize(12 * densityFactor);
    textPaint.setColor(Color.WHITE);
    textPaint.setStrokeWidth(2 * densityFactor);
    textPaint.setShadowLayer(1 * densityFactor, 0, 0, Color.BLACK);

    float textWidth = textPaint.measureText(label);

    float scaleFactor = (src.getWidth() - textPadding * 2) / textWidth;

    canvas.drawBitmap(src, 0, 0, textPaint);

    canvas.save();
    canvas.scale(scaleFactor, scaleFactor);
    float textPosX = (src.getWidth() / scaleFactor - textWidth) / 2;
    float textPosY = (src.getHeight() - textPadding) / scaleFactor;

    canvas.drawText(label, textPosX, textPosY, textPaint);
    canvas.restore();
    return result;
}

From source file:Main.java

public static Bitmap getBitmapFromBuffer(Buffer src, int width) {
    if (null == src) {
        return null;
    }/*w ww .java  2s  .c o  m*/

    int height = getHeightFromWidth(width);
    if (-1 == height) {
        return null;
    }

    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        bitmap.copyPixelsFromBuffer(src);
    } catch (Exception e) {
        e.printStackTrace();
        bitmap = null;
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    float roundPx = w / 2;
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);//ww w  .  j  a  va2  s.c om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

/**
 * Creates a bitmap with the given width and height.
 * <p/>/*from   w w w.j ava 2  s  .  c  om*/
 * If it fails with an OutOfMemory error, it will force a GC and then try to create the bitmap
 * one more time.
 *
 * @param width  width of the bitmap
 * @param height height of the bitmap
 */
public static Bitmap createBitmapAndGcIfNecessary(int width, int height) {
    try {
        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        System.gc();
        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    }
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap out = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(out);
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);//from  w  ww  .  j  av a  2 s.  co m
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect rect = new Rect(0, 0, width, height);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return out;
}

From source file:Main.java

/**
 * Method to get a rounded rectange bitmap
 * @param bitmap//from w ww.  j  ava 2  s. c  o m
 * @param pixels
 * @return
 */
public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
    Bitmap result = null;
    try {
        result = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);

        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, 200, 200);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(75, 75, 75, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

    } catch (NullPointerException e) {
    } catch (OutOfMemoryError o) {
    }
    return result;
}