程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> Swift3.0仿支付寶二維碼掃描效果

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

編輯:更多關於編程

Swift3.0仿支付寶二維碼掃描效果。本站提示廣大學習愛好者:(Swift3.0仿支付寶二維碼掃描效果)文章只能為提供參考,不一定能成為您想要的結果。以下是Swift3.0仿支付寶二維碼掃描效果正文


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

關鍵代碼

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效果:

下載地址:DEMO

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved