前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-识别图片中二维码

iOS-识别图片中二维码

作者头像
用户2215591
发布2018-08-02 16:13:02
2K0
发布2018-08-02 16:13:02
举报
文章被收录于专栏:iOSer成长记录

在iOS的CoreImage的Api中,有一个CIDetector的类,Detector的中文翻译有探测器的意思,那么CIDetector是用来做哪些的呢?它可以: CIDetectorTypeFace 面部识别 CIDetectorTypeText 文本识别 CIDetectorTypeQRCode 条码识别 CIDetectorTypeRectangle 矩形识别 这个类其实很简单,它的头文件代码很少,下面来看一下注释

代码语言:javascript
复制
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 // 文本检测器是否应该检测子特征,默认值是否

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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.07.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档