毛玻璃效果算法c语言

上图是一个典型的视觉效果——文字背景不再固定,而底部对应区域模糊不清,仿佛蒙上了一层毛玻璃。

它的原理也很简单,分三步走:

这里涉及两个技术要点:

ViewTreeObserver有一个名为OnPreDrawListener的侦听器。

执行时,版面文件经过测量、排版、显示后会被绘制到屏幕上,通过调用其getDrawingCache()方法可以获得其位图。完整的方法如下:

有两种方案:

两种方案都可以模糊位图对象,但是当模糊半径增大时,StackBlur可以保持良好的性能,不受Renderscript的半径25px的限制。

GitHub项目中有一个项目模糊,实现了Java版的StackBlur算法FastBlur,并给出了两种方案的效率对比演示。经过测试,

好像Renderscript的性能更好,应该是Renderscript在Android上做了优化。但是考虑到Android中一帧的渲染时间不能超过16ms(60fps),这样的表现并不友好。模糊作者想出了另一种思维方式:

这时,效率的提高非常明显:

当然,生成的模糊图片不一样,但都是模糊的背景,对于用户来说没有太大区别。

好了,到目前为止,Android上制作毛玻璃背景虚化效果的技术已经确定了。

我在模糊的基础上封装了一下,界面如下:

有两个相应的类别:

如何通过html5实现iOS 7的实时毛玻璃虚化效果 通过html5实现IOS7的毛玻璃模糊效果,需要使用代码来执行。

//添加模糊效果,图像就是画面,模糊就是模糊。

- (UIImage *)模糊图像:(UIImage *)具有模糊级别的图像:(CGFloat)模糊{

//歧义,

if((模糊0.1f) ||(模糊2.0f)) {

模糊= 0.5f

}

//boxSize必须大于0。

int box size =(int)(blur * 100);

box size-=(box size % 2)1;

NSLog(@"boxSize:%i ",box size);

//图像处理

CGImageRef img = image。CGImage

//需要引入# importaccelerate/accelerate.h。

/*

本文档描述了Accelerate框架,它包含用于向量和矩阵数学、数字信号处理、大数处理和图像处理的C APIs。

本文介绍了Accelerate框架,包括C语言应用编程接口(API)的向量和矩阵数学、数字信号处理、海量处理和图像处理。

*/

//图像缓存、输入缓存、输出缓存

vImage _ Buffer inBuffer,outBuffer

vImage_Error错误;

//像素缓存

void * pixelBuffer

//数据源提供程序,定义一个为quartz提供数据的不透明类型。

CGDataProviderRef in provider = cgimagegetdata provider(img);

//提供程序的数据。

CFDataRef inBitmapData = CGDataProviderCopyData(in provider);

//宽度、高度、字节/行、数据

in buffer . width = CGImageGetWidth(img);

in buffer . height = CGImageGetHeight(img);

in buffer . row bytes = CGImageGetBytesPerRow(img);

in buffer . data =(void *)CFDataGetBytePtr(inBitmapData);

//图像号缓存,字节行*图像高度

pixel buffer = malloc(CGImageGetBytesPerRow(img)* CGImageGetHeight(img));

out buffer . data = pixel buffer;

out buffer . width = CGImageGetWidth(img);

out buffer . height = CGImageGetHeight(img);

out buffer . row bytes = CGImageGetBytesPerRow(img);

//第三个中间缓冲,抗锯齿效果。

void * pixel buffer 2 = malloc(CGImageGetBytesPerRow(img)* CGImageGetHeight(img));

vImage _ Buffer outBuffer2

out buffer 2 . data = pixel buffer 2;

out buffer 2 . width = CGImageGetWidth(img);

out buffer 2 . height = CGImageGetHeight(img);

out buffer 2 . row bytes = CGImageGetBytesPerRow(img);

//通过具有箱式过滤器效果的隐式M x N内核对ARGB8888源图像中的感兴趣区域进行卷积。

error = vImageBoxConvolve _ argb 8888(in buffer,outBuffer2,NULL,0,0,boxSize,boxSize,NULL,kvImageEdgeExtend);

error = vImageBoxConvolve _ argb 8888(out buffer 2,inBuffer,NULL,0,0,boxSize,boxSize,NULL,kvImageEdgeExtend);

error = vImageBoxConvolve _ argb 8888(in buffer,outBuffer,NULL,0,0,boxSize,boxSize,NULL,kvImageEdgeExtend);

如果(错误){

NSLog(@ "来自卷积%ld的错误",错误);

}

// NSLog(@ "byte component:% zu ",cgimagegetbitspercomponent(img));

//色彩空间设备RGB

cgcolorspace ref colorSpace = cgcolorspace createdevicergb();

//创建带有图片的上下文,cgimagegetbitspercomponent (IMG),7,8

CGContextRef CTX = CGBitmapContextCreate(

outBuffer.data,

outBuffer .宽度,

outBuffer .高度,

8,

outBuffer.rowBytes,

色彩空间,

CGImageGetBitmapInfo(image。CGImage));

//根据上下文,重新组合处理后的图片。

CGImageRef imageRef = CGBitmapContextCreateImage(CTX);

ui image * return image =[ui image imageWithCGImage:imageRef];

//清理

CGContextRelease(CTX);

CGColorSpaceRelease(颜色空间);

免费(pixel buffer);

免费(pixel buffer 2);

cf release(inBitmapData);

CGColorSpaceRelease(颜色空间);

CGImageRelease(imageRef);

返回returnImage

}

