码迷,mamicode.com
首页 > 编程语言 > 详细

Swift CIDetector识别相册的二维码

时间:2017-11-03 00:10:26      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:margin   smi   nali   二维码   self   bsp   idf   nis   nal   

 

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

标签:margin   smi   nali   二维码   self   bsp   idf   nis   nal   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!