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 解決UICollectionView 計算 Cell 大小的問題
本文主要介紹iOS UICollectionView,這里給大家一個實例代碼作為參考,并指出經(jīng)常遇到的問題和解決辦法,希望能幫助有需要的小伙伴2016-07-07
CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決
這篇文章主要介紹了CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下2016-04-04