需要注意的是,上面的代码会崩溃。

崩溃日志:断言失败:(!space-is_singleton),函数color_space_dealloc,文件ColorSpaces/color-space

这可以通过以下方式实现。

-(ui image *)applybluradius:(CG float)radius to image:(ui image *)image

{

if(半径0)半径= 0;

CIContext * context =[CIContext context with options:nil];

ci image * input image =[ci imageimagewithcgimage:image。CGImage];

//设置高斯模糊

ci filter * filter =[CIFilterfilterWithName:@ " CIGaussianBlur "];

[filter setValue:inputmapageforkey:kCIInputImageKey];

[filter setValue:[nsnumbernumberwithflot:radius]forKey:@ " input radius "];

ci image * result =[filtervalueForKey:kCIOutputImageKey];

CGImageRef CG image =[context create cgImage:result from rect:[input image extent]];

ui image * return image =[UIImageimageWithCGImage:CG image];

CGImageRelease(cgImage);

返回returnImage

}

如何在iPhone中实现图片的毛玻璃效果 如何通过工具栏模拟图片的毛玻璃效果?首先,我们创建一个新项目。项目模板切换到iOS,选择单视图应用,如下图所示:

点击下一步,任意命名,语言选择Objective-C,如下图所示:

输入完成项目的名称后,继续点击下一步,选择Mac上存储项目文件的目录完成。项目确立后,我们今天就开始我们的代码之旅。

既然需要模拟画面的毛玻璃效果,当然首先需要一张图。我用过周杰伦第一张专辑的封面作为这一节的图片素材。图片如下:

接下来,将这张图片拖动到刚刚创建的项目文件的Assets.xcassets文件夹中,如下图所示:

现在单击左边的文件列表选择ViewController.m文件。此时,相关代码会显示在文件列表右侧的代码区。接下来,我们将编写代码来完成我们今天想要达到的效果。

需要注意的是,我们需要在viewDidLoad方法中写以下代码,OK,start。

第一步,我们先对图片进行处理,添加到iPhone界面,全屏显示。相关代码如下:

创建UIImageView对象来存储图片,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " ui imageView * imageView = span style = " color:# 999988;font-style:italic "[span style = " "[ui imageview alloc]/span init]/span。/代码/前

设置图片大小占据整个屏幕,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " imageview . frame = span style = " font-weight:700 " self/span . view . bounds;/代码/前

指定要显示的图片资源,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " imageview . image =[ui image span style = " color:# DD 1144 " image named:/span @ span style = " color:# DD 1144 " " Jay . jpg "/span];/代码/前

设置图片显示模式,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " imageview . content mode = UIViewContentModeScaleToFillspan style = " color:# 999988;字体样式:斜体";/span/代码/前

使用以下代码在iPhone上显示图像图片:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;normal " span style = " color:# 9900;font-weight:700 "[self . view add subview:imageView]/span span style = " color:# 999988;字体样式:斜体";/span/代码/前

现在让我们来看看效果,并运行模拟器。效果如下:

就这样,我们把这张图加到了iPhone上,全屏显示。因为图片比例和iPhone不一致,这里略有不平衡。可以找一张同比例的图来测试。接下来,我们来看看如何制作毛玻璃效果。

第二步,制作毛玻璃效果。这里我们用工具栏覆盖图片来模拟毛玻璃效果。具体操作方法是先创建一个工具栏对象,然后将其大小设置为与图片大小相同,即设置为屏幕大小,然后覆盖在图片上。相关操作代码如下。

使用以下代码创建一个ToolBar对象:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " UIToolbar * toolBar = span style = " color:# 999988;font-style:italic "[span style = " "[ui toolbar alloc]/span init]/span。/代码/前

设置工具栏大小与图片大小一致,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " toolbar . frame = imageview . bounds span style = " color:# 999988;字体样式:斜体";/span/代码/前

设置磨砂玻璃效果,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " toolbar . bar style = UIBarStyleBlackspan style = " color:# 999988;字体样式:斜体";/span/代码/前

将工具栏添加到图片中以覆盖图片。代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;normal " span style = " color:# 9900;font-weight:700 "[imageView add subview:toolBar]/span span style = " color:# 999988;字体样式:斜体";/span/代码/前

运行模拟器具有以下效果:

这是黑色磨砂玻璃效果,关于磨砂玻璃还有一个默认的白色效果。只需将毛玻璃效果设置代码修改为以下代码:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " toolbar . bar style = UIBarStyleDefaultspan style = " color:# 999988;字体样式:斜体";/span/代码/前

运行模拟器具有以下效果:

但我个人觉得黑色更漂亮,所以这里用黑色,我会把设定代码还原成黑色效果。

