Fork me on GitHub

NDK实现毛玻璃效果

前几天准备做一个动态的毛玻璃效果,中间查阅了很多网上的资料,我用的方法是CMake做NDK,由C/C++来实现图片的毛玻璃化,废话不多说,具体方法如下

首先,当然是配置NDK环境,这类网上教程很多,就麻烦自行配制了,参考教程如下

http://blog.csdn.net/xiaoyu_93/article/details/53082088

这里配置好之后我们还需要在gradle中配置一下NDK开发所需要的库

1
2
3
4
5
6
7
8
9
10
11
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
ndk {
moduleName "blur_lib" // 动态库名称
abiFilters "armeabi", "armeabi-v7a", "x86" // 相应的架构平台
ldLibs "log" //log输出
ldLibs "jnigraphics" //graphic相关jni
}
}
来编写C/C++文件了,我是直接在系统默认的C++库中做,也就是Native-lib.cpp,代码如下
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
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_pecoo_blurjnidemo_NativeHelper */
#ifndef _Included_com_pecoo_blurjnidemo_NativeHelper
#define _Included_com_pecoo_blurjnidemo_NativeHelper
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_pecoo_blurjnidemo_NativeHelper
* Method: blurBitmap
* Signature: (Ljava/lang/Object;I)V
*/
#include <android/log.h>
#include <android/bitmap.h>
#include "stackblur.h" // 在第一步中创建的.h头文件,下面可以调用里面的方法

// log宏定义
#define TAG "Native_Blur_Jni"
#define LOG_D(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)

JNIEXPORT void JNICALL Java_com_android_betterway_utils_BlurUtil_blurBitmap
(JNIEnv *env, jclass obj, jobject bitmapIn, jint r)
{
AndroidBitmapInfo infoIn;
void *pixels;
// 获取bitmap的信息
if (AndroidBitmap_getInfo(env, bitmapIn, &infoIn) != ANDROID_BITMAP_RESULT_SUCCESS) {
LOG_D("AndroidBitmap_getInfo failed!");
return;
}
// 检测bitmap是不是这两种格式,因为算法中只有对这两种图片会做处理
if (infoIn.format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
infoIn.format != ANDROID_BITMAP_FORMAT_RGB_565) {
LOG_D("Only support ANDROID_BITMAP_FORMAT_RGBA_8888 and ANDROID_BITMAP_FORMAT_RGB_565");
return;
}
// 锁定图片
if (AndroidBitmap_lockPixels(env, bitmapIn, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS) {
LOG_D("AndroidBitmap_lockPixels failed!");
return;
}
// 得到宽高
int h = infoIn.height;
int w = infoIn.width;
if (infoIn.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
// 调用stackblur.c中的blur_ARGB_8888()或blur_RGB_565()
pixels = blur_ARGB_8888((int *) pixels, w, h, r);
} else if (infoIn.format == ANDROID_BITMAP_FORMAT_RGB_565) {
pixels = blur_RGB_565((short *) pixels, w, h, r);
}
// 对应上面的AndroidBitmap_lockPixels()
AndroidBitmap_unlockPixels(env, bitmapIn);
}
#ifdef __cplusplus
}
#endif
#endif

当然,这里还有其他的文件,分别如下,这是来定义图片色彩通道的文件

stackblur.c

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <malloc.h>

#define ABS(a) ((a)<(0)?(-a):(a))
#define MAX(a, b) ((a)>(b)?(a):(b))
#define MIN(a, b) ((a)<(b)?(a):(b))

/*************************************************
Function: StackBlur
Description: Using stack way blurred image pixels
Calls: malloc
Table Accessed: NULL
Table Updated: NULL
Input: Collection of pixels, wide image, image is high, the blur radius
Output: After return to fuzzy collection of pixels
Return: After return to fuzzy collection of pixels
Others: NULL
*************************************************/
int *blur_ARGB_8888(int *pix, int w, int h, int radius) {
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;

short *r = (short *) malloc(wh * sizeof(short));
short *g = (short *) malloc(wh * sizeof(short));
short *b = (short *) malloc(wh * sizeof(short));
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;

int *vmin = (int *) malloc(MAX(w, h) * sizeof(int));

int divsum = (div + 1) >> 1;
divsum *= divsum;
short *dv = (short *) malloc(256 * divsum * sizeof(short));
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (short) (i / divsum);
}

