0 Replies
      Latest reply on Mar 25, 2020 7:57 AM by julian144
      julian144 Level 1 Level 1 (0 points)

        Hello,

         

        I am trying to encode and decode binary data in an 2D readable Barcode (e.g Aztec).

        Unfortunately I cant get it running and I couldn't find something that helped me.

         

        Since iOS 11 it should be possible to create barcodes with binary content due to this apple release notes: https://developer.apple.com/library/archive/releasenotes/General/WhatsNewIniOS/Articles/iOS_11_0.html

        "Added APIs to AV Foundation, Core Image, and SiriKit to support detection, decoding, and creation of barcodes with binary content."

         

        To generate an Aztec Code image, I used the following code snippet:

        let binaryData = Data([UInt8](arrayLiteral: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06))
        guard let filter = CIFilter(name: "CIAztecCodeGenerator") else {
                        return nil
                }
        
         filter.setValue(binaryData, forKey: "inputMessage")
        
         guard let image = filter.outputImage else {
                return nil
         }

         

        I now can use the image and show it in an UIImageView. It shows a nice Aztec code.

         

        But I have a problem, when decoding the Aztec code back to binary. I am using the the iphone camera and a AVCaptureSession with an AVMetadataOutput. The detection of the aztec code works fine, but in the delegate function, I am not able to retrieve the actually binary data

        func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
                captureSession.stopRunning()
        
                if let metadataObject = metadataObjects.first {
                    
                   guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
                   guard let descriptor = readableObject.descriptor as? CIAztecCodeDescriptor else {return}
                     
                    // here I dont get the correct raw bytes (0x01 -0x06)
                    // Instead I only get this: e8864298f0000000
                    let rawBytes = descriptor.errorCorrectedPayload
                    print(rawBytes)
                    let dataString = rawBytes.hex
                    print("dataString: \(dataString)")
        
                    guard let stringValue = readableObject.stringValue else { return }
                  
                }
        }

         

        I am not able to get the correct raw bytes (0x01, 0x02, 0x03, 0x04, 0x05, 0x06). The "stringValue" is empty, because the encoded bytes are binary data and are not a valid string.

        Are the raw bytes (e8864298f0000000) somehow encrypted or encoded in a special format?

         

        Best regards

        Julian