Barcode Recognition

This program reads EAN13 barcodes from images (e.g. photos of barcodes on books).
I made it when cataloguing my library at home.

How do barcodes work?

Check out the Wikipedia article for information about the encoding.

How does this program work?

This program takes a fairly brute-force approach to finding barcodes: It looks at each row of the image separately, and so it doesn't have to consider colour, it converts each row to black and white (i.e. one bit per pixel). It does this by picking a threshold T and then considering each pixel to be black if the sum of its brightness components (red + green + blue) is less than T. Several different threshold values are tried for each row to allow for barcodes with different contrast levels.

Let's examine the process on a sample image. Here is a photo of a barcode. Photograph of a barcode

We'll look at one row of the image. I've picked a good one from the middle and stretched it vertically to make it easier to see. One row of the barcode

We'll convert it to black and white. (The program will try lots of thresholds for doing this - I just chose a good one by eye here). One row of the barcode

A block is a (maximal) contiguous group of pixels of a single colour. We look through the row, working out where each block begins and finishes, and use the block widths to work out what the original barcode was. See the source code for details.

Limitations

  • Barcodes must be presented horizontally in the image - if this is an issue, try rotating the images before running the program.
  • A high-resolution image of the barcode is required (the 600px by 400px image above is around the minimum).
  • Source code

    The source code is available under the GPL3 license. It requires g++ and libgd to be installed.

    Download barcode reader source code.