Swift CIDetector识别相册的二维码

Posted 小炮陈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift CIDetector识别相册的二维码相关的知识,希望对你有一定的参考价值。

 

import UIKit

 

class firstViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

 

    var picker :UIImagePickerController!

 

    @IBAction func button(_ sender: UIButton) {

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){

            picker = UIImagePickerController()

            picker.delegate = self

            picker.sourceType = .photoLibrary

            self.present(picker, animated: true, completion: nil)

        }

    }

 

 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        //获取选择的图片

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage

 

        //二维码读取

        DispatchQueue.global().async {

            let imageData = UIImagePNGRepresentation(image)

            let ciImage:CIImage=CIImage(data: imageData!)!

            let detector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil,options:[CIDetectorAccuracy:CIDetectorAccuracyHigh])

            let feature = detector?.features(in: ciImage)

            let f = feature?.first as! CIQRCodeFeature

            print(f.messageString ?? "")

        }

 

        //返回

        picker.dismiss(animated: true, completion: {

            () -> Void in

        })

 

    }

}

以上是关于Swift CIDetector识别相册的二维码的主要内容,如果未能解决你的问题,请参考以下文章

Swift 中的 CIDetector 透视和裁剪

android选取系统相册图片后,识别图中二维码

[iOS]CIDetector之CIDetectorTypeFace人脸识别

怎样识别照片中的二维码

QRCode 扫描二维码扫描条形码相册获取图片后识别生成带 Logo 二维码支持微博微信 QQ 二维码扫描样式

如何用Xcode8和Swift 3 构建条形码/二维码识别应用