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

/**
 * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
 *
 * @param resources The current resources.
 * @param resourceId The resource ID of the drawable to rotate.
 * @param angle The angle of rotation.//from   w  ww  .j av a  2 s .  c o m
 * @return Rotated drawable.
 */
public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId,
        float angle) {

    // Get the original drawable and make a copy which will be rotated.
    Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
    Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform the rotation.
    Canvas tempCanvas = new Canvas(rotated);
    tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2);
    tempCanvas.drawBitmap(original, 0, 0, null);

    return new BitmapDrawable(resources, rotated);
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from   w  w  w.  j av  a  2 s. com
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, h + 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, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
    try {//from ww w .j a  v a  2  s. c om
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformMatrix, true);
    } catch (OutOfMemoryError error) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap src, int ratio) {
    if (src == null)
        return null;

    int width = src.getWidth();
    int height = src.getHeight();

    return Bitmap.createScaledBitmap(src, width * ratio, height * ratio, false);
}

From source file:Main.java

public static Bitmap getCircleBitmap(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;/* ww  w.  j  av a 2  s .com*/
        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 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);
    }/*from   w w w  .  j  a va2  s  . c o 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 Bitmap roundCorners(final Bitmap bitmap, final int radiusX, final int radiusY) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   www .j a  v  a  2s  .  c  o m*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radiusX, radiusY, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    return rounded;
}

From source file:Main.java

@NonNull
public static byte[] bitmapToByteArray(@NonNull Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {// w w  w  .jav a2 s.  com
        out = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        return out.toByteArray();
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception ignore) {
            }
    }
}

From source file:Main.java

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 *///from   w ww. j  a va2  s . c o  m
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

/**
 * For rotating images//from w  ww. j  av  a  2s .  c  om
 * @param bitmap Bitmap to rotate
 * @param degree Degree amount to rotate
 * @return
 */
public static Bitmap rotate(Bitmap bitmap, int degree) {
    if (bitmap == null) {
        return null;
    }
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix mtx = new Matrix();
    mtx.setRotate(degree);

    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}