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

来自:互联网
时间:2020-05-26
阅读:

前言

大家应该都知道在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 

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

返回顶部
顶部