"Turn" one, the blur effect of Android image effect processing

Source: Internet
Author: User

This article deals with the blur effect of image effects. As in front of the pixel is processed, the algorithm is generic, but the time will be longer, as for why, look at the following code you will understand.

Algorithm:

A simple algorithm: the pixel points around eight points including its own total of nine points of the RGB value is added after the average, as the current pixel RGB value, you can achieve the effect.

Example:

Abc

Def

GHI

If the current point is E, then there will be:

E.R = (A.R + B.R + C.R + D.R + E.R + F.R + G.R + H.R + I.R)/9  //  R represents the R value of the E-pixel RGB value

The GB value of the E-pixel point is similar.

Second, the use of Gaussian blur:

Gaussian matrix:

int New int [] {1, 2, 1, 2, 4, 2, 1, 2, 1};

The algorithm is to multiply the RGB values of the nine points by the sum of the corresponding entries in the Gaussian matrix, and then divide by a corresponding value as the RGB value of the current pixel point.

Example: (or nine points above)
If the current point is E, then there will be:

int delta =(A.R * gauss[0] + B.R * gauss[1] + C.R * gauss[2] + D.R * gauss[3] + E.R * gauss[4] + F. R * Gauss[5] + G.R * GAUSS[6] + H.R * GAUSS[7] + I.R * gauss[8])/Delta

e-pixel-like GB value, the Delta 's value seems to have no specified value, you can set any value, but to achieve the effect, can set the value is very small, the image below is a value of 16 effect.
Processing effect:

Original Picture:

After processing:

Two ways of handling code:

/*** Blur effect *@paramBMP *@return     */    PrivateBitmap blurimage (Bitmap bmp) {intwidth =bmp.getwidth (); intHeight =bmp.getheight (); Bitmap Bitmap=bitmap.createbitmap (width, height, Bitmap.Config.RGB_565); intPixcolor = 0; intNEWR = 0; intNEWG = 0; intNewb = 0; intNewcolor = 0; int[] Colors =New int[9] [3];  for(inti = 1, length = width-1; i < length; i++)        {             for(intK = 1, len = height-1; K < Len; k++)            {                 for(intm = 0; M < 9; m++)                {                    //(s,p) is the above e point, the switch statement 0~8 a total of 9 branches representing the 9 point coordinates around E (including e points)                    ints = 0; intp = 0; Switch(m) { Case0: S= I-1; P= K-1;  Break;  Case1: S=i; P= K-1;  Break;  Case2: S= i + 1; P= K-1;  Break;  Case3: S= i + 1; P=K;  Break;  Case4: S= i + 1; P= k + 1;  Break;  Case5: S=i; P= k + 1;  Break;  Case6: S= I-1; P= k + 1;  Break;  Case7: S= I-1; P=K;  Break;  Case8: S=i; P=K; } Pixcolor=Bmp.getpixel (S, p); //get the R,g,b value of these points separatelyColors[m][0] =color.red (Pixcolor); colors[m][1] =Color.green (Pixcolor); colors[m][2] =Color.Blue (Pixcolor); }                //R,g,b values of 9 points are added separately                 for(intm = 0; M < 9; m++) {NEWR+ = Colors[m][0]; NEWG+ = Colors[m][1]; Newb+ = Colors[m][2]; }                //take the average again .NEWR = (int) (NEWR/9F); NEWG= (int) (NEWG/9F); Newb= (int) (NEWB/9F); //ensure that the range of each value is 0~255NEWR = Math.min (255, Math.max (0, NEWR)); NEWG= Math.min (255, Math.max (0, NEWG)); Newb= Math.min (255, Math.max (0, newb)); Newcolor= Color.argb (255, NEWR, NEWG, newb);                                Bitmap.setpixel (i, K, Newcolor); NEWR= 0; NEWG= 0; Newb= 0; }        }                returnbitmap; }        /*** Softening effect (Gaussian blur) (optimized three times times faster than above) *@paramBMP *@return     */    PrivateBitmap blurimageameliorate (Bitmap bmp) {LongStart =System.currenttimemillis (); //Gaussian matrix        int[] Gauss =New int[] {1, 2, 1, 2, 4, 2, 1, 2, 1 }; intwidth =bmp.getwidth (); intHeight =bmp.getheight (); Bitmap Bitmap=bitmap.createbitmap (width, height, Bitmap.Config.RGB_565); intPIXR = 0; intPIXG = 0; intPIXB = 0; intPixcolor = 0; intNEWR = 0; intNEWG = 0; intNewb = 0; intDelta = 16;//the smaller the value, the brighter the picture, and the darker the larger it becomes.                intIDX = 0; int[] pixels =New int[Width *height]; Bmp.getpixels (Pixels,0, Width, 0, 0, width, height);  for(inti = 1, length = height-1; i < length; i++)        {             for(intK = 1, len = width-1; K < Len; k++) {idx= 0;  for(intm =-1; M <= 1; m++)                {                     for(intn =-1; n <= 1; n++) {Pixcolor= pixels[(i + m) * Width + k +N]; PIXR=color.red (Pixcolor); Pixg=Color.green (Pixcolor); PIXB=Color.Blue (Pixcolor); NEWR= NEWR + (int) (PIXR *Gauss[idx]); NEWG= NEWG + (int) (PIXG *Gauss[idx]); Newb= Newb + (int) (PIXB *Gauss[idx]); IDX++; }} NEWR/=Delta; NEWG/=Delta; Newb/=Delta; NEWR= Math.min (255, Math.max (0, NEWR)); NEWG= Math.min (255, Math.max (0, NEWG)); Newb= Math.min (255, Math.max (0, newb)); Pixels[i* Width + K] = Color.argb (255, NEWR, NEWG, newb); NEWR= 0; NEWG= 0; Newb= 0; }} bitmap.setpixels (pixels,0, Width, 0, 0, width, height); LongEnd =System.currenttimemillis (); LOG.D ("May", "used time=" + (End-start)); returnbitmap; }

in the optimized code, be aware that pixels array cannot exceed the specified size, that is, the size of the picture can not be too large, otherwise the stack memory overflow.

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.