BarcodeLocalizer

OpenCV and Java based barcode localizer

Download .zip Download .tar.gz View on GitHub

User Guide Localizer Algorithm

This library localizes 1-D and 2-D barcodes from arbitrary images and is able to successfully handle noisy backgrounds as well as barcodes that are skewed or rotated out of plane. The resulting cropped and localized images can then be decoded by other software barcode decoders that require an input image as close as possible to a pure barcode.

Most barcode decoders cannot easily handle noisy images or images where the barcode is rotated, so using this package as a localizer prior to decoding significantly improves decode success rates. Using this library in combination with ZXing, a popular open-source decoding library, almost doubled ZXing's success rate at decoding both 1-D and 2-D codes in single images.

Example Images with the Localized Barcode


General Observations

The goal for this project is slightly different from the goal of most software decoders used in mobile phone apps etc. Firstly, this project only aims to handle the localization step, not the decoding. Secondly, most software decoders assume that the barcode will be roughly centered and horizontal and explicitly trade accuracy for speed with the idea that their target user is on a mobile phone and the camera is feeding a continuous stream of images.

The concept there is that even if the decoder misses on one still image, the next image in the feed will have a little bit of jitter from the user's hand motion and may now present a readable image. The user is also encouraged to orient the image so that the barcode is roughly centered and horizontal. My localizer aims to get closer to "one shot, one kill" so that if there is a barcode anywhere in a single image, it can be located and passed to the decoder in presentable format for reading.

Test results


The library was tested on a sample set of 60 images, 30 of which were linear barcodes(mostly UPC and EAN) and 30 of which were 2-D barcodes(QR, PDF417, DataMatrix and Aztec). The popular open-source barcode decoding library ZXing was used as a benchmark. Roughly a third of the images were used as a training set to develop the algorithm. The results below are broken down as follows

  • Images that were successfully decoded
  • Images that were localized successfully(on manual visual inspection)*
  • Images that could not be localized or decoded

Results for BarcodeLocalizer
TypeDecodedLocalized*Failed
Linear16 8 6
Matrix15 7 8

Results for ZXing
TypeDecodedLocalizedFailed
Linear9 N/A 21
Matrix8 N/A 22


* Some barcodes were localized accurately but could not be decoded by ZXing - usually because the source image was too blurry or had shadows/light gradients that prevented ZXing from successfully decoding the barcode. These could potentially be decoded by a more robust decoder.

User Guide

The BarcodeLocalizer is designed to be used as a library imported into your Java project. A sample tester program called SimpleBarcodeTester.java is included with the source code to demonstrate its use. You will also need the OpenCV for Java library in your library path and you might want ZXing to use as a decoder