Android Hair Glass processing code (BLUR)

Source: Internet
Author: User
Tags vmin

The following is a tool class for processing images of bitmap images as frosted glass effects:

public class Fastblurutil {public static Bitmap Doblur (Bitmap sentbitmap, int radius, Boolean canreuseinbitmap) { Stack Blur v1.0 from//http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html////J Ava Author:mario Klingemann <mario at quasimondo.com>//http://incubator.quasimondo.com//created F        Eburary, 2004//Android Port:yahel Bouaziz <yahel at kayenko.com>//http://www.kayenko.com Ported April 5th,///This is a compromise between Gaussian Blur and Box Blur//It creates much B        Etter looking blurs than Box Blur, but is//7x faster than my Gaussian Blur implementation. I called it Stack Blur because this describes best how this//filter works Internally:it creates a K IND of moving stack//of colors whilst scanning through the image. thereby it//just have to add one new block of color to the right side//of the stack and remove the leftmost color. The remaining//colors on the topmost layer of the stack is either added on//or reduced by one, Dependin        G on if they is on the right or//on the left side of the stack. If you is using this algorithm in your code "ADD///The following line:////Sta        CK Blur Algorithm by Mario Klingemann <[email protected]> Bitmap Bitmap;        if (canreuseinbitmap) {bitmap = Sentbitmap;        } else {bitmap = Sentbitmap.copy (Sentbitmap.getconfig (), true);        } 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 < * 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; + +) {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; + +) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = Gsum = BS            um = 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); }}

As can be seen, the use of the method is very simple, the bitmap, the virtual program (typically 8), and whether to reuse flag.

Then, for larger graphs, an oom error often occurs because the virtual machine begins to recycle memory, including all soft references, when the virtualization begins. Then, a memory overflow still occurs. That means we can only blur the small plots to prevent memory overflow. But I do not want to change the other diagram, then, we should make this picture advanced scale.

Scaling method:

public static Bitmap Createscaledbitmap (Bitmap src, int dstwidth, int dstheight, Boolean filter)

The fourth input input parameter filter, refers to the scaling edge effect, filter is true to get a smooth edge bitmap, conversely, you will get an edge jagged, pixelrelated bitmap. Here, we have the image to be empty, does not matter the edge effect, so filter=false.

So, how to run the virtual code to achieve a large image of the virtual? The following is a concrete implementation code:

int scaleratio = 10;int Blurradius = 8; Bitmap Scaledbitmap = Bitmap.createscaledbitmap (Originbitmap,    originbitmap.getwidth ()/Scaleratio,    Originbitmap.getheight ()/Scaleratio,    false); Bitmap Blurbitmap = Fastblur.doblur (Scaledbitmap, Blurradius, True); Imageview.setscaletype ( ImageView.ScaleType.CENTER_CROP); Imageview.setimagebitmap (BLURBITMAP);

If the blur effect of the picture is weak or not obvious, provide a way to enhance the blur effect:

1, increase the value of scaleratio (increase the zoom ratio), using a smaller bitmap to blur, so that you can get a better effect of the bokeh, but also conducive to reducing memory consumption.

2, increase the value of Blurradius, this can improve the continuation of the layer, but will lead to more intensive CPU.

Resources: Implementing bitmap, Code addresses

Android Hair Glass processing code (BLUR)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.