利用iOS原生库AVFoundation实现条形码的识别功能

最近项目要实现扫描条形码的功能,于是在网络上搜寻快速的实现方案。

最早知道的方案就是ZXing这个Google的库,看了 iOS攻城狮的文章 后决定要用AVFoundation直接实现扫码的功能了。

然后,继续搜比较实用的轮子。

CSDN中的这个例子讲的很详细,代码 改改直接用上。

这份代码有几个问题:定时器导致内存泄露,设置扫描区域和模糊效果的代码太生硬,需要改进。

测试发现,扫描区域设置的不准确,然后查找相关资料,确定元凶:rectOfInterest。

它是AVCaptureMetadataOutput的一个属性,这篇文章有一个讨论,官方也给出了解释:

"The value of this property is a CGRect that determines the receiver's rectangle of interest for each frame of video. The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata. Specifying a rectOfInterest may improve detection performance for certain types of metadata. The default value of this property is the value CGRectMake(0, 0, 1, 1). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned."

这个说的很简单,但是具体做起来还是挺抽象的。然后,在简书中找到loongod的一篇文章,进而在Stack Overflow中找到问题的答案:How do I use the metadataOutputRectOfInterestForRect method and rectOfInterest property to scan a specific area? (QR Code)

根据 loongod 的文章做代码改进,特别是中间透空区域的实现。大功告成!

附上GitHub上这个 Demo 。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容