Skip to content

Commit

Permalink
RenderScript use when API > 16.
Browse files Browse the repository at this point in the history
Calculation time is 2 times faster in this case.
  • Loading branch information
nicolas authored and nicolas committed Jun 23, 2013
1 parent 6a39fda commit 18565cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 24 additions & 1 deletion BlurEffect/src/com/npi/blureffect/Blur.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.npi.blureffect;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build.VERSION;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.Log;

public class Blur {

private static final String TAG = "Blur";

public static Bitmap fastblur(Bitmap sentBitmap, int radius) {
@SuppressLint("NewApi")
public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {

if (VERSION.SDK_INT > 16) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 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(radius /* e.g. 3.f */);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}

// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
Expand Down
4 changes: 2 additions & 2 deletions BlurEffect/src/com/npi/blureffect/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void run() {

// No image found => let's generate it!
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inSampleSize = 2;
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image, options);
Bitmap newImg = Blur.fastblur(image, 12);
Bitmap newImg = Blur.fastblur(MainActivity.this, image, 12);
ImageUtils.storeImage(newImg, blurredImage);
runOnUiThread(new Runnable() {

Expand Down

0 comments on commit 18565cc

Please sign in to comment.