The world’s leading publication for data science, AI, and ML professionals.

Exploring the Barcode Universe

A Computational Thinking Story With the Wolfram Language

Photo by Johannes Plenio on Unsplash
Photo by Johannes Plenio on Unsplash

Incomprehensible by humans, but child’s play for computers and phones: Barcodes are everywhere. Every product, every package, and every store shelf has them in copious amounts. Black and white patterns, often lines and sometimes dots, provide our silicon friends with a little number that encapsulates what the object is all about.

Online services like barcodelookup.com provide databases with millions of items to turns those little numbers into a wealth of information, like product name, product category, and vendor-specific information.

In the Wolfram Language, you can read barcodes and also create barcode images. It does not come with a built-in service to interpret barcodes but in this story, I will show you how to connect to the API from the BarcodeLookup service.

Let’s start with generating barcodes ourselves. The function to use here is called BarcodeImage. It can generate the most common types of barcodes. For example, this generates a UPC barcode image:

BarcodeImage["123456789999", "UPC"]
(image by author)
(image by author)

Here is an example of a QR code. Most smartphones will offer to open the web page when you point the camera at it:

BarcodeImage["https://www.wolfram.com", "QR"]
(image by author)
(image by author)

Automatically recognizing a barcode is done with the BarcodeRecognize function. It works even when the barcode is only part of the image, although it picks up an occasional "stray" barcode. In this case, the result is correct:

(image by author)
(image by author)

Next, to interpret the code "04150800414" I wrote a simple Wolfram Language function that binds to the API from the Barcode Lookup service.

BarcodeLookup

It can be accessed by its name:

BarcodeLookup = ResourceFunction[
 "user:arnoudb/DeployedResources/Function/BarcodeLookup"
]

Now we can look up the product information. The "key" is a small alphanumerical string required and provided by the Barcode Lookup service API.

product = BarcodeLookup["041508300414",key]

And you can drill down programmatically to get, for example, the product name:

In[]:= product["products", 1, "product_name"]
Out[]= "San Pellegrino Pompelmo Grapefruit Sparkling Fruit Beverage, 11.15 Fl. Oz., 6 Count"

The application possibilities are almost endless here. With these functions, it becomes easy to build applications for tracking inventory, generating reports, and automate restocking procedures.


Related Articles