Skip to content

Instantly share code, notes, and snippets.

@hwshim0810 hwshim0810/blur.kt
Created Jul 3, 2018

Embed
What would you like to do?
android image blur
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
fun blur(context: Context, sourceBitmap: Bitmap, radius: Float): Bitmap {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
val bitmap = sourceBitmap.copy(sourceBitmap.config, true)
val rs = RenderScript.create(context)
val input = Allocation.createFromBitmap(rs, sourceBitmap,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT)
val output = Allocation.createTyped(rs, input.type)
ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)).run {
setRadius(radius) // 0.0f - 25.0f
setInput(input)
forEach(output)
output.copyTo(bitmap)
}
return bitmap
}
return sourceBitmap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.