Creating Data Matrix Barcodes

Generating 99 million barcodes for a clinic letters system used by a large hospital.

In an ideal world hospitals would be paperless. While many hospitals have made great advances in digitising their medical records, the fact is there will always be some elements of paper for years to come. Inter departmental communications are slowly being computerised. However, letters to patients will remain in paper form for the foreseeable future. To help with the management of this paper trail barcodes can be employed to help record key stages within the documents workflow.

For example, we use data matrix barcodes on all our clinical correspondence, the barcode represents the unique letter number within the hospital system. When a letter has been dictated and checked by the consultant the barcode is scanned to digitally sign the letter. If the letter is altered and returned to the secretary for editing, the secretary can scan the barcode to instantly load the letter, saving her having to type in the patients ID number and locate the letter.

As paper copies of the letter are sent to the patient, the barcode is again scanned as the letter is put into the envelope. This triggers an event in the system giving us an audit point for when the letter was posted.

These are just a couple of examples of how barcodes within the hospital workflow can save a great deal of time.

The process below outlines how I generate and store the barcode images to support a clinic letters system with over a million letters, with room to expand to 99 million.

Choosing a tool to generate the data matrix barcodes

libdmtx is open source software for reading and writing data matrix barcodes. The utility will run on Linux, Unix, OS X, Windows, and mobile devices. The libdmtx library can generate square or rectangular barcodes depending on your preference.

square or rectangle datamatrix barcodes

As we use Ubuntu Linux servers, the software is easily installed from the package manager. However, it should be easy enough to install on all the supported platforms.

sudo apt-get install libdmtx0a
sudo apt-get install libdmtx-utils

Creating a barcode image file using the dmtxwrite utility is very straight forward. To create an image representing the unique letter number 10274 you would echo the number and pipe it into the dmtxwrite command as shown below. The barcode can be output in pretty much any image format such as JPG, GIF or TIFF, but here we are using the default PNG format.

echo -n 10274 | dmtxwrite -m 1 -s s -o square.png

The -m 1 option instructs the utility to create a 1 pixel margin within the output file square.png. The option -s s invokes a square barcode. If you prefer to have rectangular barcodes then replace the option with -s r.

Creating a folder structure to hold 99 million barcode image files

Creating barcodes using dmtxwrite is very fast and you could just create the images on the fly as a new letter is generated in the system. However, as the letter numbers follow a sequential numbering scheme it makes more sense to pre-generate the images during the evening, when the system is idle so that they are ready for use during the day.

You could generate batches of a 100,000 images at a time. Alternatively you can be smart and look at the rate of letter creation over the previous 30 days. Then using this rate value adding a growth factor of maybe 20% and generating enough barcodes to cover your systems growth rate. This is how Open ALMA will operate, a nightly cron job will check and extend the barcodes as necessary.

Either way you wouldn’t want to keep thousands of these barcode images within a single folder. It’s much better to split them up over many folders. I wanted to support the possibility of 99 million barcodes yet only keep 99 barcodes within any one folder.

Below is a PHP script which can be used to create the barcodes in batches ahead of time.

To create the folder structure the number you want to represent is padded out to 8 digits using zeros for the padding value. Using our example of letter number 10274 - the number will be padded out to 00010274. The first six digits are used in three sets of two numbers to create the folder name. So in this case the folder name would be 00/01/02/

The script then checks to see if the folder already exists, if it doesn’t it’s created, and then places the barcodes within that folder. This folder would hold all the images for barcodes 10200.png through to 10299.png

<?php

$firstBarcode = 1;
$lastBarcode  = 100000;

$pathToBarcodeFolder  = '/path/to/image/store/';

for ($number = $firstBarcode; $number <= $lastBarcode; $number++) {

    $padded  = str_pad($number, 8, 0, STR_PAD_LEFT);
    $folders = str_split($padded, 2);

    $fileName = $pathToBarcodeFolder;

    for ($i = 0; $i <= 2; $i++) {
        $fileName .= $folders[$i] . '/';
    }

    if (!is_dir($fileName)) {
        @mkdir($fileName, 0777, true);
    }

    $fileName .= $number . '.png';

    $dmtxwrite = 'echo -n ' . $number . ' |
        dmtxwrite -m 1 --symbol-size=s16x16 -o ' .
        $fileName;

    echo $dmtxwrite, "\n";
    exec($dmtxwrite);
}

?>

Example running the script

creating barcodes using PHP & dmtxwrite