Example usage for android.graphics Bitmap getWidth

List of usage examples for android.graphics Bitmap getWidth

Introduction

In this page you can find the example usage for android.graphics Bitmap getWidth.

Prototype

public final int getWidth() 

Source Link

Document

Returns the bitmap's width

Usage

From source file:Main.java

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

    final int color = Color.WHITE;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*ww w .j  a  va  2  s.  c om*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, float w, float h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleW = ((float) w / width);
    float scaleH = ((float) h / height);
    matrix.postScale(scaleW, scaleH);//from   w  w  w .j  ava 2 s  .c o m
    Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    return newBmp;
}

From source file:Main.java

public static Bitmap getGrayscale(@NonNull Bitmap src) {

    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());

    Canvas canvas = new Canvas(dest);
    Paint paint = new Paint();
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(MATRIX);
    paint.setColorFilter(filter);//  ww w.ja  v a  2 s  .  c  o m
    canvas.drawBitmap(src, 0, 0, paint);

    return dest;
}

From source file:Main.java

public static Bitmap joinBitmaps(Bitmap first, Bitmap second) {
    Bitmap result = Bitmap.createBitmap(first.getWidth(), first.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint bitmapPaint = new Paint();
    if (second.getWidth() != first.getWidth() && second.getHeight() != first.getHeight()) {
        second = createScaledToFillBitmap(second, first.getWidth(), first.getHeight());
    }//from  www.  j a  v a  2s.c o m

    canvas.drawBitmap(first, 0, 0, bitmapPaint);
    canvas.drawBitmap(second, 0, 0, bitmapPaint);

    return result;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(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 w  ww  .j  a v  a  2  s. c  o 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 roundCorners(final Bitmap source, final float radius) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);// w ww. j av a2  s .  c o  m
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, 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 Drawable scaleImage(Drawable drawable, float scaleWidth, float scaleHeight) {
    Drawable resizedDrawable = null;//from w  ww  . j  ava 2  s  .  com
    if (drawable != null) {
        Bitmap bitmapOrg = ((BitmapDrawable) drawable).getBitmap();
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();

        Matrix matrix = new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

        resizedDrawable = new BitmapDrawable(resizedBitmap);
    }
    return resizedDrawable;
}

From source file:Main.java

public static Bitmap flipBitmap(Bitmap bm) {
    Matrix matrix = new Matrix();
    matrix.postRotate(90);//w  w w . j  a  va2  s  .  c o  m
    int width = bm.getWidth();
    int height = bm.getHeight();
    bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    return bm;
}

From source file:Main.java

public static Bitmap getBitmap(int background, Bitmap contour, float alpha) {
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(background);/*from  ww  w.  j  a  v  a  2 s  . c  o  m*/
    return getBitmap(bitmap, contour, alpha);
}

From source file:Main.java

public static Bitmap createCroppedScaleBitmap(Bitmap src, int reqWidth, int reqHeight) {
    final int bWidth = src.getWidth();
    final int bHeight = src.getHeight();
    Matrix matrix = new Matrix();
    int maxSize = Math.max(reqHeight, reqWidth);
    float scaleX;
    if (bWidth * bHeight < reqWidth * reqHeight)
        scaleX = 0;/*  w  w  w.j ava 2  s. c om*/
    else {
        if (bWidth > bHeight) {
            scaleX = (float) maxSize / bWidth;
        } else
            scaleX = (float) maxSize / bHeight;
    }
    Bitmap sourceBitmap;
    if (scaleX > 0 && scaleX != 1) {
        matrix.setScale(scaleX, scaleX);
        sourceBitmap = Bitmap.createBitmap(src, 0, 0, bWidth, bHeight, matrix, true);
        if (sourceBitmap != src && !src.isRecycled())
            src.recycle();
    } else
        sourceBitmap = src;
    return sourceBitmap;
}