3

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.

stackunderflow
  • 754
  • 7
  • 18
  • Is it an encrypted barcode? PDF417 is often used on driver's licenses and some states encrypt the data so it is restricted for official use. – Enrico Nov 28 '13 at 20:26
  • I'm just testing with a free online barcode generator, creating simple alphabetical string PDF417 barcodes, but the scanner won't return a non-nil stringValue for any of them. – stackunderflow Nov 28 '13 at 20:56

1 Answers1

3

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.

Alexandru Motoc
  • 582
  • 7
  • 14
  • I have the exact same problem as @stackunderflow. I wonder why Apple would assume that the PDF417 wholly consists of textual data. Some driver's licenses (in PDF417 format) contain JPEG2000 image data. If I use `stringValue`, then much of that image data will get lost to the extent that it will likely no longer be a JPEG2000 format. – BigSauce Nov 01 '18 at 03:39