java - Fast Bitmap Blur For Android SDK

ID : 20184

viewed : 30

Tags : javaandroidimage-processingrenderscriptjava

Top 5 Answer for java - Fast Bitmap Blur For Android SDK

vote vote

91

For future Googlers, here is an algorithm that I ported from Quasimondo. It's kind of a mix between a box blur and a gaussian blur, it's very pretty and quite fast too.

Update for people encountering the ArrayIndexOutOfBoundsException problem : @anthonycr in the comments provides this information :

I found that by replacing Math.abs with StrictMath.abs or some other abs implementation, the crash does not occur.

/**  * Stack Blur v1.0 from  * http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html  * Java Author: Mario Klingemann <mario at quasimondo.com>  * http://incubator.quasimondo.com  *  * created Feburary 29, 2004  * Android port : Yahel Bouaziz <yahel at kayenko.com>  * http://www.kayenko.com  * ported april 5th, 2012  *  * This is a compromise between Gaussian Blur and Box blur  * It creates much better looking blurs than Box Blur, but is  * 7x faster than my Gaussian Blur implementation.  *  * I called it Stack Blur because this describes best how this  * filter works internally: it creates a kind of moving stack  * of colors whilst scanning through the image. Thereby it  * just has to add one new block of color to the right side  * of the stack and remove the leftmost color. The remaining  * colors on the topmost layer of the stack are either added on  * or reduced by one, depending on if they are on the right or  * on the left side of the stack.  *    * If you are using this algorithm in your code please add  * the following line:  * Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>  */  public Bitmap fastblur(Bitmap sentBitmap, float scale, int radius) {      int width = Math.round(sentBitmap.getWidth() * scale);     int height = Math.round(sentBitmap.getHeight() * scale);     sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);      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 < 256 * 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; x++) {              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; x++) {         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] = ( 0xff000000 & 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); } 
vote vote

87

Android Blur Guide 2016

with Showcase/Benchmark App and Source on Github. Also check out the Blur framework I'm currently working on: Dali.

After experimenting a lot I can now safely give you some solid recommendations that will make your life easier in Android when using the Android Framework.

Load and Use a downscaled Bitmap (for very blurry images)

Never use a the full size of a Bitmap. The bigger the image the more needs to be blurred and also the higher the blur radius needs to be and usually, the higher the blur radius the longer the algorithm takes.

final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap blurTemplate = BitmapFactory.decodeResource(getResources(), R.drawable.myImage, options); 

This will load the bitmap with inSampleSize 8, so only 1/64 of the original image. Test what inSampleSize suits your needs, but keep it 2^n (2,4,8,...) to avoid degrading quality due to scaling. See Google doc for more

Another really big advantage is that bitmap loading will be really fast. In my early blur testing I figured that the longest time during the whole blur process was the image loading. So to load a 1920x1080 image from disk my Nexus 5 needed 500ms while the blurring only took another 250 ms or so.

Use Renderscript

Renderscript provides ScriptIntrinsicBlur which is a Gaussian blur filter. It has good visual quality and is just the fastest you realistically get on Android. Google claims to be "typically 2-3x faster than a multithreaded C implementation and often 10x+ faster than a Java implementation". Renderscript is really sophisticated (using the fastest processing device (GPU, ISP, etc.), etc.) and there is also the v8 support library for it making it compatible down to 2.2. Well at least in theory, through my own tests and reports from other devs it seems that it is not possible to use Renderscript blindly, since the hardware/driver fragmentation seems to cause problems with some devices, even with higher sdk lvl (e.g. I had troubles with the 4.1 Nexus S) so be careful and test on a lot of devices. Here's a simple example that will get you started:

//define this only once if blurring multiple times RenderScript rs = RenderScript.create(context);  (...) //this will blur the bitmapOriginal with a radius of 8 and save it in bitmapOriginal final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(8f); script.setInput(input); script.forEach(output); output.copyTo(bitmapOriginal); 

When using the v8 support with Gradle, which is specifically recommended by Google "because they include the latest improvements", you only need to add 2 lines to your build script and use android.support.v8.renderscript with current build tools (updated syntax for android Gradle plugin v14+)

android {     ...     defaultConfig {         ...         renderscriptTargetApi 19         renderscriptSupportModeEnabled true     } } 

Simple benchmark on a Nexus 5 - comparing RenderScript with different other java and Renderscript implementations:

The average runtime per blur on different pic sizes The average runtime per blur on different pic sizes

Megapixels per sec that can be blurred Megapixels per sec that can be blurred

Each value is the avg of 250 rounds. RS_GAUSS_FAST is ScriptIntrinsicBlur (and nearly always the fastest), others that start with RS_ are mostly convolve implementations with simple kernels. The details of the algorithms can be found here. This is not purely blurring, since a good portion is garbage collection that is measured. This can be seen in this here (ScriptIntrinsicBlur on a 100x100 image with about 500 rounds)

enter image description here

The spikes are gc.

You can check for yourself, the benchmark app is in the playstore: BlurBenchmark

Reuses Bitmap wherever possible (if prio: performance > memory footprint)

