swift4.0 扫一扫功能 代码

最近在做一个swift项目 要写扫一扫功能 上网找的代码基本都是3.0以前的 一用全都是错的

下面把最近的代码记录一下, 由于时间紧迫,只放上功能代码

func setupScanSession(){

do{

//设置捕捉设备

guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {

Tool.confirm(title: "温馨提示", message: "未得到摄像头", controller: self)

return

}

//设置设备输入输出

let input = try AVCaptureDeviceInput(device: device)

let output = AVCaptureMetadataOutput()

output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

//设置会话

captureSession.canSetSessionPreset(AVCaptureSession.Preset.high)

if captureSession.canAddInput(input){

captureSession.addInput(input)

}

if captureSession.canAddOutput(output){

captureSession.addOutput(output)

}

//设置扫描类型(二维码和条形码)

output.metadataObjectTypes = [

AVMetadataObject.ObjectType.qr,

AVMetadataObject.ObjectType.code39,

AVMetadataObject.ObjectType.code128,

AVMetadataObject.ObjectType.code39Mod43,

AVMetadataObject.ObjectType.ean13,

AVMetadataObject.ObjectType.ean8,

AVMetadataObject.ObjectType.code93]

//预览图层

let scanPreviewLayer = AVCaptureVideoPreviewLayer(session:captureSession)

scanPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

scanPreviewLayer.frame = view.layer.bounds

cameraView.layer.insertSublayer(scanPreviewLayer, at: 0)

//设置扫描区域

NotificationCenter.default.addObserver(forName: NSNotification.Name.AVCaptureInputPortFormatDescriptionDidChange, object: nil, queue: nil, using: { (noti) in

output.rectOfInterest = (scanPreviewLayer.metadataOutputRectConverted(fromLayerRect: self.scanPane.frame))

})

//保存会话

captureSession.startRunning()

} catch {

//摄像头不可用

Tool.confirm(title: "温馨提示", message: "摄像头不可用", controller: self)

return

}

}

//开始扫描viewWillAppear中调用就行

func scannerStart(){

captureSession.startRunning()

}

func scannerStop() {

captureSession.stopRunning()

}

/// 实现代理方法

//注意这个代理方法的名字变了!!!不要只用了上面的代码 发现不走代理 我就被坑了

extension ScannerViewController: AVCaptureMetadataOutputObjectsDelegate {

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

if metadataObjects.count > 0 {

//            scanSession?.stopRunning()

scannerStop()

let object = metadataObjects[0]

let string: String = (object as AnyObject).stringValue

if let url = URL(string: string) {

if UIApplication.shared.canOpenURL(url) {

//去打开地址链接,这是一个自定义的webView

let result = WebViewController()

result.url = string;

result.hidesBottomBarWhenPushed = true;

self.navigationController?.pushViewController(result, animated: true)

} 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.scannerStart()

})

alertViewController.addAction(actionCancel)

alertViewController.addAction(actinSure)

self.present(alertViewController, animated: true, completion: nil)

}

}

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,867评论 0 6
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,742评论 0 3
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • 二十几日,能时常念叨分享的,不是那些标志性的大景区,而是还保留着原生味道的一些景致,还有就是几位仅是一面之...
    水妮儿阅读 710评论 2 2
  • 第一次感觉到父亲老去,是在大学期间,他操心妹妹的婚事。 第一次感觉到母亲的老去,是在父亲发生意外住院。 父亲的老去...
    书青麦阅读 520评论 0 1