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 toRoundedCornerBitmap(Bitmap bitmap, int radius) {
    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);
    paint.setAntiAlias(true);//from ww w . ja v  a2  s .com
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, radius, radius, 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 toGrey(Bitmap bitmap) {
    Bitmap grey = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    Paint p = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);//  ww w.j  ava 2  s  .  c o m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}

From source file:Main.java

public static Bitmap toRoundCorner(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);/*from w  w w. jav  a  2 s .c om*/
    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 getMatrixBitmap(Bitmap bm, int w, int h, boolean needRecycle) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    boolean isCompress = (width > w && height > h) && (w != 0 && h != 0);
    if (isCompress) {
        float scaleWidth = ((float) w) / width;
        float scaleHeight = ((float) h) / height;
        float scale = Math.max(scaleWidth, scaleHeight);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);//from w  w  w .  j av  a 2  s  . co  m
        Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        if (needRecycle && bm != null && bm != bitmap) {
            bm.recycle();
        }
        return bitmap;
    } else {
        return bm;
    }

}

From source file:Main.java

public static Bitmap scale(Bitmap bm, double widthScale, double heightScale) {
    return Bitmap.createScaledBitmap(bm, (int) (bm.getWidth() * widthScale),
            (int) (bm.getHeight() * heightScale), false);
}

From source file:Main.java

/** simple resizing of the given image to the desired width/height */
public static Bitmap getBitmapFromResource(Context ctx, int id, int x1, int y1) {
    BitmapDrawable bd = (BitmapDrawable) ctx.getResources().getDrawable(id);
    Bitmap b = bd.getBitmap();

    int x = b.getWidth();
    int y = b.getHeight();

    float scaleX = (float) x1 / x;
    float scaleY = (float) y1 / y;
    float scale = 1f;

    boolean scaleXInBounds = (scaleX * x) <= x1 && (scaleX * y) <= y1;
    boolean scaleYInBounds = (scaleY * x) <= x1 && (scaleY * y) <= y1;

    if (scaleXInBounds && scaleYInBounds) {
        scale = (scaleX > scaleY) ? scaleX : scaleY;
    } else if (scaleXInBounds) {
        scale = scaleX;/*from w  w w  .  ja va 2s .  c om*/
    } else if (scaleYInBounds) {
        scale = scaleY;
    }

    return Bitmap.createScaledBitmap(b, (int) (scale * x), (int) (scale * y), true);
}

From source file:Main.java

public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) {
    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    Matrix matrix = new Matrix();

    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true);
    if (resizedBitmap != null) {
        srcBitmap = null;/*from  w  w w .j a v  a2 s. co m*/
        return resizedBitmap;
    } else {
        return srcBitmap;
    }
}

From source file:Main.java

public static Bitmap getRotatedBitmap(byte[] bildDaten) {
    Bitmap result = BitmapFactory.decodeByteArray(bildDaten, 0, bildDaten.length);
    if (result.getWidth() > result.getHeight()) {
        Matrix m = new Matrix();
        m.postRotate(90);/* w w w.j  av  a 2  s .c om*/
        result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), m, true);
    }
    return result;
}

From source file:Main.java

public static Bitmap subtract(Bitmap bitmap1, Bitmap bitmap2) {
    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 = Math.abs(value2 - value1);
            //                resultValue = (resultValue>255/3)?resultValue:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }//from   w ww .j a v  a2  s  .co  m
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static Bitmap getMosaic(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int radius = 10;

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

    int horCount = (int) Math.ceil(width / (float) radius);
    int verCount = (int) Math.ceil(height / (float) radius);

    Paint paint = new Paint();
    paint.setAntiAlias(true);// ww  w  . j a va2  s . c  o  m

    for (int horIndex = 0; horIndex < horCount; ++horIndex) {
        for (int verIndex = 0; verIndex < verCount; ++verIndex) {
            int l = radius * horIndex;
            int t = radius * verIndex;
            int r = l + radius;
            if (r > width) {
                r = width;
            }
            int b = t + radius;
            if (b > height) {
                b = height;
            }
            int color = bitmap.getPixel(l, t);
            Rect rect = new Rect(l, t, r, b);
            paint.setColor(color);
            canvas.drawRect(rect, paint);
        }
    }
    canvas.save();

    return mosaicBitmap;
}