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 zoomBitmap(Bitmap bitmap, int width, int height) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap newbmp;/*  ww w  .j a v  a2  s .c  om*/
    try {
        // if (h >= w) {
        // if (height <= h) {
        Matrix matrix = new Matrix();
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleHeight, scaleHeight);
        newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
        // }
        // } else {
        // if (width <= w) {
        // Matrix matrix = new Matrix();
        // float scaleWidth = ((float) width / w);
        // matrix.postScale(scaleWidth, scaleWidth);
        // newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
        // true);
        // return newbmp;
        // }
        // }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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 = bitmap.getWidth() / 2;

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

/**
 * Create a bitmap which is wrapped to circle
 * (similar to what you can see in G+ profile pic.)
 *
 * @param bitmap Original Bitmap/* w  w  w . jav  a 2s .  co m*/
 * @return Circled bitmap
 */
public static Bitmap createCircleBitmap(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint bitmapPaint = new Paint();
    bitmapPaint.setAntiAlias(true);
    bitmapPaint.setShader(bitmapShader);

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint);

    return output;
}

From source file:Main.java

public static Bitmap handleImageNegative(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/*  w  w w .  j  a  v  a 2  s.  co  m*/
    int r, g, b, a;
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];
    bm.getPixels(oldPx, 0, width, 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = 255 - r;
        g = 255 - g;
        b = 255 - b;
        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }
        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }
        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;

}

From source file:Main.java

/**
 * Copy the red channel to a new Bitmap's alpha channel.  The old
 * one will be recycled.//w  w  w .j  a v a 2  s . c o m
 */
static Bitmap redToAlpha(Bitmap src, boolean recycle) {
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

    float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix)));

    Canvas canvas = new Canvas(dest);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    canvas.drawBitmap(src, 0, 0, paint);

    if (recycle)
        src.recycle();
    return dest;
}

From source file:Main.java

public static Rect getBestFitRect(Bitmap bitmap, float ratio) {
    float bmpRatio = (float) bitmap.getWidth() / (float) bitmap.getHeight();

    if (bmpRatio > ratio) // bmp is wider
    {//  w  ww.java2s. c o  m
        int height = bitmap.getHeight();
        int width = (int) (height * ratio);
        int offset = (bitmap.getWidth() - width) / 2;
        return new Rect(offset, 0, offset + width, height);
    } else if (bmpRatio < ratio) // bmp is taller
    {
        int width = bitmap.getWidth();
        int height = (int) (width / ratio);
        int offset = (bitmap.getHeight() - height) / 2;
        return new Rect(0, offset, width, offset + height);
    } else
        return new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
}

From source file:Main.java

public static Bitmap ResizeBitmap(Bitmap bitmap, int newWidth) {

    int width = bitmap.getWidth();

    int height = bitmap.getHeight();

    float temp = ((float) height) / ((float) width);

    int newHeight = (int) ((newWidth) * temp);

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    //         matrix.postRotate(90);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    bitmap.recycle();/*from   www . j a va  2s.  co  m*/

    return resizedBitmap;

}

From source file:Main.java

public static Bitmap scaleDown(Bitmap bmp, float maxImgSize, boolean filter) {
    float ratio = Math.min((float) maxImgSize / bmp.getWidth(), (float) maxImgSize / bmp.getHeight());
    int width = Math.round((float) ratio * bmp.getWidth());
    int height = Math.round((float) ratio * bmp.getHeight());

    return Bitmap.createScaledBitmap(bmp, width, height, filter);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static int sizeOf(Bitmap data) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
        return data.getRowBytes() * data.getHeight();
    } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return data.getByteCount();
    } else {//  w  ww  .  j a v a  2  s.  c o  m
        return data.getAllocationByteCount();
    }
}

From source file:Main.java

public static float getScale(Bitmap bitmap, Context context) {
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    DisplayMetrics outMetrics = getScreenPixels(context);
    int displayW = outMetrics.widthPixels;
    int displayH = outMetrics.heightPixels;
    float scale = bitmapHeight > bitmapWidth ? displayH / (bitmapHeight * 1f) : displayW / (bitmapWidth * 1f);
    return scale;
}