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

/**
 * create a circle from cutout from a bitmap.
 * does not alter sizes./* w  ww .j av  a2  s. c o m*/
 *
 * @param bitmap the bitmap
 * @see #cutCircleFromBitmap(String, int)
 * @return a bitmap circle cutout
 */
public static Bitmap roundBitMap(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setShader(shader);

    Canvas c = new Canvas(circleBitmap);

    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    int w = 1536, h = 1280;
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
    Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
    android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig();
    Canvas canvas = new Canvas(bmp);

    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(frontColor);/*from  ww w .  j  a v a  2  s  .c  o  m*/
    // text size in pixels
    paint.setTextSize((int) (400 * scale));
    // text shadow
    //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    if (backColor != -1) {
        canvas.drawColor(backColor);
    }
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bmp.getWidth() - bounds.width()) / 2;
    int y = (bmp.getHeight() + bounds.height()) / 2;
    canvas.drawText(gText, x, y, paint);
    return bmp;
}

From source file:Main.java

public static Bitmap multiply(Bitmap bitmap1, Bitmap bitmap2) {
    double alpha = 0.1;
    int width = bitmap1.getWidth();
    int height = bitmap1.getHeight();
    Bitmap grayBitmap1 = toGrayScale(bitmap1);
    Bitmap grayBitmap2 = toGrayScale(bitmap2);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
            int value1 = Color.blue(grayBitmap1.getPixel(x, y));
            int value2 = Color.blue(grayBitmap2.getPixel(x, y));
            int resultValue = (int) (Math.abs((value2 / 255.0) * (value1 / 255.0)) * 255);
            //                resultValue = (resultValue>255/2)?255:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }//  w  ww.  j  a  v a 2 s  . co  m
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);//from w w  w  . j a v a  2  s .  c  o  m
    canvas.drawRect(outerRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap circleBitmap(final Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w w  w . jav a 2  s.c  om*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    final float radius = width > height ? height / 2 : width / 2;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

public static final Bitmap alpha(Bitmap bitmap, int alpha) {
    float[] matrixItems = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, alpha / 255f, 0,
            0, 0, 0, 0, 1 };//from ww  w .  j  a v  a2  s .  c om
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap alphaBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(alphaBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return alphaBitmap;
}

From source file:Main.java

public static Bitmap cutMatrixBitmap(Bitmap src, float scale) {

    int srcW = src.getWidth();
    int srcH = src.getHeight();
    int matrixW = srcW;
    int matrixH = srcH;

    matrixW = (int) (matrixH * scale);
    if (matrixW > srcW) {
        matrixW = srcW;/* w w w  . ja v  a  2s .  c  om*/
        matrixH = (int) (srcW / scale);
    }
    int left = (srcW - matrixW) / 2;
    int top = (srcH - matrixH) / 2;

    return src == null ? Bitmap.createBitmap(matrixW, matrixH, Config.ARGB_8888)
            : Bitmap.createBitmap(src, left, top, matrixW, matrixH);
}

From source file:Main.java

/**
 * Gets the bitmap with rounded corners/*from w ww.  j  a  va  2s  .  c o  m*/
 *
 * @param bitmap Bitmap for processing
 * @param pixels Picture diameter in pixels
 * @return Rounded picture
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    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

public static Bitmap drawable2Bitmap(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.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);//from  www  .  j a v a 2 s  . c  o  m
        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Create rounded corner bitmap.//from   w  ww.  ja  va  2  s  . c  o  m
 *
 * @param bitmap bitmap.
 * @return rounded bitmap.
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap source = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), 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, source.getWidth(), source.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = source.getWidth();

    paint.setAntiAlias(true);
    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(source, rect, rect, paint);

    return output;
}