Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AVFoundation to scan PDF417 barcodes

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.

like image 850
stackunderflow Avatar asked Oct 31 '25 16:10

stackunderflow


1 Answers

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.

like image 196
Alexandru Motoc Avatar answered Nov 03 '25 09:11

Alexandru Motoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!