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 getCroppedBitmap(Bitmap bitmap) {
    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());

    paint.setAntiAlias(true);//from  w  w  w . j  a  v  a2  s.c o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    } else {
        // Yatay
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getHeight() / 2, paint);
    }

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

    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        output = Bitmap.createBitmap(output, 0, output.getHeight() / 2 - output.getWidth() / 2,
                output.getWidth(), output.getWidth());
    } else {
        // Yatay
        output = Bitmap.createBitmap(output, output.getWidth() / 2 - output.getHeight() / 2, 0,
                output.getHeight(), output.getHeight());
    }

    return output;
}

From source file:Main.java

public static Bitmap createScreenshot4(View view, int thumbnailWidth, int thumbnailHeight) {
    if (view != null) {
        Bitmap mCapture;/* w w  w.  ja v a 2  s . co m*/
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        Paint transPainter = new Paint();
        transPainter.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        c.drawRect(0, 0, mCapture.getWidth(), mCapture.getHeight(), transPainter);
        try {
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

/**
 * Adds the white border to the specifed bitmap image using specified border size.
 * //w w w.  j  a v a2  s. c o  m
 * @param bmp to add border
 * @param borderSize
 * @return bitmap image with border.
 */
public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize, int imgSize) {

    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

private static Bitmap loadBitmapFromView(View view) {
    int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
    view.measure(specWidth, specWidth);// w w w  .  j  av a 2  s.  c o  m
    int questionWidth = view.getMeasuredWidth();
    int questionHeight = view.getMeasuredHeight();
    Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap toBitmap(View v) {
    int w = v.getWidth();
    int h = v.getHeight();
    if (w <= 0 || h <= 0) {
        return null;
    }/*from w  w  w  . jav  a  2  s.  c  o m*/
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap rotateDrawable(Context context, @DrawableRes int resId, int angle) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), resId);
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);// ww  w  . j  a v  a2  s.c o  m
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(angle - 90, bmpOriginal.getWidth() / 2, bmpOriginal.getHeight() / 2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmResult;
}

From source file:Main.java

public static final Bitmap grey(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap greyBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(greyBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);/*from   www  . java  2 s  . co m*/
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return greyBitmap;
}

From source file:Main.java

public static Bitmap transferMode(Bitmap src, Bitmap mask, Xfermode xfermode) {
    final int width = mask.getWidth();
    final int height = mask.getHeight();
    final Bitmap dst = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(dst);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);//from   w  w  w.j  a va2s  .  c o m

    final int srcWidth = src.getWidth();
    final int srcHeight = src.getHeight();

    canvas.save();

    // Scale down the image first if required.
    if ((width < srcWidth) || (height < srcHeight)) {
        float radioX = (float) width / srcWidth;
        float radioY = (float) height / srcHeight;
        float radio = 1f;
        float dx = 0f;
        float dy = 0f;

        if (radioX > radioY) {
            radio = radioX;
            dy = (height / radioX - srcHeight) / 2.0f;
        } else {
            radio = radioY;
            dx = (width / radioX - srcWidth) / 2.0f;
        }

        canvas.scale(radio, radio);
        canvas.translate(dx, dy);
    }
    canvas.drawBitmap(src, 0, 0, paint);
    canvas.restore();

    if (xfermode != null) {
        paint.setXfermode(xfermode);
        canvas.drawBitmap(mask, 0, 0, paint);
    }

    return dst;
}

From source file:Main.java

/**
 * Writes a simple bitmap to local storage. The image is a solid color with size 400x400
 *///from  w  w w .j  av  a  2s  .c  o m
private static void writeSolidColorBitmapToFile(File file, int color) throws IOException {
    if (!file.exists()) {
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(color);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int width, int height, float roundPx) {

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

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*from www  .ja  va 2 s  .com*/
    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;
}