Barcode API Overview

The Barcode API detects barcodes in real-time, on device, in any orientation. It can also detect multiple barcodes concurrently.

It reads the following barcode formats:

It automatically parses QR Codes, Data Matrix, PDF-417, and Aztec values, for the following supported formats:

  • URL
  • Contact information (VCARD, etc.)
  • Calendar event
  • Email
  • Phone
  • SMS
  • ISBN
  • WiFi
  • Geo-location (latitude and longitude)
  • AAMVA driver license/ID

Detect Barcode Features in Photos

This tutorial will discuss:

  1. Creating a barcode detector.
  2. Detecting barcodes in a static image.

Creating the barcode detector

First, import the GoogleMobileVision framework to use the detector API.

@import GoogleMobileVision;

Typically, the barcode detector is created in the viewDidLoad method. Instantiate a barcode detector with the desired options.

  NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(
        GMVDetectorBarcodeFormatQRCode |
        GMVDetectorBarcodeFormatEAN13 |
        GMVDetectorBarcodeFormatUPCA)
  };
  self.barcodeDetector = [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];

GMVDetectorBarcodeFormats specifies barcode detection formats allowed. It takes a bitmask of GMVDetectorBarcodeFormat. If none are specified, then the detector will detects all formats.

Detecting barcodes

Use the barcode detector to find barcodes in an UIImage.

UIImage *image = [UIImage imageNamed:@"barcodes.jpg"];
NSArray<GMVBarcodeFeature *> *barcodes = [self.barcodeDetector featuresInImage:self.image
                                                                       options:nil];

The orientation is not required for detection, but it may be useful if you wish to render a bounding box in the UI coordinate system. Therefore, the options may be left as nil.

Getting barcode Results

The detector returns a collection of GMVBarcodeFeature. We can iterate over the collection and each barcode's bounding box, type, value type, and detected value.

for (GMVBarcodeFeature *barcode in features) {
    NSLog(@"%@", NSStringFromRect(barcode.bounds));
    switch(valueFormat) {
      case GMVDetectorBarcodeValueFormatContactInfo:
        [self displayContactInfo:barcode.contactInfo];
        break;
      case GMVDetectorBarcodeValueFormatEmail:
        [self displayEmail:barcode.email];
        break;
      case GMVDetectorBarcodeValueFormatDriverLicense:
        [self displayDriverLicense:barcode.driverLicense];
        break;
      default:
        [self displayBarcodeValue:barcode.displayValue];
        break;
    }
}

The detected barcode positions are relative to the image in the view coordinate system. The barcode may contain parsed structure data (URL, contact information, calendar event, email, phone number, SMS, ISBN, WiFi, geo location, and AAMVA driver license/ID). Inspect the valueFormat property to check for the returned type.