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) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.RGB_565);//from w w w  . ja  va  2  s .  c o m
    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 addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap convertView2Bitmap(View view) {
    try {/*from w  w  w.  ja  v  a2 s.c  om*/
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getDrawableBitmap(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from   ww w .  j  a  v  a2s.  c o  m*/
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap overlayToDownCenter(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(((float) bmp1.getWidth() - bmp2.getWidth()) / 2, bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap getSingleColorBitmap(int picWidth, int picHeight, int color) {
    Bitmap bm1 = Bitmap.createBitmap(picWidth, picHeight, Bitmap.Config.ARGB_8888);

    int[] pix = new int[picWidth * picHeight];

    for (int y = 0; y < picHeight; y++)
        for (int x = 0; x < picWidth; x++) {
            int index = y * picWidth + x;
            //int r = ((pix[index] >> 16) & 0xff)|0xff;
            //int g = ((pix[index] >> 8) & 0xff)|0xff;
            //int b =( pix[index] & 0xff)|0xff;
            // pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;
            pix[index] = color;/*from   www  . j  a  v  a2s . c om*/

        }
    bm1.setPixels(pix, 0, picWidth, 0, 0, picWidth, picHeight);
    return bm1;
}

From source file:Main.java

public static Bitmap overlayToDownRightCorner(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(bmp1.getWidth() - bmp2.getWidth(), bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap getTransparentPlaceholder(int width, int height) {
    Bitmap.Config conf = Bitmap.Config.ALPHA_8;
    Bitmap bmp = Bitmap.createBitmap(width, height, conf);
    bmp.eraseColor(Color.TRANSPARENT);
    return bmp;//from w w  w .jav  a  2  s  .  c  o  m
}

From source file:Main.java

public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0;
    int r, g, b, a, r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];/*from   ww w .  j  a va 2  s . co m*/
        a = Color.alpha(color);
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);

        r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

        if (r1 > 255) {
            r1 = 255;
        }
        if (g1 > 255) {
            g1 = 255;
        }
        if (b1 > 255) {
            b1 = 255;
        }

        newPx[i] = Color.argb(a, r1, g1, b1);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap convertViewToBitmap(View view) {

    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    view.draw(new Canvas(bitmap));
    return bitmap;
}