iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🏁

The Inner Workings of Barcodes

に公開

What is a Barcode

A symbol that represents numerical values through the thickness of striped lines, also known as a 1D code.
Barcode standards are global, but the specific standard used in Japan is called the JAN code. [1]
barcode example
Example of a JAN code

JAN Code

The standard type consists of 13 digits, while the shortened type consists of 8 digits.
Furthermore, the standard type is divided into those with a 7-digit or 9-digit manufacturer code; for 7 digits, the product code is 5 digits, and for 9 digits, the product code is 3 digits.
For both standard and shortened types, the first two digits are the country code (47 or 49 in Japan), and the last digit is the check digit.

jancode type
From Barcode Course

Structure of JAN Code

jancode structure
From Barcode Course

JAN codes are represented by bits of 0 and 1. 0 is white and 1 is black.
The constituent elements include guard bars, data characters, and a check digit.
I will explain each of these below.

Guard Bars

The bars in the barcode that protrude slightly downwards are called guard bars.
These indicate the beginning, end, and middle of the barcode.
They are represented by 101, 01010, and 101, respectively.

Data Characters

In the standard type, 6 characters are stored to the left of the center bar and 5 characters to the right.
Numerical values are converted according to the table below.
Even and odd parity[2] are taken into account when determining the first digit.

Value Left Odd Parity Left Even Parity Right Even Parity
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

The first digit of the standard type is determined by the combination of odd and even parity, as shown in the table below.
For the shortened type, all digits use odd parity.

1st Digit Combination of Odd (O) and Even (E)
0 OOOOOO
1 OOEOEE
2 OOEEOE
3 OOEEEO
4 OEOOEE
5 OEEOOE
6 OEEEOO
7 OEOEOE
8 OEOEEO
9 OEEOEO

In other words, a JAN code such as 1234567890123 for the standard type is converted as follows:

Since it is a standard type, determine the combination of odd and even parity for the 1st digit. In this case, it is 1, so according to the table above, the combination is OOEOEE.
Determine the 2nd to 6th digits based on the calculated combination of odd and even parity using the table above.
The 2nd digit is 2 with odd parity, so it becomes 0010011. Similarly, the 3rd digit is 3 with odd parity, so it is 0111101, and the 4th digit is 4 with even parity, so it is 0011101, and so on.
Since only even parity is defined for the right side, convert them according to the table.

Check Digit

The last digit of a JAN code is a check digit[3] used to determine the correctness of the reading result.
The check digit is calculated as follows:

Digit Number 1 2 3 4 5 6 7 8 9 10 11 12 13
Barcode 4 9 3 3 0 3 2 0 1 0 5 7 9
  1. Add all the numbers in the even-numbered positions and multiply the result by 3.

    (9+3+3+0+0+7)×3=66
  2. Add all the numbers in the odd-numbered positions, excluding the last digit which is the check digit.

    (4+3+0+2+1+5)=15
  3. Add the results of steps 1 and 2.

    66+15=81
  4. Subtract the remainder of the result of step 3 divided by 10 from 10. The result is the check digit.
    However, if the remainder of step 3 divided by 10 is 0, the check digit is 0.

    (10-(81\%10))\%10=9

In this case, the calculation result is 9, which matches the value of the last digit (the check digit), so the reading result is considered correct.

Generating JAN Codes

By following the above procedure, converting the JAN code 4933032010579 results in the following:

Left Guard Bar 1st Digit 2nd Digit 3rd Digit 4th Digit 5th Digit 6th Digit Center Bar 7th Digit 8th Digit 9th Digit 10th Digit 11th Digit Check Digit Right Guard Bar
101 0001011 0100001 0111101 0001101 0100001 0011011 01010 1110010 1100110 1110010 1001110 1000100 1110100 101

By coloring the 1s black and the 0s white, the JAN code is complete.

jancode

Conclusion

Personally, I found it interesting that the first digit is represented by a combination of odd and even parity.
By the way, I created a Python library for generating JAN codes, so please feel free to try it out 😊

https://pypi.org/project/jancode

The source code is available here:
https://github.com/yashikota/jancode

References

https://www.keyence.co.jp/ss/products/autoid/codereader/basic_jan.jsp

脚注
  1. JAN stands for Japanese Article Number. Incidentally, it is called the EAN code in Europe and the UPC code in the USA. ↩︎

  2. Incidentally, odd and even parity refers to whether the number of 1s is odd or even. ↩︎

  3. A method called Modulus 10/Weight 3 is used. ↩︎

GitHubで編集を提案

Discussion