If you need multiple blurs for a live blur or similar and your memory allows it do not load the bitmap from drawables multiple times, but keep it "cached" in a member variable. In this case always try to use the same variables, to keep garbage collecting to a minimum.

Also check out the new inBitmap option when loading from a file or drawable which will reuse the bitmap memory and save garbage collection time.

For blending from sharp to blurry

The simple and naive method is just to use 2 ImageViews, one blurred, and alpha fade them. But if you want a more sophisticated look that smoothly fades from sharp to blurry, then check out Roman Nurik's post about how to do it like in his Muzei app.

Basically he explains that he pre-blurs some frames with different blur extents and uses them as keyframes in an animation that looks really smooth.

Diagram where Nurik exaplains his approach

vote vote

70

This is a shot in the dark, but you might try shrinking the image and then enlarging it again. This can be done with Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter). Make sure and set the filter parameter to true. It'll run in native code so it might be faster.

vote vote

61

EDIT (April 2014): This is a question/answer page that still gets a lot of hits it seems. I know I'm always getting upvotes for this post. But if you're reading this, you need to realize the answers posted here (both mine and the accepted answer) are out of date. If you want to implement efficient blur today, you should use RenderScript instead of the NDK or Java. RenderScript runs on Android 2.2+ (using the Android Support Library), so there's no reason not to use it.

The old answer follows, but beware as it's outdated.


For futureĀ² Googlers, here is an algorithm that I ported from Yahel's port of Quasimondo's algorithm, but using the NDK. It's based on Yahel's answer, of course. But this is running native C code, so it's faster. Much faster. Like, 40 times faster.

I find that using the NDK is how all image manipulation should be done on Android... it's somewhat annoying to implement at first (read a great tutorial on using JNI and the NDK here), but much better, and near real time for a lot of things.

For reference, using Yahel's Java function, it took 10 seconds to blur my 480x532 pixels image with a blur radius of 10. But it took 250ms using the native C version. And I'm pretty sure it can still be further optimized... I just did a dumb conversion of the java code, there's probably some manipulations that can be shortened, didn't want to spend too much time refactoring the whole thing.

