图片高斯模糊效果

为实现一些玻璃蒙版的效果,需要用到高斯算法对图片进行模糊处理,基础算法代码如下


  1 public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {
2
3 if (VERSION.SDK_INT > 20) {
4 Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
5
6 final RenderScript rs = RenderScript.create(context);
7 final Allocation input = Allocation.createFromBitmap(rs, sentBitmap,
8 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
9 final Allocation output = Allocation.createTyped(rs, input.getType());
10 final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
11 script.setRadius(radius /* e.g. 3.f */);
12 script.setInput(input);
13 script.forEach(output);
14 output.copyTo(bitmap);
15 return bitmap;
16 }
17
18 Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
19
20 if (radius < 1) {
21 return (null);
22 }
23
24 int w = bitmap.getWidth();
25 int h = bitmap.getHeight();
26
27 int[] pix = new int[w * h];
28 Log.e("pix", w + " " + h + " " + pix.length);
29 bitmap.getPixels(pix, 0, w, 0, 0, w, h);
30
31 int wm = w - 1;
32 int hm = h - 1;
33 int wh = w * h;
34 int div = radius + radius + 1;
35
36 int r[] = new int[wh];
37 int g[] = new int[wh];
38 int b[] = new int[wh];
39 int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
40 int vmin[] = new int[Math.max(w, h)];
41
42 int divsum = (div + 1) >> 1;
43 divsum *= divsum;
44 int dv[] = new int[256 * divsum];
45 for (i = 0; i < 256 * divsum; i++) {
46 dv[i] = (i / divsum);
47 }
48
49 yw = yi = 0;
50
51 int[][] stack = new int[div][3];
52 int stackpointer;
53 int stackstart;
54 int[] sir;
55 int rbs;
56 int r1 = radius + 1;
57 int routsum, goutsum, boutsum;
58 int rinsum, ginsum, binsum;
59
60 for (y = 0; y < h; y++) {
61 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
62 for (i = -radius; i <= radius; i++) {
63 p = pix[yi + Math.min(wm, Math.max(i, 0))];
64 sir = stack[i + radius];
65 sir[0] = (p & 0xff0000) >> 16;
66 sir[1] = (p & 0x00ff00) >> 8;
67 sir[2] = (p & 0x0000ff);
68 rbs = r1 - Math.abs(i);
69 rsum += sir[0] * rbs;
70 gsum += sir[1] * rbs;
71 bsum += sir[2] * rbs;
72 if (i > 0) {
73 rinsum += sir[0];
74 ginsum += sir[1];
75 binsum += sir[2];
76 } else {
77 routsum += sir[0];
78 goutsum += sir[1];
79 boutsum += sir[2];
80 }
81 }
82 stackpointer = radius;
83
84 for (x = 0; x < w; x++) {
85
86 r[yi] = dv[rsum];
87 g[yi] = dv[gsum];
88 b[yi] = dv[bsum];
89
90 rsum -= routsum;
91 gsum -= goutsum;
92 bsum -= boutsum;
93
94 stackstart = stackpointer - radius + div;
95 sir = stack[stackstart % div];
96
97 routsum -= sir[0];
98 goutsum -= sir[1];
99 boutsum -= sir[2];
100
101 if (y == 0) {
102 vmin[x] = Math.min(x + radius + 1, wm);
103 }
104 p = pix[yw + vmin[x]];
105
106 sir[0] = (p & 0xff0000) >> 16;
107 sir[1] = (p & 0x00ff00) >> 8;
108 sir[2] = (p & 0x0000ff);
109
110 rinsum += sir[0];
111 ginsum += sir[1];
112 binsum += sir[2];
113
114 rsum += rinsum;
115 gsum += ginsum;
116 bsum += binsum;
117
118 stackpointer = (stackpointer + 1) % div;
119 sir = stack[(stackpointer) % div];
120
121 routsum += sir[0];
122 goutsum += sir[1];
123 boutsum += sir[2];
124
125 rinsum -= sir[0];
126 ginsum -= sir[1];
127 binsum -= sir[2];
128
129 yi++;
130 }
131 yw += w;
132 }
133 for (x = 0; x < w; x++) {
134 rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
135 yp = -radius * w;
136 for (i = -radius; i <= radius; i++) {
137 yi = Math.max(0, yp) + x;
138
139 sir = stack[i + radius];
140
141 sir[0] = r[yi];
142 sir[1] = g[yi];
143 sir[2] = b[yi];
144
145 rbs = r1 - Math.abs(i);
146
147 rsum += r[yi] * rbs;
148 gsum += g[yi] * rbs;
149 bsum += b[yi] * rbs;
150
151 if (i > 0) {
152 rinsum += sir[0];
153 ginsum += sir[1];
154 binsum += sir[2];
155 } else {
156 routsum += sir[0];
157 goutsum += sir[1];
158 boutsum += sir[2];
159 }
160
161 if (i < hm) {
162 yp += w;
163 }
164 }
165 yi = x;
166 stackpointer = radius;
167 for (y = 0; y < h; y++) {
168 // Preserve alpha channel: ( 0xff000000 & pix[yi] )
169 pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
170
171 rsum -= routsum;
172 gsum -= goutsum;
173 bsum -= boutsum;
174
175 stackstart = stackpointer - radius + div;
176 sir = stack[stackstart % div];
177
178 routsum -= sir[0];
179 goutsum -= sir[1];
180 boutsum -= sir[2];
181
182 if (x == 0) {
183 vmin[y] = Math.min(y + r1, hm) * w;
184 }
185 p = x + vmin[y];
186
187 sir[0] = r[p];
188 sir[1] = g[p];
189 sir[2] = b[p];
190
191 rinsum += sir[0];
192 ginsum += sir[1];
193 binsum += sir[2];
194
195 rsum += rinsum;
196 gsum += ginsum;
197 bsum += binsum;
198
199 stackpointer = (stackpointer + 1) % div;
200 sir = stack[stackpointer];
201
202 routsum += sir[0];
203 goutsum += sir[1];
204 boutsum += sir[2];
205
206 rinsum -= sir[0];
207 ginsum -= sir[1];
208 binsum -= sir[2];
209
210 yi += w;
211 }
212 }
213
214 Log.e("pix", w + " " + h + " " + pix.length);
215 bitmap.setPixels(pix, 0, w, 0, 0, w, h);
216 return (bitmap);
217 }

但是此类实现模糊效果效率是个很大的问题,所以我们需要把原图先处理一下,再进行模糊操作。


1 private static Bitmap small(Bitmap bitmap) {
2 Matrix matrix = new Matrix();
3 matrix.postScale(0.08f, 0.08f); // 长和宽放大缩小的比例
4 Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
5 true);
6 return resizeBmp;
7 }

