Swift 3在1行中检测到多个条形码

时间:2016-11-03 10:14:51

标签: ios barcode

您好我使用swift3AVFoundation来检测帐单的条形码。我的帐单在一行中最多有3个条形码。我们如何将总条形码组合成1个字符串,并且只有在检测到行中的所有条形码并将其组合成1个字符串后才能停止操作?

这是我的样本账单

enter image description here

底部有3个条形码

这是我的AVCaptureMetadataOutputObjectsDelegate函数

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
        // This is the delegate'smethod that is called when a code is readed
        for metadata in metadataObjects {
            let readableObject = metadata as! AVMetadataMachineReadableCodeObject
            let code = readableObject.stringValue


            self.dismiss(animated: true, completion: nil)
            self.delegate?.barcodeReaded(barcode: code!)
            print(code!)
            print(readableObject.type)
        }
    }

我们如何修改上述内容以扫描所有3个条形码并将它们合并,并在所有条形码解码后通知Viewcontroller?任何帮助都非常感激。感谢

1 个答案:

答案 0 :(得分:3)

除非检测到3个条形码,然后组合代码,否则不要停止扫描:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    // This is the delegate'smethod that is called when a code is headed

    guard metadataObjects.count == 3 else { return }

    var finalString: String = ""

    for metadata in metadataObjects {
        let readableObject = metadata as! AVMetadataMachineReadableCodeObject
        let code = readableObject.stringValue

        finalString.append(code!)
        print(code!)
        print(readableObject.type)
    }

    self.dismiss(animated: true, completion: nil)
    self.delegate?.barcodeReaded(barcode: finalString)
}