如何使用AVFoundation Swift 4从条形码扫描仪解码PDF 417数据

时间:2017-10-01 17:24:03

标签: swift barcode-scanner key-value-observing swift4 pdf417

我正在使用PDF 417 Scanner和Swift 4,并使用AVFoundation,但如果我使用AVMetadataMachineReadableCodeObject,之后我尝试使用stringValue解码数据我得到了限制数据(只是几个字符),所以如何在第一个实例中实现它:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRect.zero
            messageLabel.text = "No barcode is detected"
            return
        }


        for meta in metadataObjects {
            // Get the metadata object.
            let metadataObject = meta as! AVMetadataMachineReadableCodeObject

            if supportedCodeTypes.contains(metadataObject.type) {

                let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObject)
                qrCodeFrameView?.frame = barCodeObject!.bounds

                if metadataObject.stringValue != nil {
                    messageLabel.text = metadataObject.stringValue
                }
            }
        }
    }

在尝试了解更多关于PDF 417和AVFoundation之后,我发现了一些与如何检索二进制数据相关的帖子

Using AVFoundation to scan PDF417 barcodes

Reading raw bytes from an Aztec barcode via AVMetadataMachineReadableCodeObject yields unexpected result

之后我实现了以下

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRect.zero
            messageLabel.text = "No QR/barcode is detected"
            return
        }


        for meta in metadataObjects {
            // Get the metadata object.
            let metadataObject = meta as! AVMetadataMachineReadableCodeObject

            let descriptor = metadataObject.value(forKeyPath: "_internal.basicDescriptor") as? NSDictionary;
            let barcodeData = descriptor!["BarcodeRawData"] as? NSData

            let count = (barcodeData?.length)! / MemoryLayout<UInt8>.size
            var array = [UInt8](repeating: 0, count: count)

            barcodeData?.getBytes(&array, length:count * MemoryLayout<UInt8>.size)
            print(array)
            let data = array.reduce("", { $0 + String(format: "%c", $1)})
            print(data)
            print("-----------------")
            if supportedCodeTypes.contains(metadataObject.type) {

                let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObject)
                qrCodeFrameView?.frame = barCodeObject!.bounds

                if metadataObject.stringValue != nil {
                    messageLabel.text = data
                }
            }
        }
    }

在上面的代码中,我调试了对象descriptor我做了po descriptor并检索了以下信息

 po descriptor
