Lê Khánh Vinh Lê Khánh Vinh - 1 month ago 18
iOS Question

Swift 3 detect multiple barcode in 1 line

Hi I'm using

swift3
and
AVFoundation
to detect barcode for a bill. My bill have up to 3 barcode in 1 line. How can we combine total barcode into 1 string and make operation stop only after all barcode in the line have been detected and combined into 1 string?

here is my sample bill

enter image description here

There is 3 barcode at the bottom

here is my AVCaptureMetadataOutputObjectsDelegate function

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)
}
}


How can we modify the above to scan all 3 barcode and combine them and notify
Viewcontroller
when all the barcode have been decoded? Any help is much appreciate. Thanks

Avt Avt
Answer

Just do not stop scanning unless 3 barcodes are detected and then combine codes:

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)
}
Comments