欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发教程之识别图片中二维码功能的实现

程序员文章站 2023-12-11 17:04:28
前言 大家应该都知道在ios的coreimage的api中,有一个cidetector的类,detector的中文翻译有探测器的意思,那么cidetector是用来做哪些...

前言

大家应该都知道在ios的coreimage的api中,有一个cidetector的类,detector的中文翻译有探测器的意思,那么cidetector是用来做哪些的呢?

它可以:

  • cidetectortypeface 面部识别
  • cidetectortypetext 文本识别
  • cidetectortypeqrcode 条码识别
  • cidetectortyperectangle 矩形识别

这个类其实很简单,它的头文件代码很少,下面来看一下注释

open class cidetector : nsobject {
 // 初始化方法
 public init?(oftype type: string, context: cicontext?, options: [string : any]? = nil)
 // 获取识别特征
 open func features(in image: ciimage) -> [cifeature]
 open func features(in image: ciimage, options: [string : any]? = nil) -> [cifeature]
}
// 识别类型
public let cidetectortypeface: string // 面部识别
public let cidetectortyperectangle: string // 矩形识别
public let cidetectortypeqrcode: string // 条码识别
public let cidetectortypetext: string // 文本识别
// 下面定义的就是options中可以传的参数
public let cidetectoraccuracy: string // 识别精度
public let cidetectoraccuracylow: string // 低精度,识别速度快
public let cidetectoraccuracyhigh: string // 高精度,识别速度慢
public let cidetectortracking: string // 是否开启面部追踪
public let cidetectorminfeaturesize: string // 指定最小尺寸的检测器,小于这个尺寸的特征将不识别,cidetectortypeface(0.01 ~ 0.50),cidetectortypetext(0.00 ~ 1.00),cidetectortyperectangle(0.00 ~ 1.00)
public let cidetectormaxfeaturecount: string // 设置返回矩形特征的最多个数 1 ~ 256 默认值为1
public let cidetectornumberofangles: string // 设置角度的个数 1, 3, 5, 7, 9, 11
public let cidetectorimageorientation: string // 识别方向
public let cidetectoreyeblink: string // 眨眼特征
public let cidetectorsmile: string // 笑脸特征
public let cidetectorfocallength: string // 每帧焦距
public let cidetectoraspectratio: string // 矩形宽高比
public let cidetectorreturnsubfeatures: string // 文本检测器是否应该检测子特征,默认值是否

下面是二维码识别的实例代码

func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [uiimagepickercontroller.infokey : any]) {
 // 1.取到图片
 let image = info[uiimagepickercontroller.infokey.originalimage] as? uiimage
 // 2.生成ciimage
 let ciimage = ciimage(cgimage: image!.cgimage!)
 // 3.识别精度
 let options = [cidetectoraccuracy: cidetectoraccuracyhigh]

 /**
 4.创建识别器,3个参数

 oftype:识别类型
 cidetectortypeface 面部识别
 cidetectortypetext 文本识别
 cidetectortypeqrcode 条码识别
 cidetectortyperectangle 矩形识别

 context:上下文,默认传nil

 options:识别精度
 cidetectoraccuracylow 低精度,识别速度快
 cidetectoraccuracyhigh 高精度,识别速度慢
 */
 let detector = cidetector(oftype: cidetectortypeqrcode, context: nil, options: options)

 /**
 5.获取识别结果,2个参数

 in:需要识别的图片

 options:需要识别的特征
 cidetectorminfeaturesize: 指定最小尺寸的检测器,小于这个尺寸的特征将不识别,cidetectortypeface(0.01 ~ 0.50),cidetectortypetext(0.00 ~ 1.00),cidetectortyperectangle(0.00 ~ 1.00)
 cidetectortracking: 是否开启面部追踪 true 或 false
 cidetectormaxfeaturecount: 设置返回矩形特征的最多个数 1 ~ 256 默认值为1
 cidetectornumberofangles: 设置角度的个数 1, 3, 5, 7, 9, 11
 cidetectorimageorientation: 识别方向
 cidetectoreyeblink: 眨眼特征
 cidetectorsmile: 笑脸特征
 cidetectorfocallength: 每帧焦距
 cidetectoraspectratio: 矩形宽高比
 cidetectorreturnsubfeatures: 文本检测器是否应该检测子特征,默认值是否
 */
 let features = detector?.features(in: ciimage, options: nil)

 // 遍历出二维码
 for item in features! where item.iskind(of: ciqrcodefeature.self) {
 print((item as! ciqrcodefeature).messagestring ?? "")
 }
 }

demo地址 https://github.com/cdcyd/ccqrcode (本地下载

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: