3

I'm trying to read information from a barcode type PDF417 with the AVFoundation library.

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    if let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
        print(metadataObj.stringValue)//prints 0357379329
    }
}

At the point of execution for the didOutput method in the "AVCaptureMetadataOutputObjectsDelegate", I can capture the AVMetadataObject and its stringValue; nevertheless the stringValue has not complete information contained in the barcode, it generates something like this:

0357379329

I can see more information when I use paid applications like BlinkId.

According to this topic, I tried to use the "_internal.basicDescriptor" method:

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    if let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
        if let basic_descriptor = metadataObj.value(forKeyPath: "_internal.basicDescriptor") as? NSObject {
            if let barcodeRawData = basic_descriptor.value(forKeyPath: "BarcodeRawData") as? NSData {
                let barcode_raw_data_length = barcodeRawData.length
                let memory_layout = MemoryLayout<UInt8>.size
                let count = barcode_raw_data_length / memory_layout
                var array = [UInt8](repeating: 0, count: count)//Array con 1050 elementos. Cada elemento es igual a cero
                barcodeRawData.getBytes(&array, length:count * memory_layout)
                let data_character = array.reduce("", { $0 + String(format: "%c", $1)})
                print(data_character)
            }
        }
    }
}

data_character prints a string with something like this that I don't know how to interpret:

ÍHÕÛ+ÝY-<4mI ËÇ;ÚÖ ÂÿûJhiòþ ^HT¶îèm½C3&îðÜ,Feæ»0æ¤öc$¯#m,WÆç4Pá$Ññü0njtYÜ0U¼ÇçA°'£aR.)9 Êq3uúù7=,~mV½Qý;áu½7ó&æiTÝJ¸ËõÚAZähc¨à¼Ø²BsÇKZÖëu-Õß/³©îprX×Ijßçól{¡¿ý...

I would like to know any of these:

  1. How can I get the AVMetadataObject stringValue with the whole info?
  2. How can I interpret the string generated with the "_internal.basicDescriptor"?
  3. Is there another method to get info from a pdf417 in swift 4?

Thanks!

0 Answers0