1. 程式人生 > >Android 圖片模糊、高斯模糊、毛玻璃的三種實現方法

Android 圖片模糊、高斯模糊、毛玻璃的三種實現方法

轉載自:http://blog.csdn.net/fan7983377/article/details/51568059

效果圖:

這裡寫圖片描述

原文連結:點選訪問

這使用也很簡單,匯入依賴,使用模糊方法就行,就這兩步搞定

依賴:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'net.qiujuer.genius:blur:2.0.0-beta4'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

實現方法有三種,第一個是Java實現的,第二個和第三個是呼叫C語言實現的 ,具體的區別也就是程式碼執行的耗時操作時間,從圖片中可以看出java使用時間遠大於c執行的時間。

可以通過改變radius的值來改變模糊度,值越大,模糊度越大,radius<=0時則圖片不顯示;一般radius的值以20左右為佳!

// Java
Bitmap newBitmap = StackBlur.blur(mBitmap, (int) radius, false);

// Bitmap JNI Native
Bitmap newBitmap = StackBlur.blurNatively(mBitmap, (int) radius, false);

// Pixels JNI Native
Bitmap newBitmap = StackBlur.blurNativelyPixels(mBitmap, (int) radius, false);
另外附上 ImageView 轉換 Bitmap 的方法
img_f = (ImageView) findViewById(R.id.img_f);
img_new = (ImageView) findViewById(R.id.img_new);
Bitmap mBitmap = ((BitmapDrawable) ((ImageView) img_f).getDrawable()).getBitmap();
// Bitmap JNI Native
Bitmap newBitmap = StackBlur.blurNatively(mBitmap, (int) 50, false);
img_new
.setImageBitmap(newBitmap);