Skip to content

Instantly share code, notes, and snippets.

@hwshim0810
Created July 3, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwshim0810/bf9a64f6142cb51e2112ab93c64e4555 to your computer and use it in GitHub Desktop.
Save hwshim0810/bf9a64f6142cb51e2112ab93c64e4555 to your computer and use it in GitHub Desktop.
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