加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

二维码---相册识别 swift

发布时间:2020-12-14 06:36:02 所属栏目:百科 来源:网络整理
导读:swift 3.0 xcode 8.1 需要在info.plist 添加 Privacy - Photo Library Usage Description YES import UIKitclass ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { override func viewDidLoad() { super

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.
    }


}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读