服务器之家

服务器之家 > 正文

Swift3.0仿支付宝二维码扫描效果

时间:2021-01-03 16:53     来源/作者:Stevin三天三夜的专栏

本文实例为大家分享了Swift3.0二维码扫描的具体代码,供大家参考,具体内容如下

关键代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import AVFoundation
//获取摄像设备
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
  //创建输入,输出流
  let input = try AVCaptureDeviceInput.init(device: device)
  let output = AVCaptureMetadataOutput()
  output.rectOfInterest = CGRect(x: 0.1, y: 0, width: 0.9, height: 1)
  //设置代理,并且在主线程刷新
  output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
  //初始化链接对象 / 高质量采集率
  session.canSetSessionPreset(AVCaptureSessionPresetHigh)
  session.addInput(input)
  session.addOutput(output)
 
  //先添加输入输出流,后设置支持的扫码所支持的格式,不然报错如下: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
  //设置扫码支持的编码格式
  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)")
}
 
/// 实现代理方法
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 {
        //获取非链接结果
        let alertViewController = UIAlertController(title: "扫描结果", 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仿支付宝二维码扫描效果

下载地址:DEMO

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部