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

/**
 * Shrinks and rotates (if necessary) a passed Bitmap.
 *
 * @param bm/*from w w  w  .j  a  va 2 s.  co  m*/
 * @param maxLengthOfEdge
 * @param rotateXDegree
 * @return Bitmap
 */
public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge, int rotateXDegree) {
    if (maxLengthOfEdge > bm.getWidth() && maxLengthOfEdge > bm.getHeight()) {
        return bm;
    } else {
        // shrink image
        float scale = (float) 1.0;
        if (bm.getHeight() > bm.getWidth()) {
            scale = ((float) maxLengthOfEdge) / bm.getHeight();
        } else {
            scale = ((float) maxLengthOfEdge) / bm.getWidth();
        }
        // CREATE CommonAsync MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scale, scale);
        matrix.postRotate(rotateXDegree);

        // RECREATE THE NEW BITMAP
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);

        matrix = null;
        System.gc();

        return bm;
    }
}

From source file:Main.java

public static void invertImage(Bitmap b) {
    int A, R, G, B;
    int pixelColor;
    int height = b.getHeight();
    int width = b.getWidth();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixelColor = b.getPixel(x, y);
            A = Color.alpha(pixelColor);

            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);

            b.setPixel(x, y, Color.argb(A, R, G, B));
        }/*from   w  w w.  ja  v  a 2  s.co m*/
    }
}

From source file:Main.java

public static Bitmap rotate(int angle, Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.postRotate((float) angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap rotateBitmap(int degree, Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.postRotate((float) degree);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap changeBitmapColor(@NonNull Bitmap sourceBitmap, @IntegerRes int color) {

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
            sourceBitmap.getHeight() - 1);
    Paint p = new Paint();
    ColorFilter filter = new LightingColorFilter(color, 1);
    p.setColorFilter(filter);/*from  w  w w  .j a  va2  s  .c  o m*/

    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, p);
    return resultBitmap;
}

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;/* w  ww . j  a v a2s.  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 = null;
    try {
        output = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    } catch (Exception e) {
        return output;
    }
    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 transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
    try {/*  www. jav  a  2 s  .c o m*/
        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 cropCenterBitmap(Bitmap src, int w, int h) {
    if (src == null)
        return null;

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

    if (width < w && height < h)
        return src;

    int x = 0;/* ww w  .jav a2 s.  c  om*/
    int y = 0;

    if (width > w)
        x = (width - w) / 2;

    if (height > h)
        y = (height - h) / 2;

    int cw = w; // crop width
    int ch = h; // crop height

    if (w > width)
        cw = width;

    if (h > height)
        ch = height;

    return Bitmap.createBitmap(src, x, y, cw, ch);
}

From source file:Main.java

private static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);// w w  w. j  a va 2s  .c  o  m
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();
    }
    return target;
}

From source file:Main.java

@NonNull
public static byte[] bitmapToByteArray(@NonNull Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {//from  w w w  .  j ava2  s  .co  m
        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) {
            }
    }
}