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 handleImagePixelsRelief(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, colorBefore = 0;
    int a, r, g, b;
    int 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 = 1; i < width * height; i++) {
        colorBefore = oldPx[i - 1];//w  w  w  .  j  a v a2 s  . c om
        a = Color.alpha(colorBefore);
        r = Color.red(colorBefore);
        g = Color.green(colorBefore);
        b = Color.blue(colorBefore);

        color = oldPx[i];
        r1 = Color.red(color);
        g1 = Color.green(color);
        b1 = Color.blue(color);

        r = (r - r1 + 127);
        g = (g - g1 + 127);
        b = (b - b1 + 127);
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap getRoundedCircleBitmap(Bitmap scaleBitmapImage) {
    int targetWidth = 150;
    int targetHeight = 150;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);//w w  w .java  2s  .co  m
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap convertBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);//  w w  w  .jav  a  2  s  .co m
    return bitmap;
}

From source file:Main.java

public static Bitmap combineTwoBitmaps(Bitmap background, Bitmap foreground) {
    Bitmap combinedBitmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(),
            background.getConfig());/* w w  w .  j  a  v  a 2  s  .c om*/
    Canvas canvas = new Canvas(combinedBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(background, 0, 0, paint);
    canvas.drawBitmap(foreground, 0, 0, paint);
    return combinedBitmap;
}

From source file:Main.java

public static Bitmap createBitmap(View target) {
    Bitmap b = Bitmap.createBitmap(target.getWidth(), target.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    target.draw(c);/*  w  ww . j  av  a 2 s . c o  m*/

    /* enable this to invert the image: */
    //invertImage(b);

    return b;
}

From source file:Main.java

public static Bitmap convertViewToBitmapWithCanvas(View view, int bitmapWidth, int bitmapHeight) {
    Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    view.draw(new Canvas(bitmap));
    return bitmap;
}

From source file:Main.java

public static Bitmap convertToAlphaMask(Bitmap b) {
    Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(a);
    c.drawBitmap(b, 0.0f, 0.0f, null);//from  w  w w.  j a va 2  s .c  o  m

    return a;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    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);
    final float roundPx = pixels;

    paint.setAntiAlias(true);/*from   ww  w.  j  av  a2 s .  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 byteArrayToBitmap(int width, int height, byte[] byteArray) {
    // use Bitmap.Config.ARGB_8888 instead of type is OK
    Bitmap stitchBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    stitchBmp.copyPixelsFromBuffer(ByteBuffer.wrap(byteArray));
    return stitchBmp;
}

From source file:Main.java

public static Bitmap convertToBitmap(Drawable drawable, int width, int height) {
    Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);

    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);//from  w  ww .  j  a  v a  2s.  co m

    return outputBitmap;
}