read
One new feature in iOS7 which didn’t get announced anywhere (as far as I can tell anyway) if the ability to read barcodes.
There’s a new output for AVCapture called AVCaptureMetadataOutput - this supports a large number of supported formats including 1D and 2D barcodes.
Getting it up and running is pretty simple. Do the normal code for capturing output from the camera and then for the output do the following:
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]
init];
[self.session addOutput:output];
// see what types are supported (do this after adding otherwise the output reports nothing supported
NSSet *potentialDataTypes = [NSSet setWithArray:@[
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeUPCECode]];
NSMutableArray *supportedMetaDataTypes =
[NSMutableArray array];
for(NSString *availableMetadataObject in
output.availableMetadataObjectTypes) {
if([potentialDataTypes
containsObject:availableMetadataObject]) {
[supportedMetaDataTypes
addObject:availableMetadataObject];
}
}
[output setMetadataObjectTypes:supportedMetaDataTypes];
// Get called back everytime something is recognised
[output setMetadataObjectsDelegate:self
queue:dispatch_get_main_queue()];
You’ll then get called back on this method with array of AVMetadataMachineReadableCodeObject
- (void) captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection {
for(AVMetadataMachineReadableCodeObject *recognizedObject in metadataObjects) {
NSLog(@"%@", recognizedObject.stringValue);
}
}
I’ve created a quick demo project available here: https://github.com/cgreening/BarCodeExample.