Created
July 3, 2018 15:56
-
-
Save hwshim0810/bf9a64f6142cb51e2112ab93c64e4555 to your computer and use it in GitHub Desktop.
android image blur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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