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 bmp, float degree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);/*from   w w w.j  a va 2  s  .  c om*/
    return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

From source file:Main.java

private static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*from   ww  w. ja va  2  s .c  o  m*/
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap addPadding(Bitmap bmp, int color) {

    if (bmp == null) {
        return null;
    }//from  w  w w .  ja va  2 s  .  c  o  m

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

    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

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 *//*from ww  w.j a  v a 2 s.  c om*/
private static Bitmap scaleBitmapDown(Bitmap bitmap) {
    final int CALCULATE_BITMAP_MIN_DIMENSION = 100;
    final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());

    if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) {
        // If the bitmap is small enough already, just return it
        return bitmap;
    }

    final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
    return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio),
            Math.round(bitmap.getHeight() * scaleRatio), false);
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap origin, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);//from www .  j  a  v a 2  s.  c  om
    return Bitmap.createBitmap(origin, 0, 0, origin.getWidth(), origin.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from  w ww .j a  v a 2 s  . c o  m*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;/*from  www  . j  a  v  a  2  s. co m*/
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

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

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true);

    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, src, dst, paint);
    return output;
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);/*  w w  w .  j  a v  a 2s  .c o  m*/
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }
}

From source file:Main.java

public static Matrix getRotateMatrix(Bitmap bitmap, int rotation) {
    Matrix matrix = new Matrix();
    if (bitmap != null && rotation != 0) {
        int cx = bitmap.getWidth() / 2;
        int cy = bitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);//  w  w w .  j a va  2  s.  c  o  m
        matrix.postRotate(rotation);
        matrix.postTranslate(cx, cy);
    }
    return matrix;
}

From source file:Main.java

/** simply resizes a given drawable resource to the given width and height */
public static Drawable resizeImage(Context ctx, int resId, int iconWidth, int iconHeight) {

    // load the origial Bitmap
    Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(), resId);

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = iconWidth;
    int newHeight = iconHeight;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);

    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}