yw = yi = 0;

int(*stack)[3] = (int (*)[3]) malloc(div * 3 * sizeof(int));
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 + (MIN(wm, MAX(i, 0)))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);

rbs = r1 - 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] = 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 = MAX(0, yp) + x;

sir = stack[i + radius];

sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];

rbs = r1 - 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] = 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;
}
}

free(r);
free(g);
free(b);
free(vmin);
free(dv);
free(stack);
return (pix);
}


short *blur_RGB_565(short *pix, int w, int h, int radius) {
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;

short *r = (short *) malloc(wh * sizeof(short));
short *g = (short *) malloc(wh * sizeof(short));
short *b = (short *) malloc(wh * sizeof(short));

int rsum, gsum, bsum, x, y, p, i, yp, yi, yw;

int *vmin = (int *) malloc(MAX(w, h) * sizeof(int));

int divsum = (div + 1) >> 1;
divsum *= divsum;

short *dv = (short *) malloc(256 * divsum * sizeof(short));

for (i = 0; i < 256 * divsum; i++) {
dv[i] = (short) (i / divsum);
}

yw = yi = 0;

int(*stack)[3] = (int (*)[3]) malloc(div * 3 * sizeof(int));
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 + (MIN(wm, MAX(i, 0)))];
sir = stack[i + radius];
sir[0] = (((p) & 0xF800) >> 11) << 3;
sir[1] = (((p) & 0x7E0) >> 5) << 2;
sir[2] = ((p) & 0x1F) << 3;

rbs = r1 - 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] = MIN(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];

sir[0] = (((p) & 0xF800) >> 11) << 3;
sir[1] = (((p) & 0x7E0) >> 5) << 2;
sir[2] = ((p) & 0x1F) << 3;

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 = MAX(0, yp) + x;

sir = stack[i + radius];

sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];

rbs = r1 - 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++) {
// Not have alpha channel
pix[yi] = ((((dv[rsum]) >> 3) << 11) | (((dv[gsum]) >> 2) << 5) | ((dv[bsum]) >> 3));

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] = 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;
}
}
free(r);
free(g);
free(b);
free(vmin);
free(dv);
free(stack);
return (pix);
}

代码有些长。。。还好不用自己敲

以及它对应的头文件

stackblur.h

1
2
3
4
5
6
#ifndef BETTERWAY_STACKBLUR_H
#define BETTERWAY_STACKBLUR_H
int *blur_ARGB_8888(int *, int, int, int);

short *blur_RGB_565(short *, int, int, int);
#endif //BETTERWAY_STACKBLUR_H

这些都做完了之后,就可以在CmakeList.text文件中加载了,如下所示,注意,我这里只给出了与默认文件不一样的部分,注意,这里的jnigraphics虽然说我们已经在gradle中添加,但在这里不添加的话还是会报错,但是c/c++中使用Log的库只要在gradle中添加就可以,这里实在是没弄明白,如有高手知道请告知,感激不尽。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
add_library( # Sets the name of the library.
stackblur

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/stackblur.c )
target_link_libraries( # Specifies the target library.
native-lib
stackblur
jnigraphics
# Links the target library to the log library
# included in the NDK.
${log-lib} )

##### 因为我是在默认的native-lib中实现的函数,所以并没有添加另外的文件,当然,如果你实在另外的C/C++文件中实现的,就要添加其他的

最后在java函数中调用native函数,就可以实现图片的毛玻璃化了,这里给出我的实现方法

1
2
3
4
5
6
7
8
9
public static Bitmap blur(Bitmap srcBitmap) {
blurBitmap(srcBitmap, 13);
return srcBitmap;
}
static {
System.loadLibrary("native-lib");
}
// 参数r为对bitmap虚化的程度范围
static native void blurBitmap(Object bitmap, int r);

附加一点东西,为了实现截图的毛玻璃效果,我们一般还会压缩一下图片,这样才能使毛玻璃效果更快更自然,这里给出一个减少图片像素的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public static Bitmap compressScale(Bitmap bmp) {
// 尺寸压缩倍数,值越大,图片尺寸越小
int ratio = 5;
// 压缩Bitmap到对应尺寸
Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
canvas.drawBitmap(bmp, null, rect, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, 60 ,baos);
return result;
}

配合上这个方法,毛玻璃效果就能更快得实现了

如上,如有错误,恳请指正