比較高效的高斯模糊工具類

注意:需要版本大于19才可以正常使用

public classBlurImageview {

//圖片縮放比例

private static final floatBITMAP_SCALE=0.2f;

/**

* 模糊圖片的具體方法

*

*@paramcontext上下文對(duì)象

*@parambit需要模糊的圖片

*@return模糊處理后的圖片

*/

public staticBitmapBlurImages(Context context,Bitmap bit, floatblurRadius) {

Bitmap image =ImageCompressL(bit);

// 計(jì)算圖片縮小后的長(zhǎng)寬

intwidth = Math.round(image.getWidth() *BITMAP_SCALE);

intheight = Math.round(image.getHeight() *BITMAP_SCALE);

// 將縮小后的圖片做為預(yù)渲染的圖片

Bitmap inputBitmap = Bitmap.createScaledBitmap(image,width,height, false);

// 創(chuàng)建一張渲染后的輸出圖片

Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

// 創(chuàng)建RenderScript內(nèi)核對(duì)象

RenderScript rs = RenderScript.create(context);

// 創(chuàng)建一個(gè)模糊效果的RenderScript的工具對(duì)象

ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,Element.U8_4(rs));

// 由于RenderScript并沒(méi)有使用VM來(lái)分配內(nèi)存,所以需要使用Allocation類來(lái)創(chuàng)建和分配內(nèi)存空間

// 創(chuàng)建Allocation對(duì)象的時(shí)候其實(shí)內(nèi)存是空的,需要使用copyTo()將數(shù)據(jù)填充進(jìn)去

Allocation tmpIn = Allocation.createFromBitmap(rs,inputBitmap);

Allocation tmpOut = Allocation.createFromBitmap(rs,outputBitmap);

// 設(shè)置渲染的模糊程度, 25f是最大模糊度

blurScript.setRadius(1);

// 設(shè)置blurScript對(duì)象的輸入內(nèi)存

blurScript.setInput(tmpIn);

// 將輸出數(shù)據(jù)保存到輸出內(nèi)存中

try{

// 實(shí)例化Bitmap

blurScript.forEach(tmpOut);

}catch(OutOfMemoryError e) {

//

}

// 將數(shù)據(jù)填充到Allocation中

tmpOut.copyTo(outputBitmap);

returnoutputBitmap;

}

public staticBitmapImageCompressL(Bitmap bitmap) {

doubletargetwidth = Math.sqrt(10.00*1000);

//? ? ? ? if (bitmap.getWidth() > targetwidth || bitmap.getHeight() > targetwidth) {

// 創(chuàng)建操作圖片用的matrix對(duì)象

Matrix matrix =newMatrix();

// 計(jì)算寬高縮放率

doublex = Math.max(targetwidth / bitmap.getWidth(),targetwidth

/ bitmap.getHeight());

// 縮放圖片動(dòng)作

matrix.postScale((float) x,(float) x);

bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),

bitmap.getHeight(),matrix, true);

//? ? ? ? }

returnbitmap;

}

}

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

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

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