Create blur bitmap using RenderScript and ScriptIntrinsicBlur

ScriptIntrinsicBlur, added in API Level 17, is a Intrinsic Gausian blur filter. Applies a gaussian blur of the specified radius to all elements of an allocation.

Here is a example show how to create blur bitmap with RenderScript and ScriptIntrinsicBlur.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;
SeekBar seekbarBlurRadius;

Bitmap bitmapOriginal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bitmapOriginal);

// create blur bitmaps
image2.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, 25.0f));
image3.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, 25.0f));

seekbarBlurRadius = (SeekBar)findViewById(R.id.blurradius);

seekbarBlurRadius.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
float radius = (float)seekbarBlurRadius.getProgress();
image2.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, radius));
image3.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, radius));
}});

}

private Bitmap createBitmap_ScriptIntrinsicBlur(Bitmap src, float r) {

//Radius range (0 < r <= 25)
if(r <= 0){
r = 0.1f;
}else if(r > 25){
r = 25.0f;
}

Bitmap bitmap = Bitmap.createBitmap(
src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);

RenderScript renderScript = RenderScript.create(this);

Allocation blurInput = Allocation.createFromBitmap(renderScript, src);
Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
Element.U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(r);
blur.forEach(blurOutput);

blurOutput.copyTo(bitmap);
renderScript.destroy();
return bitmap;
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/blurradius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="25"
android:progress="25" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />


</LinearLayout>