Swift3.0仿支付寶二維碼掃描效果
更新時間:2017年02月20日 16:30:00 作者:Stevin三天三夜的專欄
這篇文章主要為大家詳細(xì)介紹了Swift3.0仿支付寶二維碼掃描效果的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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è)置支持的掃碼所支持的格式,不然報錯如下: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)")
}
/// 實現(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效果:

下載地址:DEMO
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Swift開發(fā)之UITableView狀態(tài)切換效果
這篇文章主要介紹了Swift開發(fā)之UITableView狀態(tài)切換效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Swift利用CoreData如何存儲多種數(shù)據(jù)類的通訊錄
這篇文章主要給大家介紹了關(guān)于Swift利用CoreData如何存儲多種數(shù)據(jù)類的通訊錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Swift中優(yōu)雅處理閉包導(dǎo)致的循環(huán)引用詳解
這篇文章主要給大家介紹了關(guān)于Swift中優(yōu)雅的處理閉包導(dǎo)致的循環(huán)引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

