要编码一个13位数字,需要先把这些数字分为3部分:第一个数字,开始6个数字和后6个数字。开始的6个数字有两种编码模式,even模式(标识为G)和odd模式(标识为L)。第一个数字不会直接用G或L模式编码,但决定了开始6数字是用G模式还是L模式。后6个数字都会用RRRRRR模式来编码。
To encode the 13-digit EAN-13 number, the digits are split into 3 groups; the first digit, the first group of 6 and the last group of 6. The first group of 6 is encoded using a pattern whereby each digit has two possible encodings, one of which has even parity (denoted with letter G) and one of which has odd parity (denoted with letter L). The first digit is not represented directly by a pattern of bars and spaces, but is encoded indirectly, by selecting a pattern of choices between these two encodings for the first group of 6 digits, according to the table below. All digits in the last group of 6 digits are encoded using a single pattern RRRRRR, the one also used for UPC.
比如第一个数字为0,那么前6个数字全部使用LLLLLL模式来编码。UPC条码可以理解为一个0开头的EAN13条码。
If the first digit is zero, all digits in the first group of 6 are encoded using the pattern LLLLLL used for UPC, therefore, a UPC barcode is also an EAN-13 barcode with the first digit set to zero.
Structure of EAN-13
First digit |
First group of 6 digits |
Last group of 6 digits
|
0 |
LLLLLL |
RRRRRR
|
1 |
LLGLGG |
RRRRRR
|
2 |
LLGGLG |
RRRRRR
|
3 |
LLGGGL |
RRRRRR
|
4 |
LGLLGG |
RRRRRR
|
5 |
LGGLLG |
RRRRRR
|
6 |
LGGGLL |
RRRRRR
|
7 |
LGLGLG |
RRRRRR
|
8 |
LGLGGL |
RRRRRR
|
9 |
LGGLGL |
RRRRRR
|
This encoding guarantees that the first group always starts with an L-code, which has odd parity, and that the second group always starts with an R-code, which has even parity. Thus, it does not matter whether the barcode is scanned from the left or from the right, as the scanning software can use this parity to identify the start and end of the code.
EAN-8 barcodes encode all digits directly, using this scheme:
Structure of EAN-8
First group of 4 digits |
Last group of 4 digits
|
LLLL |
RRRR
|
Encoding of the digits
Digit |
L-code |
G-code |
R-code
|
0 |
0001101 |
0100111 |
1110010
|
1 |
0011001 |
0110011 |
1100110
|
2 |
0010011 |
0011011 |
1101100
|
3 |
0111101 |
0100001 |
1000010
|
4 |
0100011 |
0011101 |
1011100
|
5 |
0110001 |
0111001 |
1001110
|
6 |
0101111 |
0000101 |
1010000
|
7 |
0111011 |
0010001 |
1000100
|
8 |
0110111 |
0001001 |
1001000
|
9 |
0001011 |
0010111 |
1110100
|
Note: Entries in the R-column are bitwise complements (logical operator: negation) of the respective entries in the L-column. Entries in the G-column are the entries in the R-column in reverse bit order. See pictures of all codes against a colored background.
A run of one or more black areas is known as a "bar", and a run of one or more white areas is known as a "space". As can be seen in the table, each digit's encoding comprises two bars and two spaces, and the maximum width of a bar or space is four areas.