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 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;//  w  w  w.j a v a 2 s . c o m
        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 getCroppedBitmap(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());

    paint.setAntiAlias(true);//from w w w  .  j  a  v a  2 s  .  co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    } else {
        // Yatay
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getHeight() / 2, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    if (bitmap.getHeight() > bitmap.getWidth()) {
        // Dikey
        output = Bitmap.createBitmap(output, 0, output.getHeight() / 2 - output.getWidth() / 2,
                output.getWidth(), output.getWidth());
    } else {
        // Yatay
        output = Bitmap.createBitmap(output, output.getWidth() / 2 - output.getHeight() / 2, 0,
                output.getHeight(), output.getHeight());
    }

    return output;
}

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);//  w  ww  . ja v  a2 s.co 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

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

From source file:Main.java

/**
 * Create a rounded corner bitmap from current bitmap
 *
 * @param bitmap//w ww  .j a va  2s  .c o m
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    try {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        final float roundPx = 50;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap flip(Bitmap src) {
    Matrix m = new Matrix();
    m.preScale(-1, 1);//from w ww . j  a  va  2s .co m
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    return dst;
}

From source file:Main.java

public static Bitmap resize(Bitmap bMap) {
    if ((float) bMap.getHeight() / (float) bMap.getWidth() == .5625
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .5625) { //16/9
        if (bMap.getHeight() < bMap.getWidth()) {
            //bMap = Bitmap.createScaledBitmap(bMap, 1024, 576, false);
            bMap = Bitmap.createScaledBitmap(bMap, 1280, 720, false); //720p
        } else {//from ww w  .  ja va  2 s  .com
            //bMap = Bitmap.createScaledBitmap(bMap, 576, 1024, false);
            bMap = Bitmap.createScaledBitmap(bMap, 720, 1280, false); //720p
        }
    } else if ((float) bMap.getHeight() / (float) bMap.getWidth() == .625
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .625) { //16/10
        if (bMap.getHeight() < bMap.getWidth()) {
            //bMap = Bitmap.createScaledBitmap(bMap, 960, 600, false);
            bMap = Bitmap.createScaledBitmap(bMap, 1152, 720, false); //720p
        } else {
            //bMap = Bitmap.createScaledBitmap(bMap, 600, 960, false);
            bMap = Bitmap.createScaledBitmap(bMap, 720, 1152, false); //720p
        }
    } else if ((float) bMap.getHeight() / (float) bMap.getWidth() == .75
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .75) { //4/3
        if (bMap.getHeight() < bMap.getWidth()) {
            bMap = Bitmap.createScaledBitmap(bMap, 960, 720, false);
        } else {
            bMap = Bitmap.createScaledBitmap(bMap, 720, 960, false);
        }
    }
    return bMap;
}

From source file:Main.java

/**
 * Method to split an image in {@code numStages} pieces.
 *
 * @param bmpOriginal The original Bitmap.
 * @param numStages   int that represents the number of pieces.
 * @return A List of Bitmap, i.e. a List of pieces of {@code bmpOriginal}
 *///from  ww w.  j  a  v  a2  s . c  o m
public static List<Bitmap> splitImage(Bitmap bmpOriginal, int numStages) {
    List<Bitmap> pieces = new ArrayList<>();
    int width = bmpOriginal.getWidth() / numStages;
    int start = 0;
    for (int i = 0; i < numStages; i++) {
        Bitmap pieceBitmap = Bitmap.createBitmap(bmpOriginal, start, 0, width - 1, bmpOriginal.getHeight() - 1);
        pieces.add(pieceBitmap);
        start = (bmpOriginal.getWidth() / numStages) * (i + 1);
    }
    return pieces;
}

From source file:Main.java

public static Bitmap rotate(Bitmap originalBitmap, float alpha) {
    if (originalBitmap == null)
        return null;
    int width = originalBitmap.getWidth();
    int height = originalBitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.setRotate(alpha);/*w  w  w  .j  a  va2  s .  co m*/
    return Bitmap.createBitmap(originalBitmap, 0, 0, width, height, matrix, true);
}

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