在使用模糊图的地方可以再添加一些透明蒙版来实现高亮偏暗的效果。

时间: 2024-10-24 18:21:14

图片高斯模糊效果的相关文章

iOS开发-图片高斯模糊效果

http://www.cnblogs.com/xiaofeixiang/p/5129074.html iOS开发的时候有的时候需要将图片设置模糊,或者通过点击下拉方法,去除模糊,一切都是为了应用更受用户欢迎,iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,关于图片实现高斯模糊效果有三种方式,CoreImage,GPUImage(第三方开源类库)和vImage.GPUImage没怎么用过,本文就讲两种方式Core Image和vImage. Core

iOS实现图片高斯模糊效果

RT,支持iOS 6.0 and later CIContext *context = [CIContext contextWithOptions:nil]; CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; // create gaussian blur filter CIFilter *filter = [CIFilter filterWithName:@&quo

Android 实现高斯模糊效果及低版本兼容

Android 实现高斯模糊效果及低版本兼容 Hello,好久不见,好久没写博客了,总结了下原因有三,第一,变懒了:第二,变懒了:第三,还是变懒了:因为任何不更新博客的理由都是借口! 一.效果演示 项目中用到了高斯模糊效果,查阅过一些资料,考虑到性能问题最终还是选择使用Android自带的RenderScript库来实现,关于使用RenderScript来实现高斯模糊网上也有很多类似的方法,大部分都总结的比较乱,此处算是做一个整理吧,供有类似需求的同学参考及学习.           (项目效果

iOS 图片背景模糊效果

iOS 图片背景模糊效果 1.使用CoreImage中的模糊滤镜 原始效果图如下: CoreImage的实现: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImage * image = [UIImage imageNamed:@"icon"]; /*..CoreImage中的模糊效果滤镜..*/

UIVisualEffectView(高斯模糊效果)

///高斯模糊. UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; tempView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:tempView]; ///UIBlurEffectStyleExtraLight聚光灯效果.UIBlurEffectStyleLight效果轻.UIBlurEffectSt

Android jni 高斯模糊效果

Android jni 高斯模糊效果 本Demo 通过jni调用C实现高斯模糊效果,提高了实现速度,并和java实现的速度做了比较. 下载地址:http://www.devstore.cn/code/info/451.html 运行截图:

图片的高斯模糊效果

一:常用的是UIVisualEffectView, 是在iOS 8 增加的,让我们可以很方便的做出模糊(毛玻璃)效果. UIVisualEffectView是一个view,就是对这个view对模糊设置后,把这个view 盖在自己的图片上, UIVisualEffectView 包含两种特效,一种叫 UIBlurEffect,一种叫 UIVibrancyEffect.他们都是用来模糊的,区别在于 UIBlurEffect 不会对自身这层进行模糊,UIVibrancyEffect 除了模糊,还会透出

教你几行代码实现图片的模糊效果

一张清楚的图片变的模糊了,也就是所谓的毛玻璃效果,通过查看图层可以发现,它只是在原有的图片上加了一个View,而这个View具有毛玻璃效果, 图层查看如下图所示: 最终效果图如下图: 1 1 // 创建图片 2 2 UIImageView *imgView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 3 3 imgView.image = [UIImage imageNamed:@"1"]; 4

仿面包旅行个人中心下拉顶部背景放大高斯模糊效果

HeaderView.h // // HeaderView.h // 仿面包旅行个人中心 // // Created by [email protected] on 15/5/14. // Copyright (c) 2015年 wb145230. All rights reserved. // #import <UIKit/UIKit.h> @interface HeaderView : UIView @property(nonatomic, strong) UIScrollView *im