版權聲明:本文爲博主原創文章,未經博主允許不得轉載。如需轉載請標註! https://blog.csdn.net/qq_34347441/article/details/77366179
直接上代碼
// 二維碼識別
fileprivate func recoginzeQRCode(_ image: UIImage) -> String?
{
// 創建二維碼探測器
//1.初始化掃描儀,設置設別類型和識別質量
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
let cgImage = CIImage(image: image)
//2.掃描獲取的特徵組
if let image = cgImage {
let features = detector?.features(in: image)
if let count = features?.count {
guard count > 0 else { return nil }
} else {
return nil
}
//3.獲取掃描結果
let feature = features?.first as? CIQRCodeFeature
return feature?.messageString
} else {
UIApplication.dLog("圖片無法轉成CIImage")
return nil
}
}
// 二維碼識別結果
fileprivate func recoginze(_ image: UIImage) {
DispatchQueue.global().async {
let recognizeResult = self.recoginzeQRCode(image)
if let count = recognizeResult?.characters.count {
let result = count > 0 ? recognizeResult : "無法識別"
DispatchQueue.main.async {
UIApplication.dLog(result)
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: result!)!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(URL(string: result!)!)
}
}
}
}
}
簡書博客地址
https://www.jianshu.com/u/3c7c13f3dc6b