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 void drawToCenterOfCanvas(Canvas canvas, Bitmap bp, int max_width, int max_height,
        Rect cacheRect) {//  w w w. ja  v  a2  s . c om
    final int b_w = bp.getWidth();
    final int b_h = bp.getHeight();
    if (b_w <= max_width && b_h <= max_height) { // center aligned
        canvas.drawBitmap(bp, (max_width - b_w) / 2, (max_height - b_h) / 2, null);
    } else { // scaled fix given rect size
        final float s_w = 1.0f * max_width / b_w;
        final float s_h = 1.0f * max_height / b_h;
        final float f_s = Math.min(s_w, s_h);
        final int f_w = (int) (b_w * f_s);
        final int f_h = (int) (b_h * f_s);
        cacheRect.set(0, 0, f_w, f_h);
        cacheRect.offset((max_width - f_w) / 2, (max_height - f_h) / 2);
        canvas.drawBitmap(bp, null, cacheRect, null);
    }
}

From source file:Main.java

/**
 * Scales an image maintaining aspect ratio
 *
 * @param original  original image/*from www.j  a v  a  2  s.co  m*/
 * @param maxWidth  maximum width
 * @param maxHeight maximum height
 * @return a Bitmap representing the thumbnail
 */
public static Bitmap createThumbnailForImage(Bitmap original, int maxWidth, int maxHeight) {
    int width = original.getWidth();
    int height = original.getHeight();

    Log.v("Pictures", "Width and height are " + width + "--" + height);

    if (width > height) {
        // landscape
        float ratio = (float) width / maxWidth;
        width = maxWidth;
        height = (int) (height / ratio);
    } else if (height > width) {
        // portrait
        float ratio = (float) height / maxHeight;
        height = maxHeight;
        width = (int) (width / ratio);
    } else {
        // square
        height = maxHeight;
        width = maxWidth;
    }

    Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);

    return Bitmap.createScaledBitmap(original, width, height, true);
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, int color) {

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

    color = 0xff424242; // FIXME
    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 = convertDipsToPixels(context, ROUND_DIPS);

    paint.setAntiAlias(true);/*from   w  w w. j a v  a  2s  .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

/**
 * //from   w w  w . j av a  2 s .  c o m
 * @return Resource's RGBA byte array
 */
public static byte[] getImageRGBA(Resources res, int resouceId) {
    Bitmap bitmap = BitmapFactory.decodeResource(res, resouceId);
    if (bitmap.getWidth() != 32 || bitmap.getHeight() != 32) {
    }
    return getImageRGBA(bitmap);
}

From source file:Main.java

public static Bitmap decodeInSampleSize(Bitmap bitmap, int reqWidth, int reqHeight) {
    if (bitmap.getWidth() >= reqWidth * 2 && bitmap.getHeight() >= reqHeight * 2) {
        ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayBitmapStream);
        byte[] byteArray = byteArrayBitmapStream.toByteArray();
        bitmap = decodeInSampleSize(byteArray, reqWidth, reqHeight);
    }/*from   w  w w.j  a  v  a 2  s. com*/
    return bitmap;
}

From source file:Main.java

public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
    try {/*from   ww  w  . j a  v  a2  s.c om*/
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        final float roundPx = 14;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap zoom(Bitmap source, float width, float height) {
    Matrix matrix = new Matrix();
    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();
    matrix.postScale(scaleX, scaleY);//from  w w w  . ja v a  2 s.c om

    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    return bitmap;
}

From source file:Main.java

/** Creates and returns a new bitmap which is the same as the provided bitmap
 * but with horizontal or vertical padding (if necessary)
 * either side of the original bitmap//from   www. j a v  a  2  s  .com
 * so that the resulting bitmap is a square.
 * @param bitmap is the bitmap to pad.
 * @return the padded bitmap.*/
public static Bitmap padBitmap(Bitmap bitmap) {
    int paddingX;
    int paddingY;

    if (bitmap.getWidth() == bitmap.getHeight()) {
        paddingX = 0;
        paddingY = 0;
    } else if (bitmap.getWidth() > bitmap.getHeight()) {
        paddingX = 0;
        paddingY = bitmap.getWidth() - bitmap.getHeight();
    } else {
        paddingX = bitmap.getHeight() - bitmap.getWidth();
        paddingY = 0;
    }

    Bitmap paddedBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingX, bitmap.getHeight() + paddingY,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(paddedBitmap);
    canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF); // this represents white color
    canvas.drawBitmap(bitmap, paddingX / 2, paddingY / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return paddedBitmap;
}

From source file:Main.java

public static Bitmap applyFilter(Bitmap bmpOriginal) {
    int width = bmpOriginal.getWidth();
    int height = bmpOriginal.getHeight();

    int core[][] = { { 1, 1, 1 }, { 1, 2, 1 }, { 1, 1, 1 } };

    int[] result = new int[height * width];
    int[][] pixels2D = new int[height][width];
    for (int i = 0; i < height; i++) {
        bmpOriginal.getPixels(pixels2D[i], 0, width, 0, i, width, 1);
    }/*w  w  w. j  a v a2 s.co  m*/

    int count = 0;
    int R, G, B;
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            R = dotProduct(i, j, 0, pixels2D, core) / 10 + 20;
            G = dotProduct(i, j, 1, pixels2D, core) / 10 + 20;
            B = dotProduct(i, j, 2, pixels2D, core) / 10 + 20;

            result[count] = Color.rgb(R, G, B);
            count++;
        }

    Bitmap bmpFiltered = Bitmap.createBitmap(result, 0, width, width, height, Bitmap.Config.RGB_565);
    return bmpFiltered;
}

From source file:Main.java

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

    int[] sum = new int[3];
    for (int YY = 0; YY < width; ++YY) {
        for (int XX = 0; XX < height; ++XX) {
            int bitmapColor = pixels[(YY + XX * width)];
            sum[0] += Color.red(bitmapColor);
            sum[1] += Color.green(bitmapColor);
            sum[2] += Color.blue(bitmapColor);
        }/*w  w  w  .  j a v  a 2  s  . c om*/
    }

    return sum;
}