Last active
July 23, 2019 00:26
-
-
Save mohanen/f8cb9175c2a6af9d3343a87df002c933 to your computer and use it in GitHub Desktop.
Android Image blur beyond renderscript blur radius
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
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