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

/** simple resizing of the given image to the desired width/height */
public static Bitmap getBitmapFromResource(Context ctx, int id, int x1, int y1) {
    BitmapDrawable bd = (BitmapDrawable) ctx.getResources().getDrawable(id);
    Bitmap b = bd.getBitmap();

    int x = b.getWidth();
    int y = b.getHeight();

    float scaleX = (float) x1 / x;
    float scaleY = (float) y1 / y;
    float scale = 1f;

    boolean scaleXInBounds = (scaleX * x) <= x1 && (scaleX * y) <= y1;
    boolean scaleYInBounds = (scaleY * x) <= x1 && (scaleY * y) <= y1;

    if (scaleXInBounds && scaleYInBounds) {
        scale = (scaleX > scaleY) ? scaleX : scaleY;
    } else if (scaleXInBounds) {
        scale = scaleX;// ww  w  . j  ava 2 s . c  o m
    } else if (scaleYInBounds) {
        scale = scaleY;
    }

    return Bitmap.createScaledBitmap(b, (int) (scale * x), (int) (scale * y), true);
}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();/*  w  w  w .j a  v  a2  s  .c o  m*/

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    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 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);//from  ww  w .  j av  a 2  s. c  o  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);

    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }

    return output;
}

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);/*from   www .  ja v  a  2  s  .com*/
    return Bitmap.createBitmap(source, 0, 0, width, height, m, true);
}

From source file:Main.java

private static boolean checkBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return false;
    }//  w w w .j a va 2  s .c  o  m

    if (bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
        return false;
    }
    return true;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);/*from   w ww .  j av a 2 s.  c o  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 scaleToFitWidth(Bitmap b, int width) {
    float factor = width / (float) b.getWidth();
    return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
}

From source file:Main.java

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

    int len = width * height;
    byte[] yuv = new byte[len * 3 / 2];
    int y, u, v;//from  w  w w. j av  a 2s .com
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int rgb = pixels[i * width + j] & 0x00FFFFFF;

            int r = rgb & 0xFF;
            int g = (rgb >> 8) & 0xFF;
            int b = (rgb >> 16) & 0xFF;

            y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
            u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
            v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;

            y = y < 16 ? 16 : (y > 255 ? 255 : y);
            u = u < 0 ? 0 : (u > 255 ? 255 : u);
            v = v < 0 ? 0 : (v > 255 ? 255 : v);

            yuv[i * width + j] = (byte) y;
            // yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
            // yuv[len + (i >> 1) * width + (j & ~1) + 1] = (byte) v;
        }
    }
    return yuv;
}

From source file:Main.java

static public Bitmap scaleToFitWidth(Bitmap b, int width) {
    float factor = width / (float) b.getWidth();
    return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), false);
}

From source file:Main.java

static public Bitmap scaleToFitWidth(Bitmap b, int width) {
    float factor = width / (float) b.getWidth();
    return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
}