Android realizes Gaussian Blur

Source: Internet
Author: User

about how to blur the image, online methods are more, commonly used and convenient method is to use Gaussian blur, but the method of the Internet is not ideal, today to share a previous project used in the fuzzy processing method to achieve Gaussian blur, OK, first look at the blur effect:

Original:

Fuzzy:

Note the point:

The various processing of the picture is mostly through bitmap operation, this example is no exception, in addition, this example is the use of imageloader loaded network pictures, to give everyone a fuzzy network image reference, of course, loading network pictures must be asynchronous, So the first time to enter the interface loading pictures will have a waiting time, if you want the perfect user experience, you need to use your brains!

Gauss encapsulation Tool Class:

Bitmapblurutil:

 PackageCom.byl.blur;ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors;ImportAndroid.graphics.Bitmap;Importandroid.graphics.drawable.BitmapDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Handler;ImportAndroid.os.Message; Public  class bitmapblurutil {    Private StaticExecutorservice executor;Private Static intPool_size =2;//Single CPU thread pool size    Private StaticExecutorserviceGetexecutor() {if(Executor = =NULL) {intCpunums = Runtime.getruntime (). Availableprocessors ();        Executor = Executors.newfixedthreadpool (Cpunums * pool_size); }returnExecutor } Public Static void AddTask(Bitmap Bitmap, Handler Handler) {Getexecutor (). Submit (NewBitmapvaguetask (bitmap, handler)); }/** Horizontal direction blur * /    Private Static floatHradius =3;/** Vertical direction blur * /    Private Static floatVradius =3;/** Fuzzy Iteration degree * /    Private Static intiterations =5;/** * Async * @author Baiyuliang * *    Private Static  class bitmapvaguetask implements Runnable {        PrivateBitmap Bitmap;PrivateHandler Handler; Public Bitmapvaguetask(Bitmap Bitmap, Handler Handler) {Super(); This. Bitmap = bitmap; This. handler = handler; }@Override         Public void Run() {Boxblurfilter (bitmap, handler); }    }/** * Gaussian Blur * * @param BMP * @return  */    Private Static void Boxblurfilter(Bitmap bmp, Handler Handler) {intwidth = Bmp.getwidth ();intHeight = bmp.getheight ();int[] Inpixels =New int[Width * height];int[] Outpixels =New int[Width * height];        Bitmap Bitmap = Bitmap.createbitmap (width, height,bitmap.config.argb_8888); Bmp.getpixels (Inpixels,0, Width,0,0, width, height); for(inti =0; I < iterations;            i++) {blur (Inpixels, outpixels, width, height, hradius);        Blur (Outpixels, inpixels, height, width, vradius);        } blurfractional (Inpixels, outpixels, width, height, hradius);        Blurfractional (Outpixels, inpixels, height, width, vradius); Bitmap.setpixels (Inpixels,0, Width,0,0, width, height);if(Handler! =NULL) {@SuppressWarnings("Deprecation") Drawable drawable =NewBitmapdrawable (bitmap); Message message =NewMessage ();            Message.obj = drawable;        Handler.sendmessage (message); }    }Private Static void Blur(int[] in,int[] out,intWidthintHeightfloatRADIUS) {intWidthMinus1 = width-1;intR = (int) Radius;intTablesize =2* R +1;intDivide[] =New int[ the* Tablesize]; for(inti =0; I < the* TABLESIZE; i++) Divide[i] = i/tablesize;intInindex =0; for(inty =0; Y < height; y++) {intOutindex = y;intTA =0, tr =0, TG =0, TB =0; for(inti =-r; I <= R; i++) {intRGB = In[inindex + Clamp (i,0, Width-1)]; Ta + = (RGB >> -) &0xFF; TR + = (RGB >> -) &0xFF; TG + = (RGB >>8) &0xFF; TB + = RGB &0xFF; } for(intx =0; x < width; X + +) {Out[outindex] = (Divide[ta] << -) | (Divide[tr] << -)                        | (DIVIDE[TG] <<8) | DIVIDE[TB];intI1 = x + R +1;if(I1 > WidthMinus1) i1 = WidthMinus1;intI2 = X-r;if(I2 <0) I2 =0;intRGB1 = In[inindex + I1];intRGB2 = In[inindex + I2]; Ta + = (rgb1 >> -) &0xFF)-((Rgb2 >> -) &0xFF); TR + = (RGB1 &0xff0000)-(Rgb2 &0xff0000)) >> -; TG + = (RGB1 &0xff00)-(Rgb2 &0xff00)) >>8; TB + = (RGB1 &0xFF)-(Rgb2 &0xFF);            Outindex + = height;        } Inindex + = width; }    }Private Static void blurfractional(int[] in,int[] out,intWidthintHeightfloatRADIUS) {Radius-= (int) Radius;floatf =1.0F/(1+2* radius);intInindex =0; for(inty =0; Y < height; y++) {intOutindex = y; Out[outindex] = in[0]; Outindex + = height; for(intx =1; X < width-1; X + +) {inti = Inindex + x;intRGB1 = In[i-1];intRGB2 = In[i];intRGB3 = In[i +1];intA1 = (RGB1 >> -) &0xFF;intR1 = (Rgb1 >> -) &0xFF;intG1 = (RGB1 >>8) &0xFF;intB1 = RGB1 &0xFF;intA2 = (Rgb2 >> -) &0xFF;intr2 = (Rgb2 >> -) &0xFF;intG2 = (Rgb2 >>8) &0xFF;intB2 = rgb2 &0xFF;intA3 = (Rgb3 >> -) &0xFF;intR3 = (Rgb3 >> -) &0xFF;intG3 = (Rgb3 >>8) &0xFF;intB3 = rgb3 &0xFF; a1 = A2 + (int) ((A1 + A3) * radius); R1 = R2 + (int) ((r1 + R3) * radius); G1 = G2 + (int) ((G1 + g3) * radius); B1 = B2 + (int) ((B1 + b3) * radius);                A1 *= F;                R1 *= F;                G1 *= F;                B1 *= F; Out[outindex] = (A1 << -) | (R1 << -) | (G1 <<8) |                B1;            Outindex + = height; } Out[outindex] = In[width-1];        Inindex + = width; }    } Public Static int Clamp(intXintAintb) {return(x < a)? A: (x > B)?    b:x; }}

The degree of ambiguity can be adjusted in class Hradius,vradius,iterations, three variables! How to use:

        //Load network pictures and get bitmapImageloader.getinstance (). LoadImage ("Http://www.2cto.com/meinv/uploads/160328/1-16032Q42211962.jpg",NewImageloadinglistener () {@Override             Public void onloadingstarted(String Imageuri, view view) {            }@Override             Public void onloadingfailed(String Imageuri, View View,failreason Failreason) {            }@Override             Public void Onloadingcomplete(String Imageuri, view view,FinalBitmap loadedimage) {if(loadedimage!=NULL){//Fuzzy processingBitmapblurutil.addtask (Loadedimage,NewHandler () {@Override                         Public void Handlemessage(Message msg) {Super. Handlemessage (msg);                            Drawable drawable = (drawable) msg.obj;                            Iv_blur.setimagedrawable (drawable);                        Loadedimage.recycle ();                }                    }); }            }@Override             Public void onloadingcancelled(String Imageuri, view view) {            }        });

ecdemo:http://download.csdn.net/detail/baiyuliang2013/9512311

Android realizes Gaussian Blur

Related Article

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.