二维码---相册识别 swift

swift 3.0 xcode 8.1

需要在info.plist 添加 Privacy - Photo Library Usage Description YES
import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton.init(type: UIButtonType.custom)
        button.frame = CGRect.init(x: 20,y: 300,width: 300,height: 40)
        button.setTitle("点击获取相册二维码图片信息",for: UIControlState.normal)
        button.setTitleColor(UIColor.black,for: UIControlState.normal)
        button.addTarget(self,action: #selector(self.getimage),for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
 
    }
    
    
    func getimage() {
        
        let imagePickCST = UIImagePickerControllerSourceType.photoLibrary
        let imagePickC = UIImagePickerController.init()
        imagePickC.sourceType = imagePickCST
        imagePickC.delegate = self
        self.present(imagePickC,animated: true,completion: nil)
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [String : Any]) {
        
        picker.dismiss(animated: true,completion: nil)
        
        //获取到的原始图片
        let image = info[UIImagePickerControllerOriginalImage]
        
        let imageData = UIImagePNGRepresentation(image as! UIImage)
        let ciImage = CIImage(data: imageData!)
        
        //初始化一个检测器
        let detector = CIDetector(ofType: CIDetectorTypeQRCode,context: nil,options: [CIDetectorAccuracy:CIDetectorAccuracyLow])
        
        //检测到的结果数组
        let array = detector?.features(in: ciImage!)
        
        //取出数组中的第一个结果
        if (array?.count)! > 0 {
            let result: CiqrCodeFeature = array!.first as! CiqrCodeFeature
            print(result.messageString!)
        }
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }


}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...