本文实例为大家分享了本地图片或者网络图片高斯模糊效果(毛玻璃效果),具体内容如下
首先看效果图
1.本地图片高斯模糊
2.网络图片高斯模糊
github网址:https://github.com/qiushi123/blurimageqcl
下面是使用步骤
一、实现本地图片或者网络图片的毛玻璃效果特别方便,只需要把下面的fastblurutil类复制到你的项目中就行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
package com.testdemo.blur_image_lib10; import android.graphics.bitmap; import android.graphics.bitmapfactory; import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.url; /** * created by qcl on 14/7/15. */ public class fastblurutil { /** * 根据imagepath获取bitmap */ /** * 得到本地或者网络上的bitmap url - 网络或者本地图片的绝对路径,比如: * <p> * a.网络路径: url="http://blog.foreverlove.us/girl2.png" ; * <p> * b.本地路径:url="file://mnt/sdcard/photo/image.png"; * <p> * c.支持的图片格式 ,png, jpg,bmp,gif等等 * * @param url * @return */ public static int io_buffer_size = 2 * 1024 ; public static bitmap geturlbitmap(string url, int scaleratio) { int blurradius = 8 ; //通常设置为8就行。 if (scaleratio <= 0 ) { scaleratio = 10 ; } bitmap originbitmap = null ; inputstream in = null ; bufferedoutputstream out = null ; try { in = new bufferedinputstream( new url(url).openstream(), io_buffer_size); final bytearrayoutputstream datastream = new bytearrayoutputstream(); out = new bufferedoutputstream(datastream, io_buffer_size); copy(in, out); out.flush(); byte [] data = datastream.tobytearray(); originbitmap = bitmapfactory.decodebytearray(data, 0 , data.length); bitmap scaledbitmap = bitmap.createscaledbitmap(originbitmap, originbitmap.getwidth() / scaleratio, originbitmap.getheight() / scaleratio, false ); bitmap blurbitmap = doblur(scaledbitmap, blurradius, true ); return blurbitmap; } catch (ioexception e) { e.printstacktrace(); return null ; } } private static void copy(inputstream in, outputstream out) throws ioexception { byte [] b = new byte [io_buffer_size]; int read; while ((read = in.read(b)) != - 1 ) { out.write(b, 0 , read); } } // 把本地图片毛玻璃化 public static bitmap toblur(bitmap originbitmap, int scaleratio) { // int scaleratio = 10; // 增大scaleratio缩放比,使用一样更小的bitmap去虚化可以到更好的得模糊效果,而且有利于占用内存的减小; int blurradius = 8 ; //通常设置为8就行。 //增大blurradius,可以得到更高程度的虚化,不过会导致cpu更加intensive /* 其中前三个参数很明显,其中宽高我们可以选择为原图尺寸的1/10; 第四个filter是指缩放的效果,filter为true则会得到一个边缘平滑的bitmap, 反之,则会得到边缘锯齿、pixelrelated的bitmap。 这里我们要对缩放的图片进行虚化,所以无所谓边缘效果,filter=false。*/ if (scaleratio <= 0 ) { scaleratio = 10 ; } bitmap scaledbitmap = bitmap.createscaledbitmap(originbitmap, originbitmap.getwidth() / scaleratio, originbitmap.getheight() / scaleratio, false ); bitmap blurbitmap = doblur(scaledbitmap, blurradius, true ); return blurbitmap; } public static bitmap doblur(bitmap sentbitmap, int radius, boolean canreuseinbitmap) { bitmap bitmap; if (canreuseinbitmap) { bitmap = sentbitmap; } else { bitmap = sentbitmap.copy(sentbitmap.getconfig(), true ); } if (radius < 1 ) { return ( null ); } int w = bitmap.getwidth(); int h = bitmap.getheight(); int [] pix = new int [w * h]; bitmap.getpixels(pix, 0 , w, 0 , 0 , w, h); int wm = w - 1 ; int hm = h - 1 ; int wh = w * h; int div = radius + radius + 1 ; int r[] = new int [wh]; int g[] = new int [wh]; int b[] = new int [wh]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int vmin[] = new int [math.max(w, h)]; int divsum = (div + 1 ) >> 1 ; divsum *= divsum; int dv[] = new int [ 256 * divsum]; for (i = 0 ; i < 256 * divsum; i++) { dv[i] = (i / divsum); } yw = yi = 0 ; int [][] stack = new int [div][ 3 ]; int stackpointer; int stackstart; int [] sir; int rbs; int r1 = radius + 1 ; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0 ; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0 ; for (i = -radius; i <= radius; i++) { p = pix[yi + math.min(wm, math.max(i, 0 ))]; sir = stack[i + radius]; sir[ 0 ] = (p & 0xff0000 ) >> 16 ; sir[ 1 ] = (p & 0x00ff00 ) >> 8 ; sir[ 2 ] = (p & 0x0000ff ); rbs = r1 - math.abs(i); rsum += sir[ 0 ] * rbs; gsum += sir[ 1 ] * rbs; bsum += sir[ 2 ] * rbs; if (i > 0 ) { rinsum += sir[ 0 ]; ginsum += sir[ 1 ]; binsum += sir[ 2 ]; } else { routsum += sir[ 0 ]; goutsum += sir[ 1 ]; boutsum += sir[ 2 ]; } } stackpointer = radius; for (x = 0 ; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[ 0 ]; goutsum -= sir[ 1 ]; boutsum -= sir[ 2 ]; if (y == 0 ) { vmin[x] = math.min(x + radius + 1 , wm); } p = pix[yw + vmin[x]]; sir[ 0 ] = (p & 0xff0000 ) >> 16 ; sir[ 1 ] = (p & 0x00ff00 ) >> 8 ; sir[ 2 ] = (p & 0x0000ff ); rinsum += sir[ 0 ]; ginsum += sir[ 1 ]; binsum += sir[ 2 ]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1 ) % div; sir = stack[(stackpointer) % div]; routsum += sir[ 0 ]; goutsum += sir[ 1 ]; boutsum += sir[ 2 ]; rinsum -= sir[ 0 ]; ginsum -= sir[ 1 ]; binsum -= sir[ 2 ]; yi++; } yw += w; } for (x = 0 ; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0 ; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = math.max( 0 , yp) + x; sir = stack[i + radius]; sir[ 0 ] = r[yi]; sir[ 1 ] = g[yi]; sir[ 2 ] = b[yi]; rbs = r1 - math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0 ) { rinsum += sir[ 0 ]; ginsum += sir[ 1 ]; binsum += sir[ 2 ]; } else { routsum += sir[ 0 ]; goutsum += sir[ 1 ]; boutsum += sir[ 2 ]; } if (i < hm) { yp += w; } } yi = x; stackpointer = radius; for (y = 0 ; y < h; y++) { // preserve alpha channel: ( 0xff000000 & pix[yi] ) pix[yi] = ( 0xff000000 & pix[yi]) | (dv[rsum] << 16 ) | (dv[gsum] << 8 ) | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[ 0 ]; goutsum -= sir[ 1 ]; boutsum -= sir[ 2 ]; if (x == 0 ) { vmin[y] = math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[ 0 ] = r[p]; sir[ 1 ] = g[p]; sir[ 2 ] = b[p]; rinsum += sir[ 0 ]; ginsum += sir[ 1 ]; binsum += sir[ 2 ]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1 ) % div; sir = stack[stackpointer]; routsum += sir[ 0 ]; goutsum += sir[ 1 ]; boutsum += sir[ 2 ]; rinsum -= sir[ 0 ]; ginsum -= sir[ 1 ]; binsum -= sir[ 2 ]; yi += w; } } bitmap.setpixels(pix, 0 , w, 0 , 0 , w, h); return (bitmap); } } |
二、使用实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package com.testdemo; import android.app.activity; import android.content.res.resources; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.text.textutils; import android.view.view; import android.widget.edittext; import android.widget.imageview; import com.testdemo.blur_image_lib10.fastblurutil; public class mainactivity10_blurimage extends activity { imageview image; edittext edit; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main10_blur_image); image = (imageview) findviewbyid(r.id.image); edit = (edittext) findviewbyid(r.id.edit); findviewbyid(r.id.button2).setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { string pattern = edit.gettext().tostring(); int scaleratio = 0 ; if (textutils.isempty(pattern)) { scaleratio = 0 ; } else if (scaleratio < 0 ) { scaleratio = 10 ; } else { scaleratio = integer.parseint(pattern); } // 获取需要被模糊的原图bitmap resources res = getresources(); bitmap scaledbitmap = bitmapfactory.decoderesource(res, r.drawable.filter); // scaledbitmap为目标图像,10是缩放的倍数(越大模糊效果越高) bitmap blurbitmap = fastblurutil.toblur(scaledbitmap, scaleratio); image.setscaletype(imageview.scaletype.center_crop); image.setimagebitmap(blurbitmap); } }); findviewbyid(r.id.button).setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { //url为网络图片的url,10 是缩放的倍数(越大模糊效果越高) final string pattern = edit.gettext().tostring(); final string url = // "http://imgs.duwu.me/duwu/doc/cover/201601/18/173040803962.jpg"; " http://b.hiphotos.baidu.com/album/pic/item/caef76094b36acafe72d0e667cd98d1000e99c5f.jpg?psign=e72d0e667cd98d1001e93901213fb80e7aec54e737d1b867 " ; new thread( new runnable() { @override public void run() { int scaleratio = 0 ; if (textutils.isempty(pattern)) { scaleratio = 0 ; } else if (scaleratio < 0 ) { scaleratio = 10 ; } else { scaleratio = integer.parseint(pattern); } // 下面的这个方法必须在子线程中执行 final bitmap blurbitmap2 = fastblurutil.geturlbitmap(url, scaleratio); // 刷新ui必须在主线程中执行 app.runonuithread( new runnable() { //这个是我自己封装的在主线程中刷新ui的方法。 @override public void run() { image.setscaletype(imageview.scaletype.center_crop); image.setimagebitmap(blurbitmap2); } }); } }).start(); } }); } } |
下面是上面的布局文件
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<linearlayout xmlns:android= " http://schemas.android.com/apk/res/android " xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <imageview android:id= "@+id/image2" android:layout_width= "match_parent" android:layout_height= "220dp" android:background= "@drawable/filter" /> <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" > <edittext android:id= "@+id/edit" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "15dp" android:hint= "输入模糊度" /> <button android:id= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "转化毛玻璃" /> <button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "4dp" android:text= "转化网络图片毛玻璃" /> </linearlayout> <imageview android:id= "@+id/image" android:layout_width= "match_parent" android:layout_height= "220dp" android:layout_below= "@+id/image2" /> </linearlayout> |
三、注意事项
1.一定不要忘记intent权限
2.加载网络图片时一定要在子线程中执行。
github网址:https://github.com/qiushi123/blurimageqcl
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。