问题
I have a working iOS barcode scanner using the new AVFoundation barcode scanning classes in iOS 7. I can successfully get a string representation of a 1D barcode (for example, a UPC barcode) but I also need to scan two dimensional PDF417 barcodes.
I can't seem to figure out how to get a string representation (or any representation) of the data stored in a PDF417 barcode. The scanner recognizes the barcode and returns a AVMetadataMachineReadableCodeObject but sending the stringValue message to this object returns nil. The description of that method from the documentation says:
The value of this property is an NSString created by decoding the binary payload according to the format of the machine-readable code or nil if a string representation cannot be created
So it would appear that a string representation cannot be created. That's fine...but what do I do then? There doesn't seem to be any other method for this class that will return raw data or anything other useful information about the scanned barcode.
回答1:
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.
来源:https://stackoverflow.com/questions/20255989/using-avfoundation-to-scan-pdf417-barcodes