Code 128


code128 barwidth string [> output-file]


Code128 generates barcode images in JPG (or GIF, if you don’t have a copy of libjpeg on your system) using’s GD graphics library. It takes two arguments – the width of the smallest bar in the output (typically 1; the title image on this page uses 2) in pixels, and the string you wish to generate a barcode of. It encodes that string using the Code 128 barcode format, and dumps the resulting image to standard out.

There are approximately a million versions of GD out there, but if your version supports PNG images, it will produce a png, otherwise if it supports JPEGs, it will produce a jpeg, otherwise otherwise it will produce a GIF.


  1. Code 128 format barcodes can get to be fairly long. A code 128 barcode is 11 barwidth long per character, plus 55 barwidth for the message envelope.

Source Code

Code128 uses my configuration system to configure itself for building on various platforms. To build the code, untar the archive, run, then run a make.

version 0.5
A fairly major rework on the internals of this code; first, I implemented CODEC (compresses number strings by up to ~50%), and secondly I took out all of the places where I was table scanning the code128[] array and replaced it with lookups into a 3 automatically generated maps (generated by the new program mkcodes, which is used to make the file table.c which contains codeAmap[], codeBmap[], and controlmap[] (codeC doesn’t use lookups; the 100 CODEC points are 0..99 so no lookup is needed.))
version 0.4

It turns out that when I originally wrote this code I’d completely misread the checksum generation algorithm, so instead of code128 barcodes I was generating orc128 barcodes. So, as part of a small software death march to updating the configuration to work with a more uptodate version of libgd (which was a pain because libgd has gone not only to GNU autoconfigure, but has – at least on Unix – abandoned make for cmake, so I couldn’t even salvage a GNU but had to create one from scratch instead) I reworked the checksum computation so that it actually works.

Other features in 0.4 include changing the copyright to a 3-clause BSD-style one and changing the output format of the barcode image to (depending on libgd support) PNG, then Jpeg, and finally (the finally free-to-use) GIF format.

version 0.2
The first release. It’s version 0.2 because version 0.1 didn’t include the copyright notice.