More Barcodes

I’ve been working more with my barcode application and am finally close to a working solution.

Adding start and stop characters is more or less dependent on which charset you want to use, unless you’re going to use special characters or some other special functionality, you’ll be using Code128 B – which supports most normal alphanumeric characters and punctuation characters in the ASCII table. The rest is just basic String concatenation, which you can do in LibreOffice Calc or through programming.

The first stretch was developing the checksum method in Java, which was pretty easy thanks to the information provided by wikipedia here.

What I did was simply to make an array with the characters listed for Code 128 B, disregarding special characters since I won’t be using them anyway. The array looks a little something like this…

private String code128B_table[] = {
" ","!","\"","#","$"," %","&","'","(",")","*","+",",","-",".","/",
"0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?",
"@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL","FNC3",
"FNC2","ShiftA","CodeC","FNC4","CodeA","FNC1","StartCodeA","StartCodeB","StartCodeC"
};
private String code128_table[] = code128B_table;

With the Array in place, all I needed to do to calculate the checksum was write this method:

/**
* Calculates the checksum
* @param input String input for calculating checksum
* @return the checksum in integer form
*/
public int checksumInt(String input) {
int checksum = STARTCODE;
for(int i = 0; i < input.length(); i++) {
int value = charNumber(input.charAt(i));
if(value < 0)
return value;
int position = i+1;
checksum += (value * position);
}
return checksum % 103;
}

/**
* Helper function for checksumInt
* Finds the number of the particular character using the Code128B table
* @param c
* @return
*/
public int charNumber(char c) {
for(int i = 0; i < code128_table.length; i++) {
if(code128_table[i].charAt(0) == c)
return i;
}
System.out.println("Could not find " + c);
return -1;
}

But wait, there’s more. The checksum itself isn’t enough, it needs to be in the form of a character, but since we already have the table for Code128B in array form, we just convert the integer to character for printing.

/**
* Converts calculated checksum into character
* @param i calculated checksum
* @return character for printing checksum
*/
public char checksumChar(int i) {
if(i < 95)
return (char) (i + 32);
else
return (char) (i+ 100);
}

Since the computers come with Norwegian keyboards and the barcode scanner will read barcodes as if receiving keyboard input, I had to do a few cheap hacks to remap the characters according to Norwegian keyboards, this isn’t a problem with alphanumeric data, but characters like { [ ] and } will read on a computer set with  a Norwegian keyboard layout as receiving the letters Æ Ø and Å or some other character because of this, I made a function with way too many if/else tests rather than using a HashMap or other key-value pair since it just seemed simpler to write at the time.

The class file is available on github here.

Beginning Barcodes

About a year ago, I made a bit of an impulse buy on eBay and purchased a barcode scanner for a little less than a dollar (little did I know that shipping and handling would cost me 9 dollars more, but 10 bucks isn’t really anything to worry about).

BarcodeScanner

Why you ask?

Because for some reason I thought it might be a good idea to have a barcode scanner available so I could learn a bit about programming applications using barcodes.

What have I learned so far?

Well there’s a lot of talk on how you need permission to use certain barcodes. Especially EAN and UPC seem to be regulated if you want to distribute your product, but EAN is only the tip of the iceberg.

Some of the simplest barcodes to work with today can be installed as fonts and these include Code 39, Code 93 and Code 128.

But you can’t just download a font and think it’ll work like that – barcodes usually need some way of knowing where to start and stop reading, so the name “Studio Black” would have to be written “ÌStudioÂBlack{Δ to work in Code 128.

Luckily, several websites, like this one, can help translate for you.

So how does this work?

Barcodes are actually an incredibly simple and efficient input method and isn’t really all that different from a computer keyboard. Think of it as a mix between a keyboard and a scanner. When it reads any barcode it supports, your computer will pretty much only act as if it received keyboard input in the form of a string or number.

Some barcodes support alphanumeric data (like Code 39 and Code 128), which means it can contain both text and numbers, while others are simply numeric (like EAN and UPC).

Because of this, programming an application that reads barcodes require next to nothing extra and you can probably just use the same old text fields and forms as always. The USB barcode scanner I have is pretty simple to use and drivers were as easy and automatic as when connecting a keyboard or mouse – just plug and play.

In the long run, the biggest hassle is probably getting familiar with the legal issues of it all.

You can find the fonts for the different barcode systems:

Here: Code 39

Here: Code 93 and

Here: Code 128

As of yet I’ve had the best luck using Code 128, with the type of barcode scanner you can find for about 10 bucks off eBay. My model is called X-9100 and works like a charm.

By the way, make sure to never use bold or italic with barcode fonts, they mess up everything.