长按二维码识别
直接上代碼
// 二維碼識別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? CIQRCodeFeaturereturn 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
總結
- 上一篇: 第九届蓝桥杯——星期一
- 下一篇: c语言scanf中 作用,c语言中sca