How to Use Render Scripts in Android

If you are an android developer, you must have faced the problem of slowing down or even crash of your app while implementing complex image processing operations? I faced the same and found RenderScripts as appropriate solution to it.

The RenderScript are used in android for complex computations such as image processing. The Scripts are based on the C99 standard of C language. RenderScripts was introduced in API 11 i.e. Honeycomb.

For accessing RenderScript in android there are two API’s

1. Android.renderscript :- from API 11(HoneyComb) to higher API’s.
2. Android.support.v8.renderscript :- from API 8(Froyo) to higher.

For accessing these API’s check the following details first

1.Android SDK Tools must be 22.2 or higher.(If not open SDK manager and in tools select Android SDK Tools then click on install).
2.Android SDK build tools must be 18.1.0 or higher.(If not open SDK manager and in tools select Android SDK build tools  then click on install).

Now for using the renderscripts in your app do the following

*Open project properties and add the following

renderscript.target=19
renderscript.support.mode=true
sdk.buildtools=18.1.0

Now after all configurations let’s move on to some coding. There are 2 Bitmaps involved during the image processing

  1. Bitmap InputBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);.
  2. Bitmap OutputBitmap = InputBitmap.copy(InputBitmap.getConfig(), true);

After initializing the input and output bitmap initialize the RenderScript.

RenderScript renderScript = RenderScript.create(this);

Now you need Input and output Allocations through which the data is passed to and from the Kernal(RenderScript contains methods which is called Kernal).

Allocation inputAllocation = Allocation.createFromBitmap(renderScript, InputBitmap);

Allocation outputAllocation = Allocation.createTyped(renderScript,inputAllocation.getType());

The whole method would look like,

 

 /**
	 * Applying the Blur Effect on an Image
	 * @param image
	 */
	private void blurEffect(ImageView image)
	{
		//Input Bitmap 
		Bitmap InputBitmap = BitmapFactory.decodeResource(getResources(),InputBitmap.drawable.image);
		//Output Bitmap
		Bitmap OutputBitmap = InputBitmap.copy(InputBitmap.getConfig(), true);
		//RenderScript Initialization 
		RenderScript renderScript = RenderScript.create(this);
		
		Allocation inputAllocation = Allocation.createFromBitmap(renderScript, InputBitmap);
		Allocation outputAllocation= Allocation.createTyped(renderScript,inputAllocation.getType());

		//Blur Effect on Image using 
		ScriptIntrinsicBlur blurEffect = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
		//Specified radius on which blur effect will work.
		blurEffect.setRadius(6);
		blurEffect.setInput(inputAllocation);
		blurEffect.forEach(outputAllocation);
		outputAllocation.copyTo(OutputBitmap);
		//set image to the imageview
		image.setImageBitmap(OutputBitmap);
	}

Use this method in your activity applied the method to blur the image and the result was something like this:

Blur effect on the image

Blur effect on the image

 

I hope you find it helpful. Thanks for reading 🙂

Presenter: Ankit Khare, Mobile developer, Mindfire Solutions

Advertisements

4 thoughts on “How to Use Render Scripts in Android

  1. Pingback: How to Use Render Scripts in Android | khareankit0804

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s