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 drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        return bitmapDrawable.getBitmap();
    } else {// w  ww.  ja va 2s . com
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);/*from  www  .ja v a  2 s  .  c o m*/
    int w = src.getWidth();
    int h = src.getHeight();
    int canvasW = Math.round(radius * 2);
    Bitmap bitmap = Bitmap.createBitmap(canvasW, canvasW, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Path path = new Path();
    path.addCircle(radius, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect srcRect = new Rect(0, 0, w, h);
    Rect dstRect = new Rect(0, 0, canvasW, canvasW);

    canvas.drawBitmap(src, srcRect, dstRect, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof NinePatchDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        drawable.draw(canvas);//from ww w  . java  2 s  . co m
        return bitmap;
    } else {
        throw new IllegalArgumentException("can not support this drawable to bitmap now!!!");
    }
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);//from   ww  w . j  a  v  a 2 s.c om
    iconPaint.setFilterBitmap(true);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}

From source file:Main.java

public static byte[] bmpToByteArray2(final Bitmap bmp, final boolean needRecycle) {

    int i;//from  w ww.  j a  v  a  2 s  . c  om
    int j;
    if (bmp.getHeight() > bmp.getWidth()) {
        i = bmp.getWidth();
        j = bmp.getWidth();
    } else {
        i = bmp.getHeight();
        j = bmp.getHeight();
    }

    Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);

    while (true) {
        localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null);
        if (needRecycle)
            bmp.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            //F.out(e);
        }
        i = bmp.getHeight();
        j = bmp.getHeight();
    }
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (null == drawable)
        return null;
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;/*from   ww  w .  j  a  v a  2s.  c om*/
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap getRemoveBitmap(Context context, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w ww  . j  a  v  a  2s.c  om*/
    Bitmap bitmap1;
    try {
        Bitmap bitmap2 = BitmapFactory.decodeStream(context.getAssets().open("remove@2x.png"));
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        canvas1.drawARGB(0, 0, 0, 0);
        canvas1.drawBitmap(bitmap, bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, paint);
        bitmap.recycle();
        canvas1.drawBitmap(bitmap2, 0.0F, 0.0F, paint);
        bitmap2.recycle();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
    }
    return bitmap1;
}

From source file:Main.java

/**
 * convert Bitmap to round corner/*from w  w  w  . java2s.  c om*/
 *
 * @param bitmap
 * @return
 */
public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xff424242);
    //paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap cropJpgFile(String inFl, String outFl) throws IOException {
    Bitmap bmpIn = BitmapFactory.decodeFile(inFl);
    Bitmap bmOverlay = Bitmap.createBitmap(bmpIn.getWidth(), bmpIn.getHeight(), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Canvas c = new Canvas(bmOverlay);
    c.drawBitmap(bmpIn, 0, 0, null);//from ww w. j a  v  a 2  s  . c  om
    //c.drawRect(30, 30, 100, 100, p);
    File fileOut = new File(outFl);
    FileOutputStream out = new FileOutputStream(fileOut);
    bmOverlay = drawTextToBitmap(bmOverlay, "Image Viewer");
    bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, out);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float cornerRadius) {
    if (bitmap == null) {
        return null;
    }//www .java2  s.c  o  m
    Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(result, rect, rect, paint);

    return result;
}