At My Fingertips

Barcode

Look carefully at this image: you've probably seen similar ones many times.

UPC-A Barcode

It is legitimate to wonder whether there is some relationship between the numbers and the bars. In reality, they are simply two different ways to represent the same data.

There are numerous global standards to uniquely identify products on the market. One of these standards is GTIN.

There are several variants, among which GTIN-12 is among the most popular ones. It uniquely identifies each product with a 12-digit number (which in turn has a certain structure, with some digits identifying the manufacturer and others the specific product, but we won't go into details here).

Barcodes in UPC format

The barcode shown above actually contains a GTIN-12 number twice: in the lower part the digits are written so that they are easily readable by a human ("human-readable"), while in the upper part they are coded with an alternating sequence of bars and spaces so as to be easily readable by a machine ("machine-readable").

The bar coding format shown above uses the UPC standard (Universal Product Code), in its UPC-A variant, which is capable of representing a 12-digit number.

This format simply defines a structure composed of 95 modules. A module is each of the vertical stripes, which are separated by a red line in the following figure, filled with black (what we colloquially call the "bar") or white (the "space").

Barcode divided into 95 modules

We can bundle the modules in the following groups:

Grouped modules

  • at the left and right extremes, two groups called "quiet" each composed of 9 space modules
  • just further inside, respectively a "start" group on the left and an "end" group on the right, each composed of the sequence bar-space-bar
  • a group in the center made up of 5 modules, space-bar-space-bar-space
  • the remaining 12 groups (6 on the left and 6 on the right with respect to the central group) are each composed of 7 modules and encode in sequence the digits of the number we want to represent

Before we get into coding the individual digits, try creating the "start" group by simply placing three rectangles beside each other:

Loading...

Encoding of the digits

For simplicity, let's start by encoding the first six digits (to be clear, those on the left, between the initial group and the central one).

Each digit is represented using 7 modules. The digit 0, for example, is represented with the sequence space-space-space-bar-bar-space-bar. For convenience, we can identify the space with the boolean value False and the bar with the value True. Following the standard, we can create a list where the element in position i indicates the encoding of the digit i, represented with a list of 7 boolean values.

Implement a function that creates the graphic corresponding to the given digit, appropriately placing 7 modules one side by side (remember that you can call the module function defined above).

Don't worry about handling the case where left is False for now: we will deal with the right side shortly.

Loading...

The last six digits, to the right of the central part, are encoded by reversing the roles of bar and space: the digit 0, for example, is represented with the sequence bar-bar-bar-space-space-bar-space (exactly the opposite of the encoding for the left side).

Extend the implementation of the function you just defined to also handle the case where left is False.

Let's put the pieces together

We can now create the entire barcode, placing the groups of modules one next to the other as described in the bulleted list above.

You can verify that your barcode "really works" by scanning it with your smartphone using an application that reads barcodes (you can find free ones on the various stores).

Loading...

This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Barcode

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version b744b47 (Tue, 08 Oct 2024 16:30:14 GMT)