▿ Optional<NSDictionary>
  ▿ some : 4 elements
    ▿ 0 : 2 elements
      - key : BarcodeType
      - value : PDF417
    ▿ 1 : 2 elements
      - key : BarcodeRawData
      - value : <cd014803 5c000900 b4001501 95008503 00000000 00000000 00000000 00000000 00000000 00000000 8403dd01 59022d03 3c033403 6d034903 85030000 00000000 00000000 00000000 84034a03 f8009800 2b018503 00000000 84034903 07005b00 b500bb00 58004300 0f001100 0c028503 00000000 00000000 00000000 00000000 00000000 00000000 00008403 05000602 3c009401 85030000 00000000 00000000 00000000 00000000 00000000 00000000 84034d00 02014202 0d008503 00000000 00000000 00000000 00000000 00000000 00000000 00008403 3c007001 58018503 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00008403 48035403 8603b600 af028000 0c020000 85038400 5c02d702 9502ff01 0000dc02 8f01f801 b000da00 ac010f03 ea01da02 af00d100 1f01bf02 07018d00 a6022602 18014102 f5005201 7a000a02 0c003801 9d028600 01017002 1d01df00 9d028602 9c02fd00 2e012b00 db022102 f0006a00 ee003d03 7d00e100 c801e802 ad010403 8f00c901 48027801 0d00af00 95005602 1d02d400 9c008702 5903ba02 2d000801 2001a500 d8000402 20013103 21007f00 9e002c01 97024f00 81035302 a700a200 c2009000 ca008403 40033403 1902a702 9c032b01 d0003d03 39016202 84032d03 8b013f00 74038503 85008e00 8c009900 84033203 2d026e03 cd027502 8503e400 a9015400 0600fe02 da009501 56004603 ae00b500 3901dd02 10034b00 8c01a402 04000201 c001da00 28037703 e0021002 5c00a600 15034e02 4c01ac01 9f003003 7d001d01 e200c102 97014f02 2900fa00 2500bf02 99012302 ae007f02 73017902 e3023301 0d007201 35010203 c6007002 84004f02 a3024201 f502d602 49035e01 9b000601 2800a301 7702b000 4d032503 3a022202 81008103 2800a902 91017400 86008403 17012901 dd016600 5f033d03 8503d500 6501f200 49036900 df00a102 15003e03 0d010901 b0005802 56019001 52016603 58028702 b9013e01 a5023b01 09020702 62014603 be01df02 47005501 63010902 f9010601 67008e00 41008700 84036c03 eb02ed02 94024903 53035001 1902dc00 8503d700 f6006203 9f02b800 d7006801 49011000 0b024000 eb009700 14008b02 9e01d200 55033903 07031401 b5015a00 2e012402 6c01e102 e200c102 9502d700 9d02ed00 7d002900 99001b01 b6022000 64006101 4a02ba01 7700a302 96019900 e502d702 b100c500 34000f00 d501b400 c4008403 84038403 84038403 84038403 84038403 84034f01 bf026300 1d017f02 38033502 34037d03 f100f901 2d000e01 6e014003 03022303 5b00cb02 27018801 60004002 3800b900 2b019601 01011501 5e03f900 5303b100 01024700 8203e101 9e018302 a6018502 8403a002 6b01e002 3c02b801 0702ed01 48015101 7f031c03 55025502 52009101 14039701 97037700 63034801 4400>
    ▿ 2 : 2 elements
      - key : DecodeConfidence
      - value : 1
    ▿ 3 : 2 elements
      - key : CodeLocation
      ▿ value : 4 elements
        ▿ 0 : 2 elements
          ▿ 0 : 2 elements
            - key : X
            - value : 41
          ▿ 1 : 2 elements
            - key : Y
            - value : 550
        ▿ 1 : 2 elements
          ▿ 0 : 2 elements
            - key : X
            - value : 44
          ▿ 1 : 2 elements
            - key : Y
            - value : 670
        ▿ 2 : 2 elements
          ▿ 0 : 2 elements
            - key : X
            - value : 732
          ▿ 1 : 2 elements
            - key : Y
            - value : 661
        ▿ 3 : 2 elements
          ▿ 0 : 2 elements
            - key : X
            - value : 733
          ▿ 1 : 2 elements
            - key : Y
            - value : 542

如果我将BarcodeRawData从hexa转换为ascii(在这种特殊情况下)我得到了

ÍH\   ´ÝY-<4mIJø+I[µ»XC<MB
<pXHT¶¯\×ÿÜø°Ú¬êگѿ¦&AõRz
8pßý.+Û!ðjî=}áÈè­ÉHx
¯VÔYº- ¥Ø 1!,OS§¢ÂÊ@4§+Ð=9b-?t2-nÍuä©TþÚVF®µ9ÝK¤ÀÚ(wà\¦NL¬0}âÁO)ú%¿#®syã3
r5ÆpO£BõÖI^(£w°M%:"(©t)Ýf_=ÕeòIiß¡>
   °XVRfX¹>¥;    bF¾ßGUc    ùgAlëíISPÜ×öb¸×hI@ëÒU9µZ.$láâÁ×í})¶ daJºw£å×±Å4Õ´ÄO¿c854}ñù-n@#[Ë'`@8¹+^ùS±Gᦠkà<¸íHQUURwcHD

这让我问下面的问题:

  1. 如何解码二进制数据AVFoundation中的字符串值以及AVFoundation限制信息的原因

  2. 如果来自描述符的二进制数据是压缩,我可以使用哪些算法来解压缩数据?

  3. 我可以通过forKeyPath,KVO从metadataObject中检索另一个对象。

  4. 还有哪些问题需要解决?

  5. 为什么这个PDF417应用程序运行良好?

0 个答案:

没有答案