Copyright Brian Starkey 2012-2014
Valid HTML 4.01 Transitional

Valid CSS!
iconBarcode Generatormax, min, close
Scan Me!Scan Me!
Description(top)
This simple little utility uses the Cairo 2D graphics library to generate 8-digit or 13-digit UPC codes.
Usage: barcode [code]
code is required (8 or 13 digits)
Generates a PNG file with the filename '[code].png'

Implementation(top)
Character setCharacter set
Actually generating barcodes is pretty simple. The Wikipedia article on EAN codes gives you all the information you need. All you need to do is take the input numbers and generate a binary code from them, the rest is figuring out how to display it.
The image size is decided based on the length of the barcode number, and then the guard bands either side and in the middle drawn. The 'i' loops in main then go through the encoded barcode number, and decide whether to draw a vertical black line or not depending on each bit.
Most of the complexity in my code comes from printing the numbers along the bottom of the barcode. objcopy is used to embed the charset.png image into the binary file, and then the functions readPNGFromStream(), streamReader() and buildCharset() create an array of cairo surfaces containing the characters. Once this is done, the approprite numbers are painted onto the barcode in the correct places
Finally, the whole image is written to file.