Override the stringValue property of AVMetadataMachineReadableCodeObject or create an alternative that outputs binary data

折月煮酒 提交于 2019-12-13 14:03:41

问题


Okay, I get it, possible duplicate of Read binary QR Code with AVFoundation but I'll try to tackle this issue from a different angle.

I'm trying to scan a barcode (in this case, Aztec) in my swift app. It works for barcodes that have regular string data encoded. For my app though, I need to be able to scan a certain type of barcode (read more about this on SO) that stores the data in binary format. Sadly, stringValue of AVMetadataMachineReadableCodeObject is (per Apple's docs)

The value of this property is an NSString created by decoding the binary payload according to the format of the machine-readable code

so the output gets garbled, truncated and unusable (It's a zlib-encoded data stream).

My question is: is there a way to get to this binary payload other that stringValue? Can I override part of AVMetadataMachineReadableCodeObject and add my own binaryValue or something like it.

I'm willing to try anything, but I'd love this to work natively without resorting to ZXing or some other library, as this is a pretty compact project. If you know this to be working with a library, feel free to add a comment though.

Disclaimer: I'm coding this in Swift, but I think I could manage to abstract this from Obj-C code as well, if that is what you know.


回答1:


With ZXing I found a solution (that is still work in progress, but I managed to inflate the deflated content with success using libz).

Go to ZXAztecDecoder.m and in the method + (NSString *)encodedData:(ZXBoolArray *)correctedBits you can use code given from + (int)readCode:(ZXBoolArray *)rawbits startIndex:(int)startIndex length:(int)length to create unsigned char.

So wherever I found a line of code trying to add characters to the NSString I also filled up a buffer of unsigned char (casted from int returned fro the readCode:rawBits:startIndex:length method).

I used the Aztec barcode from the question about parsing Deutche Ban tickets with Python that you posted. I was able to inflate the content and everything. The only problem is that inflate does not work for my Railway Company... So I'm left with figuring out what kind of algorithm that should inflate a stream that starts with 55 4E or 55 8E ...

Good luck! Your other questions I found helped a lot, so thanks.

EDIT: Some code that might help: https://gist.github.com/johanhar/a421a14bef2f06ee2340




回答2:


As it turns out, the raw binary data does exist in the AVMetadataMachineReadableCodeObject and can be read into an NSData object, but the method comes with a catch.

In my case I was trying to read an Aztec barcode, but it seems it works with anything iOS can detect. This SO answer has the solution: Read binary QR Code with AVFoundation



来源:https://stackoverflow.com/questions/34438301/override-the-stringvalue-property-of-avmetadatamachinereadablecodeobject-or-crea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!