iOS開發(fā)教程之識(shí)別圖片中二維碼功能的實(shí)現(xiàn)
前言
大家應(yīng)該都知道在iOS的CoreImage的Api中,有一個(gè)CIDetector的類,Detector的中文翻譯有探測(cè)器的意思,那么CIDetector是用來做哪些的呢?
它可以:
- CIDetectorTypeFace 面部識(shí)別
- CIDetectorTypeText 文本識(shí)別
- CIDetectorTypeQRCode 條碼識(shí)別
- CIDetectorTypeRectangle 矩形識(shí)別
這個(gè)類其實(shí)很簡(jiǎn)單,它的頭文件代碼很少,下面來看一下注釋
open class CIDetector : NSObject {
// 初始化方法
public init?(ofType type: String, context: CIContext?, options: [String : Any]? = nil)
// 獲取識(shí)別特征
open func features(in image: CIImage) -> [CIFeature]
open func features(in image: CIImage, options: [String : Any]? = nil) -> [CIFeature]
}
// 識(shí)別類型
public let CIDetectorTypeFace: String // 面部識(shí)別
public let CIDetectorTypeRectangle: String // 矩形識(shí)別
public let CIDetectorTypeQRCode: String // 條碼識(shí)別
public let CIDetectorTypeText: String // 文本識(shí)別
// 下面定義的就是options中可以傳的參數(shù)
public let CIDetectorAccuracy: String // 識(shí)別精度
public let CIDetectorAccuracyLow: String // 低精度,識(shí)別速度快
public let CIDetectorAccuracyHigh: String // 高精度,識(shí)別速度慢
public let CIDetectorTracking: String // 是否開啟面部追蹤
public let CIDetectorMinFeatureSize: String // 指定最小尺寸的檢測(cè)器,小于這個(gè)尺寸的特征將不識(shí)別,CIDetectorTypeFace(0.01 ~ 0.50),CIDetectorTypeText(0.00 ~ 1.00),CIDetectorTypeRectangle(0.00 ~ 1.00)
public let CIDetectorMaxFeatureCount: String // 設(shè)置返回矩形特征的最多個(gè)數(shù) 1 ~ 256 默認(rèn)值為1
public let CIDetectorNumberOfAngles: String // 設(shè)置角度的個(gè)數(shù) 1, 3, 5, 7, 9, 11
public let CIDetectorImageOrientation: String // 識(shí)別方向
public let CIDetectorEyeBlink: String // 眨眼特征
public let CIDetectorSmile: String // 笑臉特征
public let CIDetectorFocalLength: String // 每幀焦距
public let CIDetectorAspectRatio: String // 矩形寬高比
public let CIDetectorReturnSubFeatures: String // 文本檢測(cè)器是否應(yīng)該檢測(cè)子特征,默認(rèn)值是否
下面是二維碼識(shí)別的實(shí)例代碼
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.識(shí)別精度
let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
/**
4.創(chuàng)建識(shí)別器,3個(gè)參數(shù)
ofType:識(shí)別類型
CIDetectorTypeFace 面部識(shí)別
CIDetectorTypeText 文本識(shí)別
CIDetectorTypeQRCode 條碼識(shí)別
CIDetectorTypeRectangle 矩形識(shí)別
context:上下文,默認(rèn)傳nil
options:識(shí)別精度
CIDetectorAccuracyLow 低精度,識(shí)別速度快
CIDetectorAccuracyHigh 高精度,識(shí)別速度慢
*/
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: options)
/**
5.獲取識(shí)別結(jié)果,2個(gè)參數(shù)
in:需要識(shí)別的圖片
options:需要識(shí)別的特征
CIDetectorMinFeatureSize: 指定最小尺寸的檢測(cè)器,小于這個(gè)尺寸的特征將不識(shí)別,CIDetectorTypeFace(0.01 ~ 0.50),CIDetectorTypeText(0.00 ~ 1.00),CIDetectorTypeRectangle(0.00 ~ 1.00)
CIDetectorTracking: 是否開啟面部追蹤 TRUE 或 FALSE
CIDetectorMaxFeatureCount: 設(shè)置返回矩形特征的最多個(gè)數(shù) 1 ~ 256 默認(rèn)值為1
CIDetectorNumberOfAngles: 設(shè)置角度的個(gè)數(shù) 1, 3, 5, 7, 9, 11
CIDetectorImageOrientation: 識(shí)別方向
CIDetectorEyeBlink: 眨眼特征
CIDetectorSmile: 笑臉特征
CIDetectorFocalLength: 每幀焦距
CIDetectorAspectRatio: 矩形寬高比
CIDetectorReturnSubFeatures: 文本檢測(cè)器是否應(yīng)該檢測(cè)子特征,默認(rèn)值是否
*/
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 (本地下載)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS開發(fā)CGContextRef畫圖使用總結(jié)
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)CGContextRef畫圖使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
IOS如何替換電話號(hào)碼中間4位為"-"符號(hào)
這篇文章主要為大家詳細(xì)介紹了IOS如何替換電話號(hào)碼中間4位為-符號(hào)的方法,感興趣的小伙伴們可以參考一下2016-07-07
IOS 開發(fā)之實(shí)現(xiàn)取消tableView返回時(shí)cell選中的問題
這篇文章主要介紹了IOS 開發(fā)之實(shí)現(xiàn)取消tableView返回時(shí)cell選中的問題的相關(guān)資料,希望通過本文能實(shí)現(xiàn)大家想要的功能,需要的朋友可以參考下2017-09-09
詳解iOS App設(shè)計(jì)模式開發(fā)中對(duì)于享元模式的運(yùn)用
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對(duì)于享元模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
iOS中FMDB事務(wù)實(shí)現(xiàn)批量更新數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了iOS中FMDB事務(wù)實(shí)現(xiàn)批量更新數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果
這篇文章主要為大家詳細(xì)介紹了iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實(shí)例
tableView用于顯示一個(gè)垂直滾動(dòng)的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖,這篇文章主要給大家介紹了關(guān)于iOS tableView右側(cè)索引視圖狀態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2021-07-07
使用SDLocalize實(shí)現(xiàn)高效完成iOS多語言工作
這篇文章主要介紹了使用SDLocalize實(shí)現(xiàn)高效完成iOS多語言工作的相關(guān)資料,需要的朋友可以參考下2022-10-10
簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù)
這篇文章主要介紹了簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù) 的相關(guān)資料,需要的朋友可以參考下2018-02-02

