Blur image in android using code

add in Build.gradle file : -

defaultConfig {
        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true
    }

Add in Activity : -

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

 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 = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
            theIntrinsic.setRadius(BLUR_RADIUS);
            theIntrinsic.setInput(tmpIn);
            theIntrinsic.forEach(tmpOut);
        }

        tmpOut.copyTo(outputBitmap);
        return outputBitmap;
    }

Comments

Popular posts from this blog

How to Fix ‘This file name is too long’ Error?

How to change searchview property to change searchview design

Library To Load Html Content In Android TextView in Android