Android realizes Gaussian Blur effect and is compatible with low version _android

Source: Internet
Author: User

One, the effect demonstration

The project used a Gaussian blur effect, looked over some data, considering the performance of the final choice to use the Android Renderscript Library to achieve, on the use of renderscript to achieve a Gaussian blur online there are many similar methods, most of the summary of the relatively chaotic, Here is to do a collation, for similar needs of students reference and learning.



(Project effect Chart)

A simple description of the project effect diagram of the implementation of ideas:

① load-defined XML layout

② to get the bitmap object of the current window using the screen-clipping method

③ the bitmap object to compress and Gaussian blur processing

④ the processed fuzzy graph object as the background of the layout loaded in the ①

⑤ will add layout to the Popuwindow and handle the pop-up of the child entries that have been added to the Blur map object

Second, apply renderscript to realize Gauss Blur

The method of realizing Gaussian blur effect has a lot of, can use Java to realize, can realize with NDK, also can use the way that this article recommends to realize (also use JNI way), as to why choose use Renderscript way to realize, must have certain merit.

Advantages: Renderscript mode, very fast, about 100 times times the speed of Java, NDK mode 20 times times the speed (different picture quality test results are different for reference)

Disadvantage: API17 above effective. (but Google has provided a backward-compatible approach, as described later in the article)

The following is the core code that uses the Renderscript method:

/************************ * Gaussian blur processing * @param bitmap * @param context * @return ***********************/public Stati C Bitmap Blurbitmap (Bitmap Bitmap, Context context) {//Create a Gaussian blur Bitmap with the need to create an empty Bitmap Bitmap Outbitmap = bitmap.createbit 
Map (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Initialize Renderscript, which provides the renderscript context, which must be created before other RS classes can be created, which controls Renderscript initialization, resource management, and release renderscript rs = 
Renderscript.create (context); 
Create a Gaussian fuzzy object Scriptintrinsicblur blurscript = Scriptintrinsicblur.create (RS, Element.u8_4 (RS)); Create allocations, which is the primary way to pass data to the Renderscript kernel and develop a fallback type to store the given type allocation Allin = Allocation.createfrombitmap (RS, 
Bitmap); 
Allocation Allout = Allocation.createfrombitmap (RS, outbitmap); 
Set ambiguity (note: Radius max can only be set to 25.F) Blurscript.setradius (15.F); 
Perform the Renderscript blurscript.setinput (Allin); 
Blurscript.foreach (Allout); 
Copy the final bitmap created by the "Out allocation" to the Outbitmap Allout.copyto (OUTBITMAP); Recycle the originAl bitmap Bitmap.recycle (); 
After finishing everything, we destroy the Renderscript. 
Rs.destroy (); 
return outbitmap; }

The annotation described in this method is very clear, but it is important to note that the Blurscript.setradius (); This method sets the ambiguity when the radius can only be set 25.F, may be directly to the image processing will lead to poor fuzzy effect, so the maximum effective setting of 25, but if you want to achieve a deeper blur effect, you can first compress the picture, reduce the quality of the picture to achieve a more ambiguous effect.

Below is the image compression processing method:

/** * Compress Image by Pixel, this'll modify image width/height. 
* * @param imgpath Image path * @param pixelw target pixel of width * @param pixelh target pixel of height * @return */public static Bitmap ratio (String imgpath, float pixelw, float pixelh) {bitmapfactory.options newopts = new BITMAPFA Ctory. 
Options (); 
Begins reading into the picture, at which point the options.injustdecodebounds is set back to true, that is, the read-only side does not read the content newopts.injustdecodebounds = true; 
Newopts.inpreferredconfig = config.rgb_565; 
Get bitmap info, but notice that bitmap are null now bitmap bitmap = Bitmapfactory.decodefile (imgpath,newopts); 
Newopts.injustdecodebounds = false; 
int w = newopts.outwidth; 
int h = newopts.outheight; float ww = PIXELW; Set width of 120f, you can obviously see the picture reduced to float hh = pixelh; When you set the height to 240f, you can see that the picture shrinks/zooms out, because it is fixed scaling, only one of the data is calculated with a high or a width int is = 1;//represents not scaling if (W > H && w > ww) {//AS 
If the fruit width is large, scale the BE = (int) (NEWOPTS.OUTWIDTH/WW) According to the fixed size of the width; else if (W < h && h > hh) {//If height is high, scale the BE = (int) based on width fixed size (NewoPTS.OUTHEIGHT/HH); 
if (is <= 0) be = 1; Newopts.insamplesize = be;//Set scaling//start compressing the picture, note that the options.injustdecodebounds has been set back to false at this time bitmap = 
Bitmapfactory.decodefile (Imgpath, newopts); Compress a good proportion of the size after the quality of compression//return compress (bitmap, maxSize); 
Here again the meaning of quality compression is small, instead of consuming resources, delete return bitmap;  }

The above method is to use Renderscript to realize Gauss Fuzzy core code block and need to pay attention to place. However, it is also necessary to pay attention to compatibility issues, the above mentioned that the method is only valid for API17 above, then the problem, the need to deal with the problem of API backward compatibility.

Third, processing API backward compatibility problems and attention points

After the Gaussian Blur is implemented according to the above method, run a look at the effect of a full sense of achievement, at this time, boss just take customers over, young man, to help customers install a latest version (Customer mobile phone system version for Android4.0), after loading a little ... This is so embarrassing ... it's just so awkward.

When you track a bug, you may receive the following error message:

Exception Information One:

09-21 15:07:34.417:e/androidruntime (4476): Android.support.v8.renderscript.RSRuntimeException:Error loading RS JNI Library:java.lang.UnsatisfiedLinkError:Couldn ' t load rssupport:findlibrary returned null ..........

Exception Information Two:

Java.lang.NoClassDefFoundError:android.renderscript.ScriptIntrinsicBlur ............

Solution:

The error message is Android.support.v8.renderscript.RSRuntimeException:Error loading and java.lang.UnsatisfiedLinkError:Couldn ' t Load Rssupport from loader Dalvik.system.PathClassLoader is due to be brought in over 4.2 of the mobile phone Librsjni.so and librssupport.so libraries, while some mobile phones under 4.2 do not have these two JNI libraries. So we have to import these two jni into our project. So where is the file?

The following is my file path: C:\Tools\android-sdk\build-tools\23.0.3\renderscript\lib\packaged, The Renderscript-v8.jar package is located in the Renderscript\lib directory.

That is: Under the Android SDK path, the four directories under each version of Build-tools\ are \renderscript\lib\packaged, Note that the selection of jar packages and. So versions is best selected for the most recent, such as the addition of x86-64 in 24.0.0.

Well, it's perfect to be compatible with the version below 4.2. In addition, there is a most important point of attention, I was troubled by this detail for at least 2 hours, now think of the egg pain, after you finish all the above things, please be sure to be able to import the package path to change over: import Android.renderscript replacement for import Android.support.v8.renderscript. Specifically as follows:

Import android.support.v8.renderscript.Allocation; 
Import android.support.v8.renderscript.Element; 
Import Android.support.v8.renderscript.RenderScript; 

The most important last thing to note is that if you add confusion to the code in your project, don't forget it. OK, it's over here.

Four, attached:

Also share two GitHub for dealing with Gauss Blur:

Https://github.com/wl9739/BlurredView
Https://github.com/robinxdroid/Blur

The above is a small set of Android to introduce the Gauss Blur effect and compatible with the low version of all the narration, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.