0

I tried RenderScript for blurring image and it works. I would like to know how RenderScript can be used to blur part of the image. I tried below code but it did not work:

 Bitmap overlay = Bitmap.createBitmap(
            mWidth,
            mHeight,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(overlay);

    canvas.drawBitmap(bitmap, -mletf,
            -mTop, null);

    RenderScript rs = RenderScript.create(mContext);

    Allocation overlayAlloc = Allocation.createFromBitmap(
            rs, overlay);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
            rs, overlayAlloc.getElement());

    blur.setInput(overlayAlloc);

    blur.setRadius(mRadius);

    blur.forEach(overlayAlloc);

    overlayAlloc.copyTo(overlay);




    rs.destroy();
    return  overlay;

Variables mHeight, mWidth are the height and width of the part to be blurred and its mTop, mletf are where blur should start.

BSMP
  • 4,596
  • 8
  • 33
  • 44
png
  • 4,368
  • 7
  • 69
  • 118
  • Possible duplicate of [Blur on touch. Android application](https://stackoverflow.com/questions/18188079/blur-on-touch-android-application) – Tharkius Sep 20 '17 at 20:35

2 Answers2

1

Use the LaunchOptions API:

LaunchOptions lo = new LaunchOptions();
lo.setX(mLeft, mLeft+mWidth);
lo.setY(mTop, mTop+mHeight);

blur.forEach(overlayAlloc, lo);
sakridge
  • 578
  • 3
  • 9
-1

Use this library enter link description here

And

defaultConfig {
applicationId "com.javatechig"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"

// Add the following two lines
renderscriptTargetApi 18
renderscriptSupportModeEnabled true

}

The following code snippets can be used create a bitmap blur effect in Android using RenderScript API.

//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;

public Bitmap blur(Bitmap image) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}

You can use the above code snippet to blur an ImageView as follows.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);
imageView.setImageBitmap(blurredBitmap);

Follow this Link also enter link description here