高斯模糊

方案如下:

采用RenderScript的方式,他提供了java可調(diào)用的api,是C處理,效率高,但是是api17引入,之后的需要兼容包,兼容包160K。三思- -。

直接貼代碼不多解釋了:

public Bitmap rsblur(Bitmap bitmap, Context context){

? ? ? ? ? int width = bitmap.getWidth();

? ? ? ? ? int height = bitmap.getHeight();

? ? ? ? ? //先優(yōu)化bitmap使他像素點降低

? ? ? ? ? int nw = Math.round(width * 1 / 8);

? ? ? ? ? int nh = Math.round(height * 1 / 8);

? ? ? ? ? Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, nw, nh, false);

? ? ? ? ? ?RenderScript renderScript = RenderScript.create(context);

? ? ? ? ? Allocation fromBitmap = Allocation.createFromBitmap(renderScript, scaledBitmap);

? ? ? ? ? Allocation out = Allocation.createTyped(renderScript, fromBitmap.getType());

? ? ? ? ? ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));

? ? ? ? ? scriptIntrinsicBlur.setInput(fromBitmap);

//0-25

? ? ? ? ? scriptIntrinsicBlur.setRadius(10);

? ? ? ? ?scriptIntrinsicBlur.forEach(out);

? ? ? ? ?out.copyTo(scaledBitmap);

? ? ? ? ?renderScript.destroy();

? ? ? return scaledBitmap;

}

,api17以下需要v8兼容包android.support.v8,使用方式如下在build添加:

? ? ? ? ?defaultConfig {

? ? ? ? //使用support v8庫

? ? ? ? renderscriptTargetApi 18

? ? ? ? ?renderscriptSupportModeEnabled true

}

,速度非常之快,推薦= -。之前我用java算法,要將近一秒還容易OOM,這個不錯的

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容