Read binary QR Code with AVFoundation

2020-02-05 07:37发布

Is it possible to read a binary encoded QR code with AVFoundation?

I can get a AVMetadataMachineReadableCodeObject object of .type AVMetadataObjectTypeQRCode, however this only has a stringValue property, which won't work, because the data contained in the QR code can't be converted to a string friendly representation.

Should I use ZXing instead?

Thanks

标签: ios qr-code
2条回答
做个烂人
2楼-- · 2020-02-05 07:59

The raw data does exist in your AVMetadataMachineReadableCodeObject, but it's not available through a public getter.

However, you can use KVO to extract it, but Apple might reject your app. Also, future iOS versions might change their private APIs and your code could become invalid (because of the hardcoded private keys).

Swift:

readableCodeObject.valueForKeyPath("_internal.basicDescriptor")!["BarcodeRawData"]

Objective-C

[readableCodeObject valueForKeyPath:@"_internal.basicDescriptor"][@"BarcodeRawData"];

I tested this for iOS 8 and 9.

查看更多
Deceive 欺骗
3楼-- · 2020-02-05 08:00

I was able to solve this issue by Base64 encoding the data in the QR code. This obviously won't work if you're not also generating the QR codes but could be option for people that are.

We were running into the upper limit of data that can be stored in a QR code but by compressing the data (we used zlib) and then Base64 encoding the compressed data, so long as your data compresses to less than 75% of its original size you get some additional capacity and can use the stringValue property to get your data back out, you just have to Base64 decode and then decompress to get the original data back.

Even if you're starting with binary data that isn't very compressible, so long as you can handle the overhead of Base64 and still be within the limitations of QR codes this may be a viable option that avoids working around the fact that AVMetadataMachineReadableCodeObject seems to want to work with string values.

查看更多
登录 后发表回答