现在苹果iOS系统已经原生支持了二维码扫描的功能,使用原生来扫描需要导入AVFoundation。
扫描准备
一、获取摄像设备:
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
}
使用动画:
我们在创建界面的时候,扫描框有一个UIImageView,我们需要将我们的动画添加到这个ImageView上面。
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)
在Safari中打开连接。不过最好是把事件控制在自己的程序中,在iOS 9 之后,苹果引入了 SFSafariViewController 这个类,可以用这个类来显示需要浏览的网页。
let svc = SFSafariViewController(url: requestURL!)
self.present(svc, animated: true, completion: nil)