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 toGreyBitmap(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  a va2  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 convertToBlackWhite(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];

    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }//from  w  w  w . j av  a2s  .c  o m
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
}

From source file:Main.java

public static Bitmap changeBitmapColor(Bitmap bmp, int color) {
    Bitmap b = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    Canvas canvas = new Canvas(b);
    Paint paint = new Paint();
    paint.setColor(color);//from w  ww.  ja  v a2  s .  c om
    paint.setAntiAlias(true);
    canvas.drawBitmap(bmp, bmp.getWidth(), bmp.getHeight(), paint);
    return b;
}

From source file:Main.java

public static Bitmap resizeImageByWidth(Bitmap defaultBitmap, int targetWidth) {
    int rawWidth = defaultBitmap.getWidth();
    int rawHeight = defaultBitmap.getHeight();
    float targetHeight = targetWidth * (float) rawHeight / (float) rawWidth;
    float scaleWidth = targetWidth / (float) rawWidth;
    float scaleHeight = targetHeight / (float) rawHeight;
    Matrix localMatrix = new Matrix();
    localMatrix.postScale(scaleHeight, scaleWidth);
    return Bitmap.createBitmap(defaultBitmap, 0, 0, rawWidth, rawHeight, localMatrix, true);
}

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;/*from   w w  w.  jav a  2s . c o  m*/
        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

public static Rect buildRect(Bitmap bitmap, int left, int top) {
    return new Rect(left, top, left + bitmap.getWidth(), top + bitmap.getHeight());
}

From source file:Main.java

/**
 * Utility function to resize bitmap to a square
 * /*  w ww  . j  a  v a 2  s.co m*/
 * @param bitmap the bitmap to crop
 * @return the cropped bitmap
 */
public static Bitmap cropBitMapToSquare(Bitmap bitmap) {
    int shortSideLen = Math.min(bitmap.getHeight(), bitmap.getWidth());
    return Bitmap.createBitmap(bitmap, 0, 0, shortSideLen, shortSideLen);
}

From source file:Main.java

public static Bitmap getCircularBitmapImage(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();//from   ww  w  . j ava  2  s  .  com
    }
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}

From source file:Main.java

public static void loadBitmapFitWindow(String path, int nWidth) {
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    int width = bitmap.getWidth();

    if (width < nWidth) {

    }//from  w w w  .ja  v a2 s. c  om

}

From source file:Main.java

public static Bitmap applyShadingFilter(Bitmap source, int shadingColor) {
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            index = y * width + x;/*from www  .ja  v a  2s  .com*/
            pixels[index] &= shadingColor;
        }
    }
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
}