Example usage for android.graphics Bitmap getHeight

List of usage examples for android.graphics Bitmap getHeight

Introduction

In this page you can find the example usage for android.graphics Bitmap getHeight.

Prototype

public final int getHeight() 

Source Link

Document

Returns the bitmap's height

Usage

From source file:Main.java

public static Bitmap cropCenterInside(Bitmap src, Rect rect) {
    int width = Math.min(src.getWidth(), rect.width());
    int height = Math.min(src.getHeight(), rect.height());
    int[] rowData = new int[width];
    int stride = src.getWidth();
    int x = (src.getWidth() - width) / 2;
    int y = (src.getHeight() - height) / 2;

    Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int delta = 0;
    while (delta < height) {
        src.getPixels(rowData, 0, stride, x, y + delta, width, 1);
        dest.setPixels(rowData, 0, stride, 0, delta, width, 1);
        delta++;/*from ww w.  j a v a2s .com*/
    }
    return dest;
}

From source file:Main.java

/**
 * Gets the rounded corner bitmap./*from  www. j a  va2s . co m*/
 *
 * @param bitmap the bitmap
 * @return the rounded corner bitmap
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    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 = 12;

    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, rect, rect, paint);

    return output;
}

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 subtract(Bitmap bitmap1, Bitmap bitmap2) {
    int width = bitmap1.getWidth();
    int height = bitmap1.getHeight();
    Bitmap grayBitmap1 = toGrayScale(bitmap1);
    Bitmap grayBitmap2 = toGrayScale(bitmap2);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
            int value1 = Color.blue(grayBitmap1.getPixel(x, y));
            int value2 = Color.blue(grayBitmap2.getPixel(x, y));
            int resultValue = Math.abs(value2 - value1);
            //                resultValue = (resultValue>255/3)?resultValue:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }/*w  w w  .j av  a 2  s .  c om*/
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static Bitmap getRoundedCircleBitmap(Bitmap scaleBitmapImage) {
    int targetWidth = 150;
    int targetHeight = 150;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);/*from w  ww .jav  a2  s. c  o m*/
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)/*from  w ww  .  java  2 s  .  c o m*/
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    //final Paint paintBorder = new Paint();
    // paintBorder.setColor(Color.GREEN);
    //paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
    //BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(output, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    //paint.setShader(shader);
    paint.setAntiAlias(true);/*from w ww.jav a  2 s  .  c o m*/
    //paintBorder.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);
    //int circleCenter = bitmap.getWidth() / 2;
    //int borderWidth = 2;
    //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
    //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //canvas.drawBitmap(bitmap, circleCenter + borderWidth, circleCenter + borderWidth, paint);
    bitmap.recycle();
    return output;
}

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

/**
 * Converts the supplied byte[] to a Bitmap at 75% compression
 *
 * @param bytes the bitmap's bytes/*from   w w  w  . j  ava  2s .co  m*/
 * @return the Bitmap
 */
public static synchronized Bitmap asBitmap(byte[] bytes) {
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 4, bmp.getHeight() / 4, true);
}

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//  ww w  .j a v  a2 s.c  om
 * 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;
}