識別(讀取)圖片二維碼

  1. 二維碼讀取包括:
    1. 直接對靜態(tài)的圖片進行二維碼識別,最低支持iOS8.0
    2. 利用攝像頭掃描識別(動態(tài)圖片),需要真機設備

讀取圖片二維碼 <- swift

  1. 導入框架(可選)

    import CoreImage
    
  2. 實現(xiàn)代碼

    @IBOutlet weak var qrCodeImage: UIImageView!
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 0. 創(chuàng)建一個上下文
        let context = CIContext()
    
        // 1. 創(chuàng)建一個二維碼探測器
        let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
    
        // 2. 探測二維碼圖片的特征
        guard let image = qrCodeImage.image else {
            return
        }
        let imageCI = CIImage(image: image)
        let features = detector.featuresInImage(imageCI!)
    
        // 3. 處理識別到的特征值
        for feature in features {
    
            if feature.isKindOfClass(CIQRCodeFeature) {
                let qrCodeFeature = feature as! CIQRCodeFeature
    
                print(qrCodeFeature.messageString)
    
                // 繪制識別到的二維碼圖片,詳見“5.識別二維碼- 描繪邊框”
            }
        }
    
        // 將重新繪制的圖片顯示(可選),沒繪制可以不設置
        // qrCodeImage.image = tempImage
    
        // 4. 彈框顯示內(nèi)容
        // 4.1 創(chuàng)建彈框控制器
        let alterVC = UIAlertController(title: "結(jié)果", message: resultStrs.description, preferredStyle: UIAlertControllerStyle.Alert)
    
        // 4.2 給控制器設置行為
        let action = UIAlertAction(title: "關閉", style: UIAlertActionStyle.Default) { (action: UIAlertAction) in
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    
        // 4.3 添加行為
        alterVC.addAction(action)
    
        // 4.4 彈出提示內(nèi)容
        presentViewController(alterVC, animated: true, completion: nil)
    }
    
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容