Skip to content

Instantly share code, notes, and snippets.

@j67678
Forked from mohanen/BlurEffect.kt
Created July 23, 2019 00:26
Show Gist options
  • Save j67678/84f52ff10320c0127fd0f50052c82adb to your computer and use it in GitHub Desktop.
Save j67678/84f52ff10320c0127fd0f50052c82adb to your computer and use it in GitHub Desktop.
Android Image blur beyond renderscript blur radius
package com.example.ziro.blur
/**
* Created by ziro on 19/11/17.
*/
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.renderscript.Allocation
import android.renderscript.Element.U8_4
import android.renderscript.ScriptIntrinsicBlur
import android.renderscript.RenderScript
import android.view.View
public class BlurEffect {
//scales down image, blur it, scale it up
fun blur (context : Context, image : Bitmap , scalingFactor : Float = 1f, blurRadius : Float = 1f) : Bitmap {
var width : Int = Math.round(image.width * scalingFactor)
var height : Int = Math.round(image.height * scalingFactor)
var inputBitmap : Bitmap = Bitmap.createScaledBitmap(image , width, height, false)
var outputBitmap : Bitmap = Bitmap.createBitmap(inputBitmap)
var rs : RenderScript = RenderScript.create(context)
var intrinsicBlur : ScriptIntrinsicBlur = ScriptIntrinsicBlur.create(rs, U8_4(rs))
var tmpIn : Allocation = Allocation.createFromBitmap(rs, inputBitmap)
var tmpOut : Allocation = Allocation.createFromBitmap(rs, outputBitmap)
intrinsicBlur.setRadius(blurRadius)
intrinsicBlur.setInput(tmpIn)
intrinsicBlur.forEach(tmpOut)
tmpOut.copyTo(outputBitmap)
return Bitmap.createBitmap(Bitmap.createScaledBitmap(outputBitmap,image.width, image.height,false))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment