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 rotateBitmap(Bitmap bm, int direction) {
    Bitmap returnBm;//from w ww  . j a  va  2  s  .  c om
    int w = bm.getWidth();
    int h = bm.getHeight();

    Matrix matrix = new Matrix();
    if (direction == ROTATE_LEFT) {
        matrix.postRotate(-90);
    } else if (direction == ROTATE_RIGHT) {
        matrix.postRotate(90);
    }

    returnBm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);

    if (bm != returnBm) {
        bm.recycle();
    }

    return returnBm;
}

From source file:Main.java

public static int[] getBitmapPixels(@NonNull Bitmap bitmap, int x, int y, int width, int height) {
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), x, y, width, height);
    final int[] subsetPixels = new int[width * height];
    for (int row = 0; row < height; row++) {
        System.arraycopy(pixels, (row * bitmap.getWidth()), subsetPixels, row * width, width);
    }//w w  w  . j  a  v  a2 s  . c om
    return subsetPixels;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap newbmp;/* ww  w  .j a v a 2 s  .c  o m*/
    try {
        // if (h >= w) {
        // if (height <= h) {
        Matrix matrix = new Matrix();
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleHeight, scaleHeight);
        newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
        // }
        // } else {
        // if (width <= w) {
        // Matrix matrix = new Matrix();
        // float scaleWidth = ((float) width / w);
        // matrix.postScale(scaleWidth, scaleWidth);
        // newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
        // true);
        // return newbmp;
        // }
        // }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap toRoundCorner(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 ww w .j  av  a  2s .  co  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 getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    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   w  w  w  .  j av a2 s .  c  om
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, 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 getCroppedBitmap(Bitmap bmp, int radius) {

    Bitmap sbmp;//from  w  w  w .  ja  v  a2  s . c  om
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    //final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap zoom(Bitmap source, float wf, float hf) {
    Matrix matrix = new Matrix();
    float sx = wf / source.getWidth();
    float sy = hf / source.getHeight();
    matrix.postScale(sx, sy);//from   www. ja  v  a  2s .  c o  m
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int desWidth, int desHeight) {
    return zoomBitmap(bitmap, (float) desWidth / bitmap.getWidth(), (float) desHeight / bitmap.getHeight());
}

From source file:Main.java

public static Drawable scaleImage(Drawable paramDrawable, float paramFloat1, float paramFloat2) {
    BitmapDrawable localBitmapDrawable = null;
    if (paramDrawable != null) {
        Bitmap localBitmap = ((BitmapDrawable) paramDrawable).getBitmap();
        int i = localBitmap.getWidth();
        int j = localBitmap.getHeight();
        Matrix localMatrix = new Matrix();
        localMatrix.postScale(paramFloat1, paramFloat2);
        localBitmapDrawable = new BitmapDrawable(
                Bitmap.createBitmap(localBitmap, 0, 0, i, j, localMatrix, true));
    }/*from www. ja v  a2  s .c  om*/
    return localBitmapDrawable;
}

From source file:Main.java

public static Bitmap rotate(Bitmap source, float degrees) {
    if (source == null || source.getWidth() == 0 || source.getHeight() == 0)
        return null;
    int width = source.getWidth();
    int height = source.getHeight();
    Matrix m = new Matrix();
    float w = width;
    float h = height;
    float px = w / 2;
    float py = h / 2;
    m.setRotate(degrees, px, py);// w  ww  .  j av  a  2  s .c  om
    return Bitmap.createBitmap(source, 0, 0, width, height, m, true);
}