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

2025-05-29 0 106

前言

大家应该都知道在iOS的CoreImage的Api中,有一个CIDetector的类,Detector的中文翻译有探测器的意思,那么CIDetector是用来做哪些的呢?

它可以:

  • CIDetectorTypeFace 面部识别
  • CIDetectorTypeText 文本识别
  • CIDetectorTypeQRCode 条码识别
  • CIDetectorTypeRectangle 矩形识别

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
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 // 文本检测器是否应该检测子特征,默认值是否

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49
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

总结

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS开发教程之识别图片中二维码功能的实现 https://www.kuaiidc.com/88962.html

相关文章

发表评论
暂无评论