国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift3.0仿支付寶二維碼掃描效果

2020-03-09 17:46:42
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Swift3.0二維碼掃描的具體代碼,供大家參考,具體內(nèi)容如下

關(guān)鍵代碼

import AVFoundation//獲取攝像設(shè)備let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)do {  //創(chuàng)建輸入,輸出流  let input = try AVCaptureDeviceInput.init(device: device)  let output = AVCaptureMetadataOutput()  output.rectOfInterest = CGRect(x: 0.1, y: 0, width: 0.9, height: 1)  //設(shè)置代理,并且在主線程刷新  output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)  //初始化鏈接對象 / 高質(zhì)量采集率  session.canSetSessionPreset(AVCaptureSessionPresetHigh)  session.addInput(input)  session.addOutput(output)  //先添加輸入輸出流,后設(shè)置支持的掃碼所支持的格式,不然報(bào)錯(cuò)如下:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes'  //http://stackoverflow.com/questions/31063846/avcapturemetadataoutput-setmetadataobjecttypes-unsupported-type-found  //設(shè)置掃碼支持的編碼格式  output.metadataObjectTypes = [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]  let layer = AVCaptureVideoPreviewLayer(session: session)  layer?.videoGravity = AVLayerVideoGravityResizeAspectFill  layer?.frame = view.layer.bounds  view.layer.insertSublayer(layer!, at: 0)  //開始捕捉  session.startRunning()} catch let error as NSError {  print("errorInfo/(error.domain)")}/// 實(shí)現(xiàn)代理方法extension ScanCodeController:AVCaptureMetadataOutputObjectsDelegate {  func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {    if metadataObjects.count > 0 {      session.stopRunning()      let object = metadataObjects[0]      let string: String = (object as AnyObject).stringValue      if let url = URL(string: string) {      if UIApplication.shared.canOpenURL(url) {        //去打開地址鏈接        _ = self.navigationController?.popViewController(animated: true)        UIApplication.shared.open(url)      } else {        //獲取非鏈接結(jié)果        let alertViewController = UIAlertController(title: "掃描結(jié)果", message: (object as AnyObject).stringValue, preferredStyle: .alert)        let actionCancel = UIAlertAction(title: "退出", style: .cancel, handler: { (action) in          _ = self.navigationController?.popViewController(animated: true)        })        let actinSure = UIAlertAction(title: "再次掃描", style: .default, handler: { (action) in          self.session.startRunning()        })        alertViewController.addAction(actionCancel)        alertViewController.addAction(actinSure)        self.present(alertViewController, animated: true, completion: nil)      }      }    }  }}

DEMO效果:

Swift3.0支付寶二維碼掃描,Swift3.0二維碼掃描,Swift3.0支付二維碼掃描

下載地址:DEMO

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請移步到swift教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 缙云县| 阿鲁科尔沁旗| 宁陵县| 建瓯市| 车致| 贵德县| 三亚市| 剑河县| 宁远县| 罗城| 江山市| 驻马店市| 南开区| 江陵县| 奉新县| 宁乡县| 郎溪县| 新昌县| 崇礼县| 林西县| 剑川县| 封丘县| 东乌珠穆沁旗| 福清市| 新乡县| 得荣县| 丁青县| 凌源市| 厦门市| 宜兴市| 修文县| 庆阳市| 崇信县| 历史| 策勒县| 县级市| 临沂市| 遂宁市| 基隆市| 巴彦淖尔市| 嘉兴市|