Extracting the payload

The string of numbers underneath the bars are the payload for the barcode. To make it easier for humans, they’re represented as decimal values. The bars contain the same payload but in binary form.

A VNObservation doesn’t just return a single result, but an array of them. The reason for this is that sometimes Vision isn’t sure about what the correct response is. This is often the case in object detection applications. Luckily, the results are filtered in ascending order based on confidence score. Since we’re only interested in the best result, we can just grab the first result.

Let’s implement the processClassification(for:) method, which contains just a placeholder at the moment. We’ll also have to switch back to the main thread because we’re working with UI again.

Depending on the image, there may or may not be a barcode present. If there is a barcode, Vision might fail to recognize it. Therefore, we have to carefully unwrap the results, and we show an alert to the user if we don’t get any (good) results.

The product catalog

If everything has gone well, you’ll now be able to read barcodes and do whatever you want with them.

You probably don’t want to show only the payload to the user—they can read it themselves after all—but instead some kind of information about the product attached to the barcode. Because this tutorial is focused on Vision, I went ahead and already implemented a ProductCatalog struct, which serves as a “database” abstraction layer.

It loads the data from ProductCatalog.plist (which by the way you can add your own data to) and makes it searchable. The Product struct contains information about the product. If you’re interested, please take a look at how those structs are implemented.

The public API of the ProductCatalog is simple:

Let’s start by defining a product catalog at the top of the view controller:

We can now use it in our view controller as follows:

Note that we also print the payload to the console. This may be very convenient when adding new products to the catalog (you can just copy the code), and when debugging (so that you can make sure the code gets read correctly).

If you run the app on your phone, you’re able to scan barcodes. The app will check if the product catalog contains a product with this payload, and if it does, it will display an alert with details about the product.

Scanning a 33cl coke using the app

If you don’t happen to have a 33cl can of Coke at hand, here’s an image you can use instead. You can add any product of your own to the plist file—just add the payload as a key, and a name for the value.t

Conclusion

As you’ve seen, it’s super easy to work with barcodes in Swift thanks to Apple’s Vision framework. You can find the finished project in the here on GitHub.