现在画面有毛玻璃效果,比较模糊。如果这时候我们在毛玻璃上写字,字会很清楚。你想试试吗?好了,我们继续在毛玻璃上写一些字,看看效果。让我们把这张专辑的名字写在图片的底部。

第三步,添加文本,相关代码如下。

使用以下代码创建一个Label对象:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " ui label * label = span style = " color:# 999988;font-style:italic "[span style = " "[UILabel alloc]/span init]/span。/代码/前

设置文本显示位置,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " label . frame = CGRectMake(self . view . frame . size . width/span style = " color:teal " 2/span-span style = " color:teal " 50/span,self.view.frame.size.height/span style = " color:teal " 2/span span style = " color:teal " 150/span,span style = " color:teal " 100/span);/代码/前

将文本显示颜色设置为白色,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;自动换行:normal " label . text color = span style = " color:# DD 1144 "[ui color white color]/span。/代码/前

设置文本居中对齐,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal " span style = " font-weight:700 " label/span . text alignment = NSTextAlignmentCenter。/代码/前

设置文本显示内容,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;正常" label . span style = " color:# 445588;font-weight:700 " text/span = @ span style = " color:# DD 1144 " " Jay "/span;/代码/前

将文本添加到工具栏进行显示,代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;normal " span style = " color:# 9900;font-weight:700 "[toolBar addSubview:label]/span span style = " color:# 999988;字体样式:斜体";/span/代码/前

运行模拟器具有以下效果:

至此,所有的代码都写好了,今天的毛玻璃效果图的效果演示也就完成了。完整的代码如下:

pre style = " font-size:11.89999618530273 px;行高:1.45;font-family:Consolas,'解放单声道',Menlo,Courier,monospace自动换行:正常;填充:16px溢出:自动;背景色:rgb(247,247,247);颜色:rgb(51,51,51);margin-top:0px!重要;边距-底部:0px!重要" code style = " display:inline;溢出:可见;背景色:透明;font-family:Consolas,'解放单声道',Menlo,Courier,monospace断字:正常;边框:0px行高:继承;word-wrap:normal "-(span style = " font-weight:700 " void/span)viewDidLoad { nbsp。nbsp[span style = " font-weight:700 " super/span viewDidLoad];nbspnbspnbspnbspspan style = " color:# 999988;Font-style:italic"//第一步:处理图片/span nbsp;nbspspan style = " color:# 086 B3 " ui imageView/span * imageView =[[span style = " color:# 086 B3 " ui imageView/span alloc]init];nbspnbspimageViewspan style="color:teal "。frame/span = span style = " font-weight:700 " self/span span style = " color:teal "。view/spanspan style="color:teal "。界限/跨度。nbspnbspimageViewspan style="color:teal "。image/span =[span style = " color:# 086 B3 " ui image/span image name:span style = " color:# DD 1144 " @ " Jay . jpg "/span];nbspnbspimageViewspan style="color:teal "。content mode/span = span style = " color:# 086 B3 " UIViewContentModeScaleToFill/span。nbspnbspnbspnbspspan style = " color:# 999988;Font-style:italic"//第二步:制作毛玻璃/span nbsp。nbspspan style = " color:# 086 B3 " UIToolbar/span * toolBar =[[span style = " color:# 086 B3 " UIToolbar/span alloc]init];nbspnbsptoolBarspan style="color:teal "。frame/span = imageview span style = " color:teal "。界限/跨度。nbspnbsptoolBarspan style="color:teal "。bar style/span = span style = " color:# 086 B3 " UIBarStyleBlack/span。nbspnbsp[imageView添加子视图:工具栏];nbspnbspnbspnbspspan style = " color:# 999988;Font-style:italic"//第三步:添加文本/span nbsp。nbspspan style = " color:# 086 B3 " ui label/span * label =[[span style = " color:# 086 B3 " ui label/span alloc]init];nbspnbsplabelspan style="color:teal "。frame/span = span style = " color:# 086 B3 " CGRectMake/span(span style = " font-weight:700 " self/span span style = " color:teal "。view/ spanspan style="color:teal "。frame/span span style = " color:teal "。size/spanspan style="color:teal "。width/span/span style = " color:teal " 2/span-span style = " color:teal " 50/span,span style = " font-weight:700 " self/span span style = " color:teal "。view/spanspan style="color:teal "。frame/span span style = " color:teal "。size/ spanspan style="color:teal "。height/span/span style = " color:teal " 2/span span style = " color:teal " 150/span,span style="color:teal"100/span,span style = " color:teal " 100/span);nbspnbsplabelspan style="color:teal "。text color/span =[span style = " color:# 086 B3 " ui color/span white color];nbspnbsplabelspan style="color:teal "。text alignment/span = span style = " color:# 086 B3 " nstextalignment center/span。nbspnbsplabelspan style="color:teal "。text/span = span style = " color:# DD 1144 " @ " Jay "/span;nbspnbsp[工具栏添加子视图:标签];nbspnbspnbspnbsp[span style = " font-weight:700 " self/span style = " color:teal "。view/span addSubview:imageView];}/代码/前

相关文章

发表新评论