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 resize(Bitmap src, int w2, int h2) {
    int w1 = src.getWidth();
    int h1 = src.getHeight();
    int[] pxSource = new int[w1 * h1];
    int[] pxResult = new int[w2 * h2];

    src.getPixels(pxSource, 0, w1, 0, 0, w1, h1);
    double x_ratio = w1 / (double) w2;
    double y_ratio = h1 / (double) h2;
    double px, py;
    for (int i = 0; i < h2; i++) {
        for (int j = 0; j < w2; j++) {
            px = Math.floor(j * x_ratio);
            py = Math.floor(i * y_ratio);
            pxResult[(i * w2) + j] = pxSource[(int) ((py * w1) + px)];
        }/*from   w  w  w. j ava 2s.  c o m*/
    }

    return Bitmap.createBitmap(pxResult, w2, h2, Config.ARGB_8888);
}

From source file:Main.java

public static Bitmap blackAndWhited(Bitmap in) {
    int width = in.getWidth();
    int height = in.getHeight();

    int[] pixels = new int[width * height];
    in.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  ww .  jav a2s  .  co m
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBmp;
}

From source file:Main.java

public static Bitmap zoom(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 resize(Bitmap bmp, int width, int height) {
    float sx = (width * 1.0f) / bmp.getWidth();
    float sy = (height * 1.0f) / bmp.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(sx, sy);//from  w ww. j  a v a2  s  . co m
    return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap getSkewBitmap(Bitmap mBitmap, float xRatio, float yRatio) {
    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postSkew(xRatio, yRatio);//from w  w  w .  j  a va  2  s  . c o m
    Bitmap mScrewBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);

    return mScrewBitmap;
}

From source file:Main.java

public static Bitmap getResizedBitmap(Bitmap bm, float newWidth, float newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = newWidth / width;
    float scaleHeight = newHeight / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}

From source file:Main.java

public static Bitmap addPadding(@NonNull Bitmap bmp) {

    int biggerParam = Math.max(bmp.getWidth(), bmp.getHeight());
    Bitmap bitmap = Bitmap.createBitmap(biggerParam, biggerParam, bmp.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);

    int top = bmp.getHeight() > bmp.getWidth() ? 0 : (bmp.getWidth() - bmp.getHeight()) / 2;
    int left = bmp.getWidth() > bmp.getHeight() ? 0 : (bmp.getHeight() - bmp.getWidth()) / 2;

    canvas.drawBitmap(bmp, left, top, null);
    return bitmap;
}

From source file:Main.java

public static Bitmap getScaled(Bitmap bm, int sidePx, boolean max) {
    float w = bm.getWidth();
    float h = bm.getHeight();
    float wRatio = sidePx / w;
    float hRatio = sidePx / h;
    float ratio = max ? Math.min(wRatio, hRatio) : Math.max(wRatio, hRatio);
    w = ratio * w;//from  ww w.j  a  v  a  2  s . c  o m
    h = ratio * h;
    return Bitmap.createScaledBitmap(bm, (int) w, (int) h, true);
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap source, int targetWidth, int targetHeight) {
    int marginLeft = (source.getWidth() - targetWidth) / 2;
    int marginTop = (source.getHeight() - targetHeight) / 2;
    Bitmap newBitmap = Bitmap.createBitmap(source, marginLeft, marginTop, targetWidth, targetHeight);
    return newBitmap;
}

From source file:Main.java

public static Bitmap scale(Bitmap org, float xRate, float yRate) {

    final int orgWidth = org.getWidth();
    final int orgHeight = org.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(xRate, yRate);/*from w  w  w  . j  a  v  a  2 s  .  c om*/

    return Bitmap.createBitmap(org, 0, 0, orgWidth, orgHeight, matrix, true);
}