Swift3:我想读取和计数二维码
Posted
技术标签:
【中文标题】Swift3:我想读取和计数二维码【英文标题】:Swift3: I would like to function read and count QR code 【发布时间】:2017-08-28 02:45:15 【问题描述】:我正在构建一个二维码阅读器应用。我写了一个函数来读取和计算二维码。我可以显示动态按钮标题的计数;但是,二维码的值不是像1、2、3那样计算的。显示自然的二维码值。
我的视图控制器:
class LentScanViewController: QRScannerViewController
var device_name = ""
var countNum = 0
@IBOutlet weak var LentButton: UIButton!
@IBAction func lentButton(_ sender: Any)
performSegue(withIdentifier: "toLentTable",sender: device_name)
override func viewDidLoad()
super.viewDidLoad()
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)
if metadataObjects == nil || metadataObjects.count == 0
qrCodeFrameView?.frame = CGRect.zero
return
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil
device_name = metadataObj.stringValue
countNum = countNum + 1
LentButton.setTitle(String(countNum) + "Lent", for:[])
【问题讨论】:
【参考方案1】:请尝试:
var qrCodes:[String] = [] // declare it as member variable in the class
然后调用
if metadataObj.stringValue != nil
let device_name = metadataObj.stringValue
if !qrCodes.contains(device_name!)
qrCodes.append(device_name!)
countNum = countNum + 1
messageLabel.text = String(countNum) + "Lent"
【讨论】:
谢谢。但此代码是显示 metadataObj 值。我想如果扫描一个二维码显示 1. 再扫描一个显示 2. 就像这个视频youtube.com/watch?v=zKMyp17Squ8&feature=youtu.be 编辑了答案。请检查。以上是关于Swift3:我想读取和计数二维码的主要内容,如果未能解决你的问题,请参考以下文章
用于捕获 QR 码的代码在 Xcode8 和 Swift3 中似乎效果不佳