Android implementation picture Gaussian Blur

Source: Internet
Author: User
Tags vmin

The recent project design needs to use a slightly more blurred picture, so I went to Baidu search, treatment is probably the use of the Artifact PS (Flowers thousand bones See more, hehe). But in the eyes of a program ape, code can do everything. Below I will implement an Android Gaussian blur.

Gaussian Blur: Gaussian blur (Gaussian Blur) is an image processing software developed by Adobe Imaging software in the United States: a filter in Adobe Photoshop (series), in the following: Filter-blur-Gaussian blur! In the Gaussian blur principle, it is based on the Gaussian curve to adjust the image to a plain value, which is selectively blurred by the image. To put it bluntly, it is Gaussian blur can be a certain point around the image of the color of the Gaussian curve statistics, using the mathematical weighted average calculation method to get the value of the curve, and finally can leave the contour of the figure, that is, the curve. Is the bell-shaped curve that is generated when Adobe Photoshop applies a weighted average to pixels.
In the middle of PS, you should know all the colors but all the numbers, all kinds of fuzzy but are algorithms. In order to blur the value of the color-like plain, the mathematical method of weighted average (Gaussian function) to get the value, the range, radius, etc. are blurred, is roughly Gaussian blur.

This more official explanation.

principle : So-called "fuzzy", can be understood as each pixel to take the average of surrounding pixels.
Diagram of Gaussian blur principle

In the image on the right, 2 is the middle point and the surrounding point is 1.
The "Middle point" takes the average of "surrounding points" and becomes 1. In numerical values, this is a "smoothing". On the graph, it is equivalent to producing a "blur" effect, and the "middle point" loses detail.
Obviously, when you calculate the average, the greater the range, the stronger the blur effect.

Renderscript class : Renderscript is a Class C scripting language on the Android platform. Implement the Renderscript rendering function on our program.

Android's core code:

"'
public class Blur {

private static final String TAG = "Blur"; @SuppressLint ("Newapi") public static Bitmap Fastblur (context context, Bitmap sent Bitmap, int radius) {if (VERSION.        Sdk_int >) {Bitmap Bitmap = sentbitmap.copy (Sentbitmap.getconfig (), true);        Final Renderscript rs = renderscript.create (context);                Final Allocation input = Allocation.createfrombitmap (RS, Sentbitmap, 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 (RADIUS/* e.g. 3.F */);        Script.setinput (input);        Script.foreach (output);        Output.copyto (bitmap);    return bitmap;    } Bitmap 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];    LOG.E ("Pix", W + "" + H + "+ pix.length);    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 = 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] = (0xff 000000 & 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;    }} log.e ("Pix", W + "" + H + "+ pix.length);    Bitmap.setpixels (pix, 0, W, 0, 0, W, h); return (bitmap);}

}
The implementation principle is actually based on the Gaussian function. We are interested to study this function.

Usage is possible by passing in the corresponding value according to the constructor function.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android implementation picture Gaussian 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.