栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

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

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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

 
 let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: options)

 
 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 (本地下载)

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/166147.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号