Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something wrong with barcode (Code 128) font not scanning

Tags:

c#

.net

barcode

It was easy to generate a 3 of 9 barcode using Font()

Font f = new Font("Free 3 of 9", 80);
this.Font = f;

Label l = new Label();
l.Text = "*STACKOVERFLOW*";
l.Size = new System.Drawing.Size(800, 600);
this.Controls.Add(l);

this.Size = new Size(800, 600);

Its working. I see the barcode and Im able to scan it. Now I would like to use something else, like Code 128 For that I need to install the Font (done) and just change

Font f = new Font("Free 3 of 9", 80); to Font f = new Font("Code 128", 80);

After that I see a barcode on my window. The problem is that Im not able to scan that. And I think thats because I dont use the right start and stop tag for the barcode. As I understood there have to be always a start/stop char or whatever. For 3 of 9 ist the * for Code 128 im not sure. On wiki there is Start Code A so I tried

Font f = new Font("<Start Code A>test<Stop>", 80);, Font f = new Font("<Start Code A>test<Stop Code A>", 80); and so on... Im not able to scan the output. Because the scanner cannot find the start and stop char. Any ideas? Thank you

like image 432
sabisabi Avatar asked Jun 11 '12 09:06

sabisabi


People also ask

Why won't my barcode won't scan?

There are many reasons barcodes might not scan and most of them can be boiled down to one of three things—your equipment isn't suited to your barcodes, your scanner isn't being operated properly or your barcode labels aren't suited to your application or environment.

Why is my barcode font not working?

The most common cause for a barcode font failing to scan is a missing or incorrect start, stop or check digit. All barcode fonts require a special start character, a stop character and most require a calculated check digit. Make sure the required format is being used to print the barcode fonts.

How do I make my barcode font scannable?

Most 1D barcodes require a start and stop character in the barcode for it to be scannable. For Code 39, all you have to do is add the start symbol (*) to the front and back of the text.

How do I use a barcode 128 font?

A Code 128 barcode begins with a START character, followed by the DATA characters, followed by the CHECK character, and finally a STOP character. The whole barcode symbol must also include a quiet zone of white space both before the START character and after the STOP character.


1 Answers

Code 128 can be represented with a font totally fine. It's trickier than 3 of 9 though because you have to include a checksum character at the end which needs to be dynamically computed based on the content of the barcode. There are also 3 different versions which each have a different start char.

In other words the barcode needs to be laid out like:

[start char][barcode][checksum][stop char]

The benefit of code128 is that it is much more concise than 3 of 9.

This page helped me work out the algorithm to compute my checksums.

A very general overview of the algorithm is:

  1. Each character of the barcode gets a specific value assigned to it depending on what the character is and where it is located in the barcode.

  2. All of the values in 1) above are added together.

  3. Get the modulus 103 value of the total in 2) above.

  4. In most cases, the checksum char will be the ASCII code for: (modulus value plus 32) as determined in 3) above.

There were some nuances, I ended up needing to create this algorithm in javascript of all things (no questions). For my own future reference and to show some of the nuances this is what it looked like:

/*
 * This is the variable part of my barcode, I append this to a 
 * static prefix later, but I need to perform logic to compute the 
 * checksum for this variable. There is logic earlier that enforces 
 * this variable as a 9 character string containing only digits.   
 */ 
var formIncrement = // a 9 char "digit" string variable

/*
 * Dynamically compute the total checksum value (before modulus) 
 * for the variable part of my barcode, I will need to get a modulus 
 * from this total when I am done. If you need a variable number of 
 * characters in your barcodes or if they are not all digits 
 * obviously something different would have to be done here.  
 */ 
var incrementCS = ((parseInt(formIncrement.charAt(0)) + 16) * 7) +
                  ((parseInt(formIncrement.charAt(1)) + 16) * 8) +
                  ((parseInt(formIncrement.charAt(2)) + 16) * 9) +
                  ((parseInt(formIncrement.charAt(3)) + 16) * 10) +
                  ((parseInt(formIncrement.charAt(4)) + 16) * 11) +
                  ((parseInt(formIncrement.charAt(5)) + 16) * 12) +
                  ((parseInt(formIncrement.charAt(6)) + 16) * 13) +
                  ((parseInt(formIncrement.charAt(7)) + 16) * 14) + 
                  ((parseInt(formIncrement.charAt(8)) + 16) * 15);

/*
 * 452 is the total checksum for my barcodes static prefix (600001), 
 * so it doesn't need to be computed dynamically, I just add it to 
 * the variable checksum total determined above and then get the 
 * modulus of that sum:  
 */ 
var checksum = (452 + incrementCS) % 103


var barcode = "š600001" + formIncrement

/*
 * The 0 and the 95 - 102 cases had to be defined explicitly because 
 * their checksum figures do not line up with the javascript char 
 * codes for some reason (see the code 128 definition table in the 
 * linked page) otherwise we simply need to get the charCode of the 
 * checksum + 32. I also tack on the stop char here. 
 */ 
switch (checksum) {
    case 0 :
    barcode += "€œ";
    break;
    case 95 :
    barcode += "‘œ";
    break;
    case 96 :
    barcode += "’œ";
    break;
    case 97 :
    barcode += "“œ";
    break;
    case 98 :
    barcode += "”œ";
    break;
    case 99 :
    barcode += "•œ";
    break;
    case 100 :
    barcode += "–œ";
    break;
    case 101 :
    barcode += "—œ";
    break;
    case 102 :
    barcode += "˜œ";
    break;
    default :
    barcode += String.fromCharCode(checksum + 32) + "œ";
}

return barcode;

You may notice that my start and stop chars in my example (š, œ) don't seem to match up with the ones shown on the linked page. If I remember I think it was because I had some non-standard code128 font and these chars translated to the correct ones.

EDIT

I checked back in my documentation. It looks like I got the font from right here. With that font specifically and using the algorithm above, I just made a code128b barcode for test which came out to štestwœ, it scanned fine. Your checksum algorithm seems to be working fine because we both have w but it looks like your start and stop codes are off if you are getting: ÌtestwÎ.

I made a point to look for the same barcode font that I am using because I have a feeling that different brands of code128 fonts may implement different characters to represent the start and stop barcodes.

like image 112
egerardus Avatar answered Sep 28 '22 22:09

egerardus