Android: Fast Bitmap Blur

Fast Bitmap Blur For Android SDK

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.

Android: fast bitmap blur?

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 );

Blur changing bitmap permanently

You are just passing the reference of the mainBitmap thats why the mainBitmap changes. You should make a copy of it and then blur it.

Instead of

Bitmap bitmap = activity.mainBitmap;

you should use:

Bitmap bitmap = activity.mainBitmap.copy(activity.mainBitmap.getConfig(),true);

How to blur a Bitmap (Android)?

What you are doing is basically 2D convolution between original image I and kernel K (kernel is actually PSF - point spread function). If your image I is of size m x n, and kernel is of size r x s, for each point of blurred image J you need r x s multiplications, resulting in total m x n x r x s multiplications for the whole image.

Computationally more efficient approach would be to use DFT (Discrete Fourier Transform). Make transforms of the image and of the kernel, and multiply them in the domain of transform, and then revert back via Inverse DFT. In short:

J = IDFT(DFT(I)*DFT(K))

For DFT computation fast algorithms (FFT - Fast Fourier Transform) exist. You can find them in C source on the Internet. In order to use C source, you need to use JNI (Java Native Interface), supported by the Android platform.

Regarding borders, when using DFT you have no issues, since blurring at the border is done circularly (e.g. left border values are calculated using also some right border values).

If you are working with the kernels that may be separated (2D kernel represented as outer product of 1-D kernels), then it becomes more simple. 2D convolution can be represented as 1-D convolutions over rows and then over columns (or vice versa). The same is true for blurring using DFT.

Trouble creating a bitmap blur effect on imageview in android

Off the top of my head, I would try something like the following:

//downscale
Bitmap bmp = Bitmap.createScaledBitmap(originalBitmap, originalBitmap.getWidth()/scale, originalBitmap.getHeight()/scale, true);
//blur
Bitmap blurred = blurAlgorithm.blur(bmp, BLUR_RADIUS);
//draw the blurred image
Canvas c = new Canvas(blurred);
//upscale
c.scale(scale, scale);

For a good blur algorithm for android, have a look at this.
Generally, that project has a lot of info on how to do blurring properly.



Related Topics



Leave a reply



Submit