方法一的原文链接未查询到,对原创作者表示歉意。
目前我知道的有2中办法
第一种,调用Android的API完成,但是支持API17及以上
第二种,Github的方法,每个像素点去做模糊,链接:https://github.com/kikoso/android-stackblur/blob/master/StackBlur/src/com/enrique/stackblur/JavaBlurProcess.java
方法一:
public class RSBlurProcess implements BlurProcess {
private Context context;
public RSBlurProcess(Context context) {
this.context = context;
}
/**
*
* @param original 需要模糊的bitmap
* @param radius 模糊的半径,最大值25
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public Bitmap blur(Bitmap original, float radius) {
long start = System.currentTimeMillis();
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(context);
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, original);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(radius);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
original.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
}
第二种方法代码较多,建议直接去github查看。
建议尽可能不要在客户端做高斯模糊,因为内存开销太大,如果不是特殊需求(如模糊需要动态变化),尽可能直接服务端做模糊。