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]

        /**
         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

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

相关阅读更多精彩内容

  • 2018-09-04更新: 很久没有更新文章了,工作之余花时间看了之前写的这篇文章并运行了之前写的配套Demo,通...
    Mr_Victory阅读 27,826评论 50 172
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,056评论 3 119
  • 我生命的出发地荆紫关镇,位于河南省南阳市淅川县西北部,地处豫、鄂、陕三省结合部,素有"一脚踏三省",...
    graceLiu_621阅读 3,421评论 1 2
  • 后腿直 腰部直 各15秒 提住脚 手伸 脚指天 平直 腰挺直 横叉 劈叉 叉腰延展 !
    学习是门手艺阅读 1,409评论 0 0
  • 01 清晨,雪停了。校园围墙里晨光晦暗,寂静无人。一条漆黑的正在融化的小路被脚印踩出。我走在远处的树木和围墙旁边,...
    龙爷iiii阅读 4,264评论 12 27

友情链接更多精彩内容