Features

Barcode Processor is a collection of Java classes that will perform basic image recognition on a barcode image. It will take that image and convert it into a datastructure representing the bars, spaces, and their widths. You are then free to perform whatever additional translations you want on this datastructure (for example, recognizing it as a 3 of 9 code and turning it into text).

What it is not

There are a lot of caveats. Remember, this is development-level source code that is fairly new and had little Quality Assurance testing. Its practical uses are very limited at this point in time.

  • It is not an application with a user interface. You will need to have a Java development kit, perhaps with an IDE, in order to use it.
  • It is not able to read just any barcode scan. You can't just take a photo of a barcode and use that. The input barcode must be a computer-generated image with square corners and crisp edges.
  • It will not translate a barcode into plain text. It gets translated into bar and width codes, for instance: wide space, narrow space, wide bar, narrow bar.

Invocation

The barcode processor can be invoked using its public functions. An example of this follows:

BarcodeImageProcessor processor = new BarcodeImageProcessor();
processor.setImageFilename("/Users/enigma/Desktop/barcode.gif");
processor.setUseFuzzyPixelMatching(false);
processor.setBarColor(0, 0, 0);
processor.setSpaceColor(1, 0, 0);
processor.setDebug(false);
processor.guessWidths();
System.out.println("Wide areas are " + processor.getWideElementPixels() + " pixels");
System.out.println("Narrow areas are " + processor.getNarrowElementPixels() + " pixels");
BarcodeData data = processor.translate();
System.out.println(data);
            

Example

An example input file should look something like this. Note that it is a crisp, computer-generated image, and not a scan or photograph. The edges are sharp, the corners are square, and it has not been anti-aliased. Additionally, the colors are distict and the bars go to the edge of the image--that is, there are no left and right margins.

[Wide barcode image; click to view]



Figure 1: Input Barcode

The output is in the form of a datastructure. You can do whatever you want with this data (such as parse it as a particular barcode format), but the two included output options simply display width and bar data as plaintext.

1b 2s 1b 1s 2b 1s 2b 1s 1b 1s 1b 1s 2b 1s 1b 1s 2b 2s 1b 1s 1b 1s 2b 1s 2b 1s 1b 2s 1b 1s 1b 1s 2b 1s 1b 2s 2b 1s 1b 1s 1b 1s 1b 1s 2b 2s 1b 1s 2b 1s 2b 1s 1b 1s 2b 2s 1b 1s 1b 1s 2b 1s 1b 1s 1b 1s 2b 2s 1b 1s 2b 1s 1b 1s 1b 1s 2b 2s 1b 1s 2b 1s 1b 1s 2b 2s 1b 1s 1b 1s 1b 1s 2b 1s 1b 1s 2b 2s 1b 1s 2b 1s 1b 1s 2b 2s 1b 1s 1b 1s 2b 1s 1b 1s 1b 2s 1b 1s 2b 1s 2b 1s 1b 1s 1b 1s 2b 2s 1b 1s 2b 1s 2b 1s 1b 2s 1b 1s 1b 1s 2b 1s 1b 1s 1b 2s 2b 1s 1b 1s 2b 2s 1b 1s 1b 1s 2b 1s 1b 1s 2b 1s 2b 1s 1b 2s 1b 1s 1b 1s 2b 1s 1b 1s 2b 1s 1b 2s 1b 1s 2b 1s 2b 1s 1b 1s 1b 2s 1b 1s 1b 1s 2b 1s 2b 1s 1b 2s 1b 1s 1b 2s 1b 1s 2b 1s 2b 1s 1b

In this case, 1b = narrow bar, 1s = narrow space, 2b = wide bar, 2s = wide space



Figure 2: Output Format 1
.-..-.-.....-...--....-.-..-....-..--.......--..-.-...--
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 

....-.....--..-.....--..-...--......-...--..-...--....-.
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 

...-..-.-.....--..-.-..-....-....--...--....-...-.-..-..
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 

..-...-..-..-.-....-....-.-..-...-..-.-..
| | | | | | | | | | | | | | | | | | | | |
            


In this case, the second row contains "|" is a bar and " " is a space. The first row contains "." for narrow and "-" for wide.



Figure 3: Output Format 2

To-Do

  • Currently, there is a certain amount of fuzziness in recognizing colors--that is, anything within a few shades of the defined bar color and space color is considered part of that area. There is no similar fuzziness with widths. With fuzzy logic behind the bar and space widths, less structured images can be parsed: blurry/aliased edges, photographs, scans, slight rotations, etc.
  • Output plugins--code that will recognize the bar and space widths and translate that to plaintext.
  • A rudimentary user interface, so that you don't have to be a programmer to use the code
  • While the datastructure supports bars and spaces with widths of 1x to 4x, the image recognizer only supports recognizing two different sizes: narrow and wide. This means things like Code139 cannot be parsed from an image at present. The code needs to be adjusted to recognize up to four different bar widths.
  • There is no margin detection. This is extremely difficult to do unless you know exactly what barcode standard to expect--at which point, you ignore everything until you get something that looks like the start/stop character. Without knowing what barcode format is to be expected, it is virtually impossible to detect and ignore margins.