- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- Intermediate iOS 11 Programming
- iOS tutorial 8:使用IBInspectable 和
本文是Intermediate iOS 11 Programming with Swift 4系列 的 第 十一 篇.
11.0可以在这里生成自己的二维码哦 !
话不多说, 先来创建 二维码扫描demo吧.
可以在 这里 下载 模板
首先 实现 AVFoundation Framework
导入 import AVFoundation 框架
先定义几个用的实例变量
var captureSession = AVCaptureSession()
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?
Video Capture的实现
为了实现实时捕获,我们需要做的就是:
看一下后面的摄像装置。
将AVCaptureSession对象的输入设置为合适的AVCaptureDevice以进行视频捕获
代码如下:
// Get the back-facing camera for capturing videos
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)
guard let captureDevice = deviceDiscoverySession.devices.first else {
print("Failed to get the camera device")
return
}
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Set the input device on the capture session.
captureSession.addInput(input)
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
相机权限
相机权限完成编辑后,部署应用程序并再次在真正的设备上运行它。点击扫描按钮应该会打开内置摄像头并开始拍摄视频。然而,此时消息标签和顶部栏是隐藏的。您可以通过添加以下代码行来修复它。这将移动消息标签和顶部栏显示在视频层的顶部.
// Move the message label and top bar to the front
view.bringSubview(toFront: messageLabel)
view.bringSubview(toFront: topbar)
更改后重新运行应用程序。消息标签没有检测到二维码现在应该出现在屏幕上
QR Code Reading 的实现
到目前为止,这款应用看起来很像视频捕捉应用。它怎么能扫描二维码,把代码翻译成有意义的东西呢?”该应用本身已经能够检测二维码。我们只是没有意识到这一点。下面是我们将如何调整应用程序:
当检测到二维码时,应用程序会用绿色方框高亮显示代码
二维码将被解码,解码信息将显示在屏幕底部
初始化绿色框
为了突出二维码,我们首先创建一个UIView对象,并将其边框设置为绿色。在viewDidLoad方法的do块中添加以下代码
// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubview(toFront: qrCodeFrameView)
}
qrCodeFrameView变量在屏幕上是不可见的,因为UIView对象的大小默认设置为0。之后,当检测到二维码时,我们会改变它的大小,把它变成一个绿色的盒子
解码 二维码
如前所述,当AVCaptureMetadataOutput对象识别二维码时,将调用AVCaptureMetadataOutputObjectsDelegate的委托方法
optional func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)
到目前为止,我们还没有实现这个方法;这就是为什么这个应用不能翻译二维码。为了捕获二维码并解码信息,我们需要实现对元数据对象进行额外处理的方法
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR code is detected"
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObject.ObjectType.qr {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
}
}
}
网友评论