33

I've been searching the past three days for a built-in, hardware-accelerated way of bluring a bitmap with android. I stumbled upon certain work-arounds like shrinking the bitmap and scaling it up again, but this method produced low quality results which were not suitable for my image recognition requirements. I also read that implementing convolution with shaders or JNI is a good way to go, but I cannot believe that there is no built-in solution in the Android framework for this very common purpose. Currently I've ended up with a self-written convolution implementation in Java, but it is awkwardly slow. My question is:

  • Is there really no built-in solution in the Android framework?
  • In case there is none: what is the most efficient way of accelerating the convolution with a still reasonable complexity of implementation and maintenance? Shall we use JNI, shaders or something completely different?

2 Answers 2

74

I finally found a suitable solution:

  • RenderScript allows implementing heavy computations which are scaled transparently to all cores available on the executing device. I've come to the conclusion, that with respect to a reasonable balance of performance and implementation complexity, this is a better approach than JNI or shaders.
  • Since API Level 17, there is the ScriptIntrinsicBlur class available from the API. This is exactly what I've been looking for, namely a high level, hardware-accelerated Gaussian blur implementation.
  • ScriptIntrinsicBlur is now a part of the android support library (v8) which supports Froyo and above (API>8). The android developer blog post on the support RenderScript library has some basic tips on how to use it.

However, the documentation on the ScriptIntrinsicBlur class is very rare and I've spent some more time on figuring out the correct invocation arguments. For bluring an ordinary ARGB_8888-typed bitmap named photo, here they are:

final RenderScript rs = RenderScript.create( myAndroidContext );
final Allocation input = Allocation.createFromBitmap( rs, photo, 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( myBlurRadius /* e.g. 3.f */ );
script.setInput( input );
script.forEach( output );
output.copyTo( photo );
7
  • 2
    @thV0ID : Nice answer. is there any way to make it backward compatible that can work in ICS also? Commented Jul 13, 2013 at 7:03
  • @BhaveshPatadiya: You could implement fast convolution with RenderScript and use a Gaussian kernel. An example on implementing convolution with RenderScript can be found here: stackoverflow.com/a/10257994/1444073
    – kostrykin
    Commented Jul 31, 2013 at 19:39
  • 3
    RS is now part of the compatibility lib, renderscript-v8.jar
    – Gabor
    Commented Sep 24, 2013 at 15:44
  • 1
    The support for renderscript is explained here: developer.android.com/tools/support-library/features.html Commented Sep 25, 2013 at 2:12
  • 1
    This works but takes about 2 seconds to blur a 137KB image with radius = 25 on a Galaxy S 3. :( Commented Mar 27, 2014 at 22:23
13

Probably the most demanding requirement is live blur, meaning you blur live as the view changes. In this situation a blur should not take longer than 10 or so ms (to have some playroom onto the 16ms/60fps) to look smooth. It is possible to achieve this effect with the right settings, even on not so high end devices (galaxy s3 and even slower).

Here is how to improve performance in descending importance:

  1. Use downscaled images: This decreases the pixels to blur enormously. Also it works for you when you want a real blurred image. Also image loading and memory consumption is drastically lowered.

  2. Use Renderscript ScriptIntrinsicBlur - there is probably not a better/faster solution in Android as of 2014. One mistake I often see is that the Renderscript context is not reused, but created everytime the blur algorithm is used. Mind you that RenderScript.create(this); takes around 20ms on a Nexus 5, so you want to avoid this.

  3. Reuse Bitmaps: don't create unnecessary instances and always use the same instance. When you need really fast blur, garbage collection plays a major role (taking a good 10-20 ms for collection some bitmaps). Also crop and blur only what you need.

  4. For a live blur, probably because of context switching, it's not possible to blur in another thread (even with threadpools), only the main thread was fast enough to keep the view updated timely, with threads I saw lags of 100-300ms

on more tips see my other post here https://stackoverflow.com/a/23119957/774398

btw. I did a simple live blur in this app: github, Playstore

5
  • About #2 , I don't see that it has such good quality of blurring (see here: stackoverflow.com/q/36447630/878126) . Also, Do you really suggest that the Renderscript object will stay for the whole app lifecycle, without calling "destroy()" on it, ever ? Commented Apr 6, 2016 at 9:58
  • Hi, I think you found the error for the bad quality already in your post (I was going to say I cannot reporduce your bad output). For the second point: no of course destroy the context if you done blurring. Just keep in mind that creating the context is expensive
    – Patrick
    Commented Apr 6, 2016 at 13:02
  • The bad quality - I'm not sure the reason (because I used other samples and tutorials codes), but I've used "createTyped" and now it works. If you can write there about why I had those issues, it would be great. About "destroy the context", I'm talking about RenderScript.create. If I blur images in a lot of places in the app, it should be ok to never destroy it, right? Also, does it matter which context I use for its argument? Commented Apr 6, 2016 at 14:04
  • Im not familiar with renderscript details, msg one of these guys to answer your question (they work on the google renderscript team): stackoverflow.com/users/1988713/tim-murray or stackoverflow.com/users/1819933/r-jason-sams. I guess it does not matter which context you use (I think its only used for logistics e.g. getting the defined folder for the renderscript files). For Renderscript lifecycle: In the app you linked I made, I did destroy the context in onPause(). But it depends on your use case, if you just blur 1 image and put it in a cache, you very well destroy immedeatly
    – Patrick
    Commented Apr 6, 2016 at 15:22
  • ok thanks. But will it be a memory leak if I even give it an activity as an argument, and yet the instance to the Renderscript object remains even after activity was destroyed? Commented Apr 6, 2016 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.