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 getRoundedCornerBitmap(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());
    final RectF rectF = new RectF(rect);
    final float roundPx = bitmap.getWidth() / 2;

    paint.setAntiAlias(true);/*from w  w  w.  j a  v  a2s .  c  om*/
    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 doColorFilter(Bitmap src, double red, double green, double blue) {
    int w = src.getWidth();
    int h = src.getHeight();

    Bitmap bmOut = Bitmap.createBitmap(w, h, src.getConfig());
    int A, R, G, B, pixel;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);//from ww w  .ja  v a  2s  .c  o m
            A = Color.alpha(pixel);
            R = (int) (Color.red(pixel) * red);
            G = (int) (Color.green(pixel) * green);
            B = (int) (Color.blue(pixel) * blue);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidht = ((float) w) / width;
    float scaleHeight = ((float) h) / height;
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}

From source file:Main.java

public static Bitmap compressBitmap(Bitmap bitmap, int width, int height, boolean isAdjust) {
    if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
        float scaleX = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN)
                .floatValue();/*from ww  w.  j av a 2s  .  c om*/
        float scaleY = new BigDecimal(height)
                .divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue();
        if (isAdjust) {
            scaleX = (scaleX < scaleY ? scaleX : scaleY);
            scaleY = scaleX;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleX, scaleY);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap;
}

From source file:Main.java

/**
 * Return histogram of grayscaled image//from ww w.j  a  v  a2 s.  c  o m
 */
public static int[] getHistogram(Bitmap bmp) {
    int[] histogram = new int[256];
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        int c = Color.red(pixels[i]);
        histogram[c] += 1;
    }

    return histogram;
}

From source file:Main.java

public static Bitmap handleColorMatrix(Bitmap bm, float[] matrixs) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);//from www  . j a v  a  2 s. c  om
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);//from   ww  w.ja  v a2s . 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 getScreenFitBitmap(Bitmap src, int dispWidth, int dispHeight) {
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();

    float scale = (float) dispWidth / srcWidth;

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);/*from   w w w  . j ava 2s. co m*/

    return Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix, true);
}

From source file:Main.java

public static Bitmap clearBitmap(Bitmap sourceBitmap) {
    Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),
            sourceBitmap.getHeight());/*from  w w w. j av a  2s.c om*/
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    mutableBitmap.eraseColor(Color.TRANSPARENT);
    return mutableBitmap;
}

From source file:Main.java

/**
 * Fills the bitmap's pixels of the specified Color to transparency.
 * //  w  w w . j a va 2s. co  m
 * @param aBitmap bitmap to process
 * @param aColor color to fill
 * @return bmp
 */
@SuppressLint("NewApi")
public static Bitmap eraseBG(Bitmap aBitmap, int aColor) {
    int width = aBitmap.getWidth();
    int height = aBitmap.getHeight();
    Bitmap b = aBitmap.copy(Config.ARGB_8888, true);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
        b.setHasAlpha(true);
    }

    int[] pixels = new int[width * height];
    aBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        if (pixels[i] == aColor) {
            pixels[i] = 0;
        }
    }

    b.setPixels(pixels, 0, width, 0, 0, width, height);

    return b;
}