Code 128 条形码原理和在线生成
本文发布于 6 年前,部分内容可能已经失去参考价值。
Code 128 条形码可以表示全部 128 个 ASCII 码字符(数字、大小写字母、符号和控制符)。
Code 128 有三个子集:Code 128A、Code 128B 和 Code 128C。
Code 128A 用来编码数字、大写字母、控制符、部分符号;
Code 128B 用来编码数字、大写字母、小写字母、符号;
Code 128C 用来编码 2 位数字 [00,99]。
码条大小有 4 种,按照宽度,我们用 1、2、3、4 来表示。
一个完整的 Code 128 条码是由:起始符、字符串、检验位、终止符组成的。
每个字符由 3 个条、3 个空、11 个单元构成,字符串可变长。
黑色线条用 B(Bar)表示,白色空位用 S(Space)表示,那么一个字符是由 BSBSBS 组成的。
起始符有三种,“CODE-A”、“CODE-B”和“CODE-C”。起始符的类型决定了后面字符的构成。
当采用码来设置字符时(CODE-A、CODE-B 和 CODE-C),起始符为 CODE-A 的条码在条码的处理中可以变为采用 CODE-B 或 CODE-C 栏的字符。
当采用“SHIFT”时,只有紧靠 SHIFT 的一个字符可以在下一栏被变更(A 到 B,B 到 C,C 到 A)。(和电脑键盘上的 SHIFT 键操作类似)
校验字符通过 MOD103 算法,下面举例说明:
例 1:xoyozo.net
信息:StartB x o y o z o . n e t
值:104 88 79 89 79 90 79 14 78 69 84
位置:- 1 2 3 4 5 6 7 8 9 10
计算:104 + 88 * 1+ 79 * 2 + 89 * 3 + 79 * 4 +90 * 5 + 79 * 6 + 14 * 7 + 78 * 8 + 69 * 9 + 84 * 10 = 4040
取模:4040 % 103 = 23
完整的条形码信息: (Start B)xoyozo.net(7)(STOP)
例 2:C08244
信息:StartB C 0 CodeC 82 44
值:104 35 16 99 82 44
位置:- 1 2 3 4 5 6
计算:104 + 35 * 1+ 16 * 2 + 99 * 3 + 82 * 4 +44 * 5 = 1016
取模:1016 % 103 = 89
完整的条形码信息: (Start B)C0(CodeC)8244(y)(STOP)
在线示例:https://xoyozo.net/Tools/Code128
附 Code 128 编码表:
Value | Code A | Code B | Code C | Pattern | ASCII Code |
BSBSBS | |||||
0 | SP | SP | 00 | 212222 | SP (ASCII 32) |
1 | ! | ! | 01 | 222122 | ! (ASCII 33) |
2 | " | " | 02 | 222221 | " (ASCII 34) |
3 | # | # | 03 | 121223 | # (ASCII 35) |
4 | $ | $ | 04 | 121322 | $ (ASCII 36) |
5 | % | % | 05 | 131222 | % (ASCII 37) |
6 | & | & | 06 | 122213 | & (ASCII 38) |
7 | ' | ' | 07 | 122312 | ' (ASCII 39) |
8 | ( | ( | 08 | 132212 | ( (ASCII 40) |
9 | ) | ) | 09 | 221213 | ) (ASCII 41) |
10 | * | * | 10 | 221312 | * (ASCII 42) |
11 | + | + | 11 | 231212 | + (ASCII 43) |
12 | , | , | 12 | 112232 | , (ASCII 44) |
13 | - | - | 13 | 122132 | - (ASCII 45) |
14 | . | . | 14 | 122231 | . (ASCII 46) |
15 | / | / | 15 | 113222 | / (ASCII 47) |
16 | 0 | 0 | 16 | 123122 | 0 (ASCII 48) |
17 | 1 | 1 | 17 | 123221 | 1(ASCII 49) |
18 | 2 | 2 | 18 | 223211 | 2 (ASCII 50) |
19 | 3 | 3 | 19 | 221132 | 3 (ASCII 51) |
20 | 4 | 4 | 20 | 221231 | 4 (ASCII 52) |
21 | 5 | 5 | 21 | 213212 | 5 (ASCII 53) |
22 | 6 | 6 | 22 | 223112 | 6 (ASCII 54) |
23 | 7 | 7 | 23 | 312131 | 7 (ASCII 55) |
24 | 8 | 8 | 24 | 311222 | 8 (ASCII 56) |
25 | 9 | 9 | 25 | 321122 | 9 (ASCII 57) |
26 | : | : | 26 | 321221 | : (ASCII 58) |
27 | ; | ; | 27 | 312212 | ; (ASCII 59) |
28 | < | < | 28 | 322112 | < (ASCII 60) |
29 | = | = | 29 | 322211 | = (ASCII 61) |
30 | > | > | 30 | 212123 | > (ASCII 62) |
31 | ? | ? | 31 | 212321 | ? (ASCII 63) |
32 | @ | @ | 32 | 232121 | @ (ASCII 64) |
33 | A | A | 33 | 111323 | A (ASCII 65) |
34 | B | B | 34 | 131123 | B (ASCII 66) |
35 | C | C | 35 | 131321 | C (ASCII 67) |
36 | D | D | 36 | 112313 | D (ASCII 68) |
37 | E | E | 37 | 132113 | E (ASCII 69) |
38 | F | F | 38 | 132311 | F (ASCII 70) |
39 | G | G | 39 | 211313 | G (ASCII 71) |
40 | H | H | 40 | 231113 | H (ASCII 72) |
41 | I | I | 41 | 231311 | I (ASCII 73) |
42 | J | J | 42 | 112133 | J (ASCII 74) |
43 | K | K | 43 | 112331 | K (ASCII 75) |
44 | L | L | 44 | 132131 | L (ASCII 76) |
45 | M | M | 45 | 113123 | M (ASCII 77) |
46 | N | N | 46 | 113321 | N (ASCII 78) |
47 | O | O | 47 | 133121 | O (ASCII 79) |
48 | P | P | 48 | 313121 | P (ASCII 80) |
49 | Q | Q | 49 | 211331 | Q (ASCII 81) |
50 | R | R | 50 | 231131 | R (ASCII 82) |
51 | S | S | 51 | 213113 | S (ASCII 83) |
52 | T | T | 52 | 213311 | T (ASCII 84) |
53 | U | U | 53 | 213131 | U (ASCII 85) |
54 | V | V | 54 | 311123 | V (ASCII 86) |
55 | W | W | 55 | 311321 | W (ASCII 87) |
56 | X | X | 56 | 331121 | X (ASCII 88) |
57 | Y | Y | 57 | 312113 | Y (ASCII 89) |
58 | Z | Z | 58 | 312311 | Z (ASCII 90) |
59 | [ | [ | 59 | 332111 | [ (ASCII 91) |
60 | \ | \ | 60 | 314111 | \ (ASCII 92) |
61 | ] | ] | 61 | 221411 | ] (ASCII 93) |
62 | ^ | ^ | 62 | 431111 | ^ (ASCII 94) |
63 | _ | _ | 63 | 111224 | _ (ASCII 95) |
64 | NUL | ` | 64 | 111422 | ` (ASCII 96) |
65 | SOH | a | 65 | 121124 | a (ASCII 97) |
66 | STX | b | 66 | 121421 | b (ASCII 98) |
67 | ETX | c | 67 | 141122 | c (ASCII 99) |
68 | EOT | d | 68 | 141221 | d (ASCII 100) |
69 | ENQ | e | 69 | 112214 | e (ASCII 101) |
70 | ACK | f | 70 | 112412 | f (ASCII 102) |
71 | BEL | g | 71 | 122114 | g (ASCII 103) |
72 | BS | h | 72 | 122411 | h (ASCII 104) |
73 | HT | i | 73 | 142112 | i (ASCII 105) |
74 | LF | j | 74 | 142211 | j (ASCII 106) |
75 | VT | k | 75 | 241211 | k (ASCII 107) |
76 | FF | l | 76 | 221114 | l (ASCII 108) |
77 | CR | m | 77 | 413111 | m (ASCII 109) |
78 | SO | n | 78 | 241112 | n (ASCII 110) |
79 | SI | o | 79 | 134111 | o (ASCII 111) |
80 | DLE | p | 80 | 111242 | p (ASCII 112) |
81 | DC1 | q | 81 | 121142 | q (ASCII 113) |
82 | DC2 | r | 82 | 121241 | r (ASCII 114) |
83 | DC3 | s | 83 | 114212 | s (ASCII 115) |
84 | DC4 | t | 84 | 124112 | t (ASCII 116) |
85 | NAK | u | 85 | 124211 | u (ASCII 117) |
86 | SYN | v | 86 | 411212 | v (ASCII 118) |
87 | ETB | w | 87 | 421112 | w (ASCII 119) |
88 | CAN | x | 88 | 421211 | x (ASCII 120) |
89 | EM | y | 89 | 212141 | y (ASCII 121) |
90 | SUB | z | 90 | 214121 | z (ASCII 122) |
91 | ESC | { | 91 | 412121 | { (ASCII 123) |
92 | FS | | | 92 | 111143 | | (ASCII 124) |
93 | GS | } | 93 | 111341 | } (ASCII 125) |
94 | RS | ~ | 94 | 131141 | ~ (ASCII 126) |
95 (Hex 7F) | US | DEL | 95 | 114113 | DEL (ASCII 127) |
96 (Hex 80) | FNC 3 | FNC 3 | 96 | 114311 | Ç (ASCII 128) |
97 (Hex 81) | FNC 2 | FNC 2 | 97 | 411113 | ü (ASCII 129) |
98 (Hex 82) | SHIFT | SHIFT | 98 | 411311 | é (ASCII 130) |
99 (Hex 83) | CODE C | CODE C | 99 | 113141 | â (ASCII 131) |
100 (Hex 84) | CODE B | FNC 4 | CODE B | 114131 | ä (ASCII 132) |
101 (Hex 85) | FNC 4 | CODE A | CODE A | 311141 | à (ASCII 133) |
102 (Hex 86) | FNC 1 | FNC 1 | FNC 1 | 411131 | å (ASCII 134) |
103 (Hex 87) | START (Code A) | 211412 | ‡ (ASCII 135) | ||
104 (Hex 88) | START (Code B) | 211214 | ˆ (ASCII 136) | ||
105 (Hex 89) | START (Code C) | 211232 | ‰ (ASCII 137) | ||
106 (Hex 6A) | STOP (All Codes) | 2331112 | Š (ASCII 138) |