iOS開發(fā)教程之識別圖片中二維碼功能的實現(xiàn)
前言
大家應(yīng)該都知道在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中可以傳的參數(shù) 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 // 設(shè)置返回矩形特征的最多個數(shù) 1 ~ 256 默認(rèn)值為1 public let CIDetectorNumberOfAngles: String // 設(shè)置角度的個數(shù) 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 // 文本檢測器是否應(yīng)該檢測子特征,默認(rèn)值是否
下面是二維碼識別的實例代碼
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.創(chuàng)建識別器,3個參數(shù) ofType:識別類型 CIDetectorTypeFace 面部識別 CIDetectorTypeText 文本識別 CIDetectorTypeQRCode 條碼識別 CIDetectorTypeRectangle 矩形識別 context:上下文,默認(rèn)傳nil options:識別精度 CIDetectorAccuracyLow 低精度,識別速度快 CIDetectorAccuracyHigh 高精度,識別速度慢 */ let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: options) /** 5.獲取識別結(jié)果,2個參數(shù) in:需要識別的圖片 options:需要識別的特征 CIDetectorMinFeatureSize: 指定最小尺寸的檢測器,小于這個尺寸的特征將不識別,CIDetectorTypeFace(0.01 ~ 0.50),CIDetectorTypeText(0.00 ~ 1.00),CIDetectorTypeRectangle(0.00 ~ 1.00) CIDetectorTracking: 是否開啟面部追蹤 TRUE 或 FALSE CIDetectorMaxFeatureCount: 設(shè)置返回矩形特征的最多個數(shù) 1 ~ 256 默認(rèn)值為1 CIDetectorNumberOfAngles: 設(shè)置角度的個數(shù) 1, 3, 5, 7, 9, 11 CIDetectorImageOrientation: 識別方向 CIDetectorEyeBlink: 眨眼特征 CIDetectorSmile: 笑臉特征 CIDetectorFocalLength: 每幀焦距 CIDetectorAspectRatio: 矩形寬高比 CIDetectorReturnSubFeatures: 文本檢測器是否應(yīng)該檢測子特征,默認(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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS開發(fā)CGContextRef畫圖使用總結(jié)
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)CGContextRef畫圖使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04IOS 開發(fā)之實現(xiàn)取消tableView返回時cell選中的問題
這篇文章主要介紹了IOS 開發(fā)之實現(xiàn)取消tableView返回時cell選中的問題的相關(guān)資料,希望通過本文能實現(xiàn)大家想要的功能,需要的朋友可以參考下2017-09-09詳解iOS App設(shè)計模式開發(fā)中對于享元模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對于享元模式的運用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS中FMDB事務(wù)實現(xiàn)批量更新數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了iOS中FMDB事務(wù)實現(xiàn)批量更新數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實例
tableView用于顯示一個垂直滾動的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖,這篇文章主要給大家介紹了關(guān)于iOS tableView右側(cè)索引視圖狀態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2021-07-07使用SDLocalize實現(xiàn)高效完成iOS多語言工作
這篇文章主要介紹了使用SDLocalize實現(xiàn)高效完成iOS多語言工作的相關(guān)資料,需要的朋友可以參考下2022-10-10