代码 128 可以用完全正常的字体来表示。不过,它比 9 中的 3 更棘手,因为您必须在末尾包含一个校验和字符,该字符需要根据条形码的内容动态计算。还有 3 个不同的版本,每个版本都有不同的起始字符。
换句话说,条形码需要如下布局:
[start char][barcode][checksum][stop char]
code128的好处是比3 of 9简洁很多。
这一页 http://en.wikipedia.org/wiki/Code_128帮助我制定了计算校验和的算法。
该算法的一般概述是:
条形码的每个字符都会分配一个特定的值
取决于角色是什么以及它在角色中的位置
条码。
将上述 1) 中的所有值相加。
获取上面 2) 中总计的模 103 值。
在大多数情况下,校验和字符将是以下内容的 ASCII 代码:(模值
加上上面 3) 中确定的 32)。
有一些细微差别,我最终需要用 javascript 创建这个算法(毫无疑问)。为了我自己将来的参考并展示一些细微差别,它看起来像这样:
/*
* 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;
您可能会注意到,示例中的开始字符和停止字符 (š、œ) 似乎与链接页面上显示的字符不匹配。如果我记得的话,我认为这是因为我有一些非标准的 code128 字体,这些字符被转换为正确的字符。
EDIT
我检查了我的文档。看起来我的字体是从就在这儿 http://freebarcodefonts.dobsonsw.com/code128font-download.html。专门使用该字体并使用上面的算法,我刚刚制作了一个 code128b 条形码test
结果是štestwœ
,扫描得很好。你的校验和算法似乎工作正常,因为我们都有w
但如果您收到以下信息,则您的开始和停止代码似乎已关闭:ÌtestwÎ
.
我特意寻找与我正在使用的相同的条形码字体,因为我有一种感觉,不同品牌的 code128 字体可能会实现不同的字符来表示开始和结束条形码。