Example usage for android.graphics Bitmap getConfig

List of usage examples for android.graphics Bitmap getConfig

Introduction

In this page you can find the example usage for android.graphics Bitmap getConfig.

Prototype

public final Config getConfig() 

Source Link

Document

If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.

Usage

From source file:Main.java

public static Bitmap roundCornerFromFile(String filePath, int pixels, int size) {
    try {//from w ww .jav a2 s. c  o m
        Bitmap bitmap = loadFile(filePath, size);
        if (bitmap == null)
            return null;
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        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 = pixels;
        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, rect, rect, paint);
        bitmap.recycle();
        bitmap = null;
        return output;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static int getByteCount(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= 19) {
        return bitmap.getAllocationByteCount();
    } else if (Build.VERSION.SDK_INT >= 12) {
        return bitmap.getByteCount();
    } else {/*w  w  w.j  a  va  2s  .com*/
        return getByteCount(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    }
}

From source file:Main.java

public static Bitmap roundCornerFromFile(String filePath, int pixels) {
    try {/*from w w  w .j a v  a2s  .  co m*/
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        if (bitmap == null)
            return null;
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        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 = pixels;
        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, rect, rect, paint);
        bitmap.recycle();
        bitmap = null;
        return output;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int w, int h, int radius) {
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    fastblur(sentBitmap, bitmap, w, h, radius);
    return bitmap;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap blur(Context context, Bitmap bitmap) {
    if (Build.VERSION.SDK_INT > 16) {
        bitmap = bitmap.copy(bitmap.getConfig(), true);

        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(24.f /* e.g. 3.f */);
        script.setInput(input);//from ww  w . j a v  a 2  s  .c  o  m
        script.forEach(output);
        output.copyTo(bitmap);
    } else {
        bitmap = fastBlur(context, bitmap, 10);
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap scrambleImage(Bitmap image, int piecesPerLine) {
    int px = (int) Math.ceil(image.getWidth() / (double) piecesPerLine);
    int ph = (int) Math.ceil(image.getHeight() / (double) piecesPerLine);
    int count = piecesPerLine * piecesPerLine;
    int[] arr = shuffle(arrayRange(count));

    Config config = image.getConfig() == null ? Bitmap.Config.ARGB_8888 : image.getConfig();
    Bitmap destBmp = Bitmap.createBitmap(image.getWidth(), image.getHeight(), config);
    Canvas canvas = new Canvas(destBmp);

    for (int i = 0; i < piecesPerLine; i++) {
        for (int j = 0; j < piecesPerLine; j++) {
            int n = arr[i * piecesPerLine + j];
            int row = n / piecesPerLine;
            int col = n % piecesPerLine;

            Rect fromRect = new Rect(row * px, col * ph, row * px + px, col * ph + ph);
            Rect toRect = new Rect(j * px, i * ph, j * px + px, i * ph + ph);

            canvas.drawBitmap(image, fromRect, toRect, null);
        }/*w  ww.j  a va  2 s .  c o  m*/
    }

    return destBmp;
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int radius) {
    int w = sentBitmap.getWidth();
    int h = sentBitmap.getHeight();
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    fastblur(sentBitmap, bitmap, w, h, radius);
    return bitmap;
}

From source file:Main.java

/**
 * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix.
 *///from   w  w w . j  a v a 2 s.  c  o  m
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) {
    // Re-implement Bitmap createBitmap() to always return a mutable bitmap.
    Canvas canvas = new Canvas();

    Bitmap bitmap;
    Paint paint;
    if ((m == null) || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(width, height, source.getConfig());
        paint = null;
    } else {
        RectF rect = new RectF(0, 0, width, height);
        m.mapRect(rect);
        bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig());

        canvas.translate(-rect.left, -rect.top);
        canvas.concat(m);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        if (!m.rectStaysRect()) {
            paint.setAntiAlias(true);
        }
    }
    bitmap.setDensity(source.getDensity());
    canvas.setBitmap(bitmap);

    Rect srcBounds = new Rect(x, y, x + width, y + height);
    RectF dstBounds = new RectF(0, 0, width, height);
    canvas.drawBitmap(source, srcBounds, dstBounds, paint);
    return bitmap;
}

From source file:Main.java

static Bitmap blurfast(Bitmap bmp, int radius) {
    try {//ww w.  ja v  a 2s . c  o m
        Bitmap bitmap = bmp.copy(bmp.getConfig(), true);
        int w = bmp.getWidth();
        int h = bmp.getHeight();
        int[] pix = new int[w * h];
        bmp.getPixels(pix, 0, w, 0, 0, w, h);

        for (int r = radius; r >= 1; r /= 2) {
            for (int i = r; i < h - r; i++) {
                for (int j = r; j < w - r; j++) {
                    int tl = pix[(i - r) * w + j - r];
                    int tr = pix[(i - r) * w + j + r];
                    int tc = pix[(i - r) * w + j];
                    int bl = pix[(i + r) * w + j - r];
                    int br = pix[(i + r) * w + j + r];
                    int bc = pix[(i + r) * w + j];
                    int cl = pix[i * w + j - r];
                    int cr = pix[i * w + j + r];

                    pix[(i * w) + j] = 0xFF000000
                            | (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + (br & 0xFF)
                                    + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF
                            | (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) + (br & 0xFF00)
                                    + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00
                            | (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + (bl & 0xFF0000)
                                    + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000)
                                    + (cr & 0xFF0000)) >> 3) & 0xFF0000;
                }
            }
        }
        bitmap.setPixels(pix, 0, w, 0, 0, w, h);
        return bitmap;
    } catch (Exception e) {
        return bmp;
    }
}

From source file:Main.java

private static Bitmap stackblur(Bitmap sentBitmap, int radius) {
    Bitmap bitmap = null;//from  w ww .ja  v a  2 s.c om
    try {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return sentBitmap;
    }
    if (radius < 1) {
        return (null);
    }
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);
    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;
    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];
    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }
    yw = yi = 0;
    int[][] stack = new int[div][3];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;
    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;
        for (x = 0; x < w; x++) {
            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];
            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];
            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];
            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;
            sir = stack[i + radius];
            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];
            rbs = r1 - Math.abs(i);
            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];
            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];
            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];
            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];
            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            yi += w;
        }
    }
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);
    return (bitmap);
}