OGeek|极客世界-中国程序员成长平台

标题: ios - Swift 3 在 1 行中检测多个条码 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:16
标题: ios - Swift 3 在 1 行中检测多个条码

您好,我正在使用 swift3AVFoundation 来检测账单的条形码。我的账单在 1 行中最多有 3 个条形码。怎样才能将全部条码合并为一串,并在检测到该行中的所有条码并合并为一串后才停止操作?

这是我的账单样本

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?非常感谢任何帮助。谢谢



Best Answer-推荐答案


不要停止扫描,除非检测到 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)
}

关于ios - Swift 3 在 1 行中检测多个条码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40398896/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4