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 Bitmap applyReflection(Bitmap originalImage) {
    final int reflectionGap = 4;
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from ww  w  .ja v a  2s  .co m*/

    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix,
            false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(originalImage, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap toRoundBitmap(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 - 5;//w w  w. j  av a2  s.c om
        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 - 5;
        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, Bitmap.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_left + 15, dst_top + 15, dst_right - 20, dst_bottom - 20);

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

static private Bitmap smallCoverPostProc(Bitmap smallBitmap) {
    try {//from  w w w . j  a va  2  s .  c  om
        Bitmap smallBitmapPostProc = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas();
        canvas.setBitmap(smallBitmapPostProc);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap bitmapToShade = Bitmap.createBitmap(smallBitmap, 2, 2, smallBitmap.getWidth() - 4,
                smallBitmap.getHeight() - 4);
        BitmapShader bmShader = new BitmapShader(bitmapToShade, TileMode.CLAMP, TileMode.CLAMP);
        paint.setShader(bmShader);
        canvas.drawRoundRect(new RectF(2, 2, smallBitmap.getWidth() - 2, smallBitmap.getHeight() - 2), 4, 4,
                paint);
        return smallBitmapPostProc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    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);

    paint.setAntiAlias(true);// ww w . j ava2  s  . com

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);

    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;

}

From source file:Main.java

/**
 * Method to flip vertically a Bitmap.//from ww w .j a v a2 s. c o  m
 *
 * @param source The original Bitmap.
 * @return The flipped Bitmap.
 */
public static Bitmap flipVertically(Bitmap source) {
    Matrix m = new Matrix();
    m.preScale(1, -1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}

From source file:Main.java

/**
 * TODO doc//from w  w w.jav a2  s  . c o m
 *
 * @param source
 * @return
 */
public static Bitmap flipVerticallyBitmap(Bitmap source) {
    Matrix m = new Matrix();
    m.preScale(1, -1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}

From source file:Main.java

public static Bitmap reliefImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/* w  ww  .  j a  v  a 2  s.c o  m*/
    int r, g, b, a;

    Bitmap bitmap = Bitmap.createBitmap(width, height, 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 = 1; i < oldPx.length; i++) {
        color = oldPx[i - 1];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        int nextColor = oldPx[i];
        int r1 = Color.red(nextColor);
        int g1 = Color.green(nextColor);
        int b1 = Color.blue(nextColor);

        r = r1 - r + 127;
        g = g1 - g + 127;
        b = b1 - b + 127;

        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);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static Bitmap LoadFlipedVertical(Bitmap src) {
    Matrix matrix = new Matrix();
    matrix.preScale(1.0f, -1.0f);/*from w w  w.  ja  va  2s .c  o  m*/
    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap cropImage(RectF rect, Bitmap bitmap) {
    float width = rect.width() * 2;
    if (width > bitmap.getWidth()) {
        width = bitmap.getWidth();// www .j a v  a 2 s. c  o  m
    }

    float hight = rect.height() * 2;
    if (hight > bitmap.getHeight()) {
        hight = bitmap.getHeight();
    }

    float l = rect.centerX() - (width / 2);
    if (l < 0) {
        l = 0;
    }
    float t = rect.centerY() - (hight / 2);
    if (t < 0) {
        t = 0;
    }
    if (l + width > bitmap.getWidth()) {
        width = bitmap.getWidth() - l;
    }
    if (t + hight > bitmap.getHeight()) {
        hight = bitmap.getHeight() - t;
    }

    return Bitmap.createBitmap(bitmap, (int) l, (int) t, (int) width, (int) hight);

}

From source file:Main.java

/**
 * Method to flip horizontally a Bitmap.
 *
 * @param source The original Bitmap./*w  w w  .j a  v a 2s  . c  o m*/
 * @return The flipped Bitmap.
 */
public static Bitmap flipHorizonally(Bitmap source) {
    Matrix m = new Matrix();
    m.setScale(-1, 1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}