#include <jni.h> #include <string.h> #include <math.h> #include <stdio.h> #include <android/log.h> #include <android/bitmap.h>  #define LOG_TAG "libbitmaputils" #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  typedef struct {     uint8_t red;     uint8_t green;     uint8_t blue;     uint8_t alpha; } rgba;  JNIEXPORT void JNICALL Java_com_insert_your_package_ClassName_functionToBlur(JNIEnv* env, jobject obj, jobject bitmapIn, jobject bitmapOut, jint radius) {     LOGI("Blurring bitmap...");      // Properties     AndroidBitmapInfo   infoIn;     void*               pixelsIn;     AndroidBitmapInfo   infoOut;     void*               pixelsOut;      int ret;      // Get image info     if ((ret = AndroidBitmap_getInfo(env, bitmapIn, &infoIn)) < 0 || (ret = AndroidBitmap_getInfo(env, bitmapOut, &infoOut)) < 0) {         LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);         return;     }      // Check image     if (infoIn.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || infoOut.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {         LOGE("Bitmap format is not RGBA_8888!");         LOGE("==> %d %d", infoIn.format, infoOut.format);         return;     }      // Lock all images     if ((ret = AndroidBitmap_lockPixels(env, bitmapIn, &pixelsIn)) < 0 || (ret = AndroidBitmap_lockPixels(env, bitmapOut, &pixelsOut)) < 0) {         LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);     }      int h = infoIn.height;     int w = infoIn.width;      LOGI("Image size is: %i %i", w, h);      rgba* input = (rgba*) pixelsIn;     rgba* output = (rgba*) pixelsOut;      int wm = w - 1;     int hm = h - 1;     int wh = w * h;     int whMax = max(w, h);     int div = radius + radius + 1;      int r[wh];     int g[wh];     int b[wh];     int rsum, gsum, bsum, x, y, i, yp, yi, yw;     rgba p;     int vmin[whMax];      int divsum = (div + 1) >> 1;     divsum *= divsum;     int dv[256 * divsum];     for (i = 0; i < 256 * divsum; i++) {         dv[i] = (i / divsum);     }      yw = yi = 0;      int stack[div][3];     int stackpointer;     int stackstart;     int rbs;     int ir;     int ip;     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 = input[yi + min(wm, max(i, 0))];              ir = i + radius; // same as sir              stack[ir][0] = p.red;             stack[ir][1] = p.green;             stack[ir][2] = p.blue;             rbs = r1 - abs(i);             rsum += stack[ir][0] * rbs;             gsum += stack[ir][1] * rbs;             bsum += stack[ir][2] * rbs;             if (i > 0) {                 rinsum += stack[ir][0];                 ginsum += stack[ir][1];                 binsum += stack[ir][2];             } else {                 routsum += stack[ir][0];                 goutsum += stack[ir][1];                 boutsum += stack[ir][2];             }         }         stackpointer = radius;          for (x = 0; x < w; x++) {              r[yi] = dv[rsum];             g[yi] = dv[gsum];             b[yi] = dv[bsum];              rsum -= routsum;             gsum -= goutsum;             bsum -= boutsum;              stackstart = stackpointer - radius + div;             ir = stackstart % div; // same as sir              routsum -= stack[ir][0];             goutsum -= stack[ir][1];             boutsum -= stack[ir][2];              if (y == 0) {                 vmin[x] = min(x + radius + 1, wm);             }             p = input[yw + vmin[x]];              stack[ir][0] = p.red;             stack[ir][1] = p.green;             stack[ir][2] = p.blue;              rinsum += stack[ir][0];             ginsum += stack[ir][1];             binsum += stack[ir][2];              rsum += rinsum;             gsum += ginsum;             bsum += binsum;              stackpointer = (stackpointer + 1) % div;             ir = (stackpointer) % div; // same as sir              routsum += stack[ir][0];             goutsum += stack[ir][1];             boutsum += stack[ir][2];              rinsum -= stack[ir][0];             ginsum -= stack[ir][1];             binsum -= stack[ir][2];              yi++;         }         yw += w;     }     for (x = 0; x < w; x++) {         rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;         yp = -radius * w;         for (i = -radius; i <= radius; i++) {             yi = max(0, yp) + x;              ir = i + radius; // same as sir              stack[ir][0] = r[yi];             stack[ir][1] = g[yi];             stack[ir][2] = b[yi];              rbs = r1 - abs(i);              rsum += r[yi] * rbs;             gsum += g[yi] * rbs;             bsum += b[yi] * rbs;              if (i > 0) {                 rinsum += stack[ir][0];                 ginsum += stack[ir][1];                 binsum += stack[ir][2];             } else {                 routsum += stack[ir][0];                 goutsum += stack[ir][1];                 boutsum += stack[ir][2];             }              if (i < hm) {                 yp += w;             }         }         yi = x;         stackpointer = radius;         for (y = 0; y < h; y++) {             output[yi].red = dv[rsum];             output[yi].green = dv[gsum];             output[yi].blue = dv[bsum];              rsum -= routsum;             gsum -= goutsum;             bsum -= boutsum;              stackstart = stackpointer - radius + div;             ir = stackstart % div; // same as sir              routsum -= stack[ir][0];             goutsum -= stack[ir][1];             boutsum -= stack[ir][2];              if (x == 0) vmin[y] = min(y + r1, hm) * w;             ip = x + vmin[y];              stack[ir][0] = r[ip];             stack[ir][1] = g[ip];             stack[ir][2] = b[ip];              rinsum += stack[ir][0];             ginsum += stack[ir][1];             binsum += stack[ir][2];              rsum += rinsum;             gsum += ginsum;             bsum += binsum;              stackpointer = (stackpointer + 1) % div;             ir = stackpointer; // same as sir              routsum += stack[ir][0];             goutsum += stack[ir][1];             boutsum += stack[ir][2];              rinsum -= stack[ir][0];             ginsum -= stack[ir][1];             binsum -= stack[ir][2];              yi += w;         }     }      // Unlocks everything     AndroidBitmap_unlockPixels(env, bitmapIn);     AndroidBitmap_unlockPixels(env, bitmapOut);      LOGI ("Bitmap blurred."); }  int min(int a, int b) {     return a > b ? b : a; }  int max(int a, int b) {     return a > b ? a : b; } 

Then use it like this (considering a class called com.insert.your.package.ClassName and a native function called functionToBlur, as the code above states):

// Create a copy Bitmap bitmapOut = bitmapIn.copy(Bitmap.Config.ARGB_8888, true); // Blur the copy functionToBlur(bitmapIn, bitmapOut, __radius); 

It expects a RGB_8888 bitmap!

To use a RGB_565 bitmap, either create a converted copy before passing the parameter (yuck), or change the function to use a new rgb565 type instead of rgba:

typedef struct {     uint16_t byte0; } rgb565; 

The problem is that if you do that you can't read .red, .green and .blue of the pixel anymore, you need to read the byte properly, duh. When I needed that before, I did this:

r = (pixels[x].byte0 & 0xF800) >> 8; g = (pixels[x].byte0 & 0x07E0) >> 3; b = (pixels[x].byte0 & 0x001F) << 3; 

But there's probably some less dumb way of doing it. I'm not much of a low-level C coder, I'm afraid.

vote vote

54

This code is work perfect for me

Bitmap tempbg = BitmapFactory.decodeResource(getResources(),R.drawable.b1); //Load a background. Bitmap final_Bitmap = BlurImage(tempbg);   @SuppressLint("NewApi") Bitmap BlurImage (Bitmap input) {     try     {     RenderScript  rsScript = RenderScript.create(getApplicationContext());     Allocation alloc = Allocation.createFromBitmap(rsScript, input);      ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript,   Element.U8_4(rsScript));     blur.setRadius(21);     blur.setInput(alloc);      Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Bitmap.Config.ARGB_8888);     Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);      blur.forEach(outAlloc);     outAlloc.copyTo(result);      rsScript.destroy();     return result;     }     catch (Exception e) {         // TODO: handle exception         return input;     }  } 

Top 3 video Explaining java - Fast Bitmap Blur For Android SDK

Related QUESTION?