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

/**
 * Method to remove colors in a Bitmap, creating a gray scale image.
 *
 * @param source The original Bitmap.//from   w  w  w  .j  av a 2 s .c  om
 * @return The gray scale Bitmap.
 */
public static Bitmap toGrayscale(Bitmap source) {
    Bitmap bmpGrayscale = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpGrayscale);
    canvas.drawBitmap(source, 0, 0, getGrayScalePaint());
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap createRoundedBottomBitmap(Context context, int width, int height, int color) {

    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    //color = 0x80424242;  // FIXME

    final Paint paint = new Paint();
    final int roundPxInt = convertDipsToPixels(context, ROUND_DIPS);

    final Rect rect = new Rect(0, 0, width, height - roundPxInt);
    final RectF rectF = new RectF(rect);
    final Rect rectRound = new Rect(roundPxInt, height - roundPxInt, width - roundPxInt, height);
    final RectF rectFRound = new RectF(rectRound);

    paint.setAntiAlias(true);/*from  w  ww.  java2  s .  c  o  m*/
    paint.setColor(color);

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

    // Corners
    Rect oval = new Rect(0, height - 2 * roundPxInt, 2 * roundPxInt, height);
    RectF ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 90.0f, 90.0f, true, paint);

    oval = new Rect(width - 2 * roundPxInt, height - 2 * roundPxInt, width, height);
    ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 0.0f, 90.0f, true, paint);

    // Big and small rectangles
    canvas.drawRect(rectF, paint);
    canvas.drawRect(rectFRound, paint);

    return output;
}

From source file:Main.java

public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) {
    final int w = src.getWidth();
    final int h = src.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(src, 0, 0, null);/*from  w w  w. ja v  a  2  s .  c  o m*/
    if (direction == LEFT_TOP) {
        canvas.drawBitmap(watermark, spacing, spacing, null);
    } else if (direction == LEFT_BOTTOM) {
        canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null);
    } else if (direction == RIGHT_TOP) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null);
    } else if (direction == RIGHT_BOTTOM) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing,
                null);
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap cropCenterInside(Bitmap src, Rect rect) {
    int width = Math.min(src.getWidth(), rect.width());
    int height = Math.min(src.getHeight(), rect.height());
    int[] rowData = new int[width];
    int stride = src.getWidth();
    int x = (src.getWidth() - width) / 2;
    int y = (src.getHeight() - height) / 2;

    Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int delta = 0;
    while (delta < height) {
        src.getPixels(rowData, 0, stride, x, y + delta, width, 1);
        dest.setPixels(rowData, 0, stride, 0, delta, width, 1);
        delta++;/*from  ww w.java2  s.com*/
    }
    return dest;
}

From source file:Main.java

public static Bitmap getRoundedRectBitmap(Bitmap bitmap) {
    Bitmap result = null;/*from w w  w .  j  av a2 s.c  o m*/
    Canvas canvas;
    Paint paint;
    try {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        int color = 0xff424242;
        float radius = width > height ? width / 2 : height / 2;
        paint = new Paint();
        Rect rect = new Rect(0, 0, width, height);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (NullPointerException | OutOfMemoryError e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] bmp2bytesStream(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width % 8 != 0) {
        int adjustWidth = width + (8 - width % 8);
        final Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config);
        Canvas canvas = new Canvas(whiteBgBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);

        bitmap = whiteBgBitmap;/*from  w  w  w  .jav a 2s. c  om*/
        width = bitmap.getWidth();
    }
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    byte[] image = new byte[width * height];
    final byte WHITE = 0, BLACK = 1;
    for (int i = 0; i < pixels.length; i++) {
        image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE;
    }
    final int COL = width + width % 2;
    byte[] row = new byte[COL];
    byte[] res = new byte[COL / 8 * height];
    for (int i = 0, dex = 0, num = 0; i < height; ++i) {
        System.arraycopy(image, i * width, row, 0, width);
        for (byte e : row) {
            res[dex] += e << (7 - num++);
            num = 8 == num ? 0 : num;
            dex = 0 == num ? dex + 1 : dex;
        }
    }
    return res;
}

From source file:Main.java

public static Bitmap blurBitmap(Bitmap bitmap, Context context) {

    //Let's create an empty bitmap with the same size of the bitmap we want to blur  
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    //Instantiate a new Renderscript  
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript  
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps  
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur  
    blurScript.setRadius(25.f);/*from  w w w.j  a v a  2  s .  c  o  m*/

    //Perform the Renderscript  
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap  
    allOut.copyTo(outBitmap);

    //recycle the original bitmap  
    bitmap.recycle();

    //After finishing everything, we destroy the Renderscript.  
    rs.destroy();

    return outBitmap;

}

From source file:Main.java

/**
 * Method to remove color in a Bitmap, creating a gray scale image.
 *
 * @param bmpOriginal The original Bitmap.
 * @return The gray scale Bitmap.//from w w w. ja va 2s. c  om
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    Bitmap bmpGrayscale = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);
    canvas.drawBitmap(bmpOriginal, 0, 0, paint);

    return bmpGrayscale;
}

From source file:Main.java

/**
 * Create a bitmap from a background and a foreground bitmap
 *
 * @param background The bitmap placed in the background
 * @param foreground The bitmap placed in the foreground
 * @return the merged bitmap/*from   ww  w  .ja v  a 2 s  . co  m*/
 */
public static Bitmap mergeBitmap(@NonNull Bitmap background, @NonNull Bitmap foreground) {
    Bitmap result = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(background, 0f, 0f, null);
    canvas.drawBitmap(foreground, 10, 10, null);
    return result;
}

From source file:Main.java

static Bitmap drawableToBitmap(Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(width, height, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);//  ww w .j av  a  2  s  .c  o  m
    return bitmap;
}