首页 > 编程语言 > 详细

Swift CIDetector识别相册的二维码

时间:2017-11-02 23:41:06      阅读:342      评论:0      收藏:0      [点我收藏+]

 

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识别相册的二维码

原文:http://www.cnblogs.com/chenxiaopao/p/7775141.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!