device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
try input = AVCaptureDeviceInput(device: device)
} catch let e as NSError {
print(e.localizedDescription)
}
output = AVCaptureMetadataOutput() // 设置代理在主线程中刷新 output?.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
session = AVCaptureSession() // 高质量采集率 session?.canSetSessionPreset(AVCaptureSessionPresetHigh) session?.addOutput(output) session?.addInput(input)
// 特别注意的地方:有效的扫描区域,定位是以设置的右顶点为原点。屏幕宽所在的那条线为y轴,屏幕高所在的线为x轴 let x = ((SCREENHeight - QRCodeWidth - topViewHeight) / 2.0) / SCREENHeight let y = ((SCREENWidth - QRCodeWidth) / 2.0) / SCREENWidth let width = QRCodeWidth / SCREENHeight let height = QRCodeWidth / SCREENWidth output?.rectOfInterest = CGRect(x: x, y: y, width: width, height: height)
output?.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]
preview = AVCaptureVideoPreviewLayer(session: session) preview?.videoGravity = AVLayerVideoGravityResizeAspectFill preview?.frame = self.view.layer.bounds self.view.layer.insertSublayer(preview!, at: 0) session?.startRunning()
fileprivate func scanAnimation() -> CABasicAnimation {
let scanNetAnimation = CABasicAnimation()
// 沿Y轴运动
scanNetAnimation.keyPath = "transform.translation.y"
// 扫描框的高度,注意:这里是实际高度的相反数
scanNetAnimation.byValue = QRCodeWidth
// 动画的持续时间
scanNetAnimation.duration = 1.5
// 动画的重复次数
scanNetAnimation.repeatCount = MAXFLOAT
return scanNetAnimation
}
scanImageView?.layer.add(scanAnimation(), forKey: nil)
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects.count > 0 {
session?.stopRunning()
let metadataObject = metadataObjects[0] as AnyObject
let stringValue: String = metadataObject.stringValue
let vc = QRCodeResultViewController.instantiate()
vc.resultStr = stringValue
self.navigationController?.pushViewController(vc, animated: true)
}
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let requestURL = request.url
if requestURL?.scheme == "http" || requestURL?.scheme == "https" || requestURL?.scheme == "mailto" && navigationType == .linkClicked {
// UIApplication.shared.open(requestURL!, options: [:], completionHandler: nil)
let svc = SFSafariViewController(url: requestURL!)
self.present(svc, animated: true, completion: nil)
}
return true
}
open func open(_ url: URL, options: [String : Any] = [:], completionHandler completion: ((Bool) -> Swift.Void)? = nil)
let svc = SFSafariViewController(url: requestURL!) self.present(svc, animated: true, completion: nil)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有