A barcode is an optical, machine-readable, representation of data; the data usually describes something about the object that carries the barcode – wikipedia. While first barcode was patented on 1952, it took quite a while until these codes were used in first grocery store on 1974. And now they are being used everywhere.
While using them looks quite obvious for countless people on a daily basis, science behind making and reading is much more complicated. I will skip this hi-tech theory and jump directly to examples how to generate and decode them using some clever Python scripts and a Kurokesu C1 USB camera connected to computer.
I will be using viivakoodi python library to generate barcodes (others like OpenCV and numpy are required, so install them before use). While there are few similar python libraries, viivakoodi supports generating CODE128 barcodes.
pip install viivakoodi
Native viivakoodi output format is SVG but I needed PNG. Quick format conversion and output file is a nice raster file.
This library also can output barcode in various other formats: code128, code39, ean, ean13, ean8, gs1, gtin, isbn, isbn10, isbn13, issn, jan, pzn, upc, upca. Some barcodes can’t encode letters, some have other creation rules. Usually library this let’s know what is happening and gives you a hint. Also worth to be noted that CODE-128 has checksum fields and this library calculates it just fine.
QRcode is more advanced barcode version. Information is encoded in 2D format.
pip install qrcode
These barcodes being two dimensional allow more information to be encoded. Also has flexibility to control how much redundant information to be used.
Luckily there is single library to recognize barcodes and qrcodes. It’s called zbar. Let’s prepare some barbodes for automated recognition and run zbar script. While Zbar was last modified on 2011, it is still the best open-sources recognition software. It might require some tricks installing it but it is definitely worth trying. Zbar supports following barcode formats: EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 and QR Code
Results are more than satisfying. I got ~10fps recognition speed on modern computer. Video source was 1080×1080. Some barcodes were not recognized – they were to small or unsupported format.