Code 128
Code 128 is a high-density linear barcode symbology defined in ISO/IEC 15417:2007. It is used for alphanumeric or numeric-only barcodes. It can encode all 128 characters of ASCII and, by use of an extension symbol, the Latin-1 characters defined in ISO/IEC 8859-1.. It generally results in more compact barcodes compared to other methods like Code 39, especially when the texts contain mostly digits.
GS1-128 is a subset of Code 128 and is used extensively worldwide in shipping and packaging industries as a product identification code for the container and pallet levels in the supply chain.
Specification
A Code 128 barcode has seven sections:- Quiet zone
- Start symbol
- Encoded data
- Check symbol
- Stop symbol
- Final bar
- Quiet zone
Subtypes
Code 128 includes 108 symbols: 103 data symbols, 3 start symbols, and 2 stop symbols. Each symbol consists of three black bars and three white spaces of varying widths. All widths are multiples of a basic "module". Each bar and space is 1 to 4 modules wide, and the symbols are fixed width: the sum of the widths of the three black bars and three white bars is 11 modules.The stop pattern is composed of two overlapped symbols and has four bars. The stop pattern permits bidirectional scanning. When the stop pattern is read left-to-right, the stop symbol is recognized. When the stop pattern is read right-to-left, the reverse stop symbol is recognized. A scanner seeing the reverse stop symbol then knows it must skip the 2-module bar and read the rest of the barcode in reverse.
Despite its name, Code 128 does not have 128 distinct symbols, so it cannot represent 128 code points directly. To represent all 128 ASCII values, it shifts among three code sets :
- 128A ASCII characters 00 to 95, special characters, and FNC 1–4
- 128B ASCII characters 32 to 127, special characters, and FNC 1–4
- 128C 00–99 and FNC1
Quiet zone
Start/stop and encoded data
Each symbol in the barcode is composed of three bars and three spaces. Each bar or space is 1, 2, 3 or 4 units wide, the sum of the widths of bars must be even, the sum of the widths of the spaces must be odd, and total 11 units per symbol. For instance, encoding the ASCII character "0" can be viewed as 10011101100, where a sequence of 1's is a bar and a sequence of 0's is a space. A single 1 would be the thinnest line in the bar code. Three 1's in sequence indicates a bar three times as thick as a single 1 bar.There are 108 possible 11-unit wide symbols, and the code uses all possible symbols. Two of the symbols are used for stop indication, stop and reverse stop. The two stop symbols are special because they are always followed by a 2-unit bar, forming a 13-unit long stop pattern. Reading the stop pattern left to right is the stop symbol, and reading the stop pattern right to left is the reverse stop symbol.
Check digit calculation
The check digit is a weighted modulo-103 checksum. It is calculated by summing the start code 'value' to the products of each symbol's 'value' multiplied by its position in the barcode string. The start symbol and first encoded symbol are in position 1. The sum of the products is then reduced modulo 103. The remainder is then converted back to one of the 103 non-delimiter symbols and appended to the barcode, immediately before the stop symbol.For example, in the following table, the code 128 variant A checksum value is calculated for the alphanumeric string PJJ123C:
For the purpose of computing the check symbol, the shift and code-switch symbols are treated the same as any other symbol in the bar code. The checksum is computed over the symbol values, without regard for which code set is active at the time. For instance the code set C value "33" and the code set B value "A" are both considered to be a Code 128 value of 33, and the check digit is computed based on the value of 33 times the symbol's position within the barcode.
Using FNC4 to encode high (128–255) characters
The special symbol FNC4, present only in code sets A and B, can be used to encode all the Latin-1 characters in a Code 128 barcode. The feature is not widely supported and is not used in GS1-128. When a single FNC4 is present in a string, the following symbol is converted to ASCII as usual, and then 128 is added to the ASCII value. If two FNC4s are used consecutively then all following characters will be treated as such, up to the end of the string or another pair of FNC4s. Between the double FNC4s, a single FNC4 will be used to denote that the following character will be standard ASCII.Bar code widths
Code128 specifies a combination of 6 alternating bars and spaces for each symbol. Thus, each symbol begins with a bar and ends with a space. In barcode fonts, the final bar is generally combined with the stop symbol to make a wider stop pattern. The following table details the widths associated with each bar and space for each symbol. The width of each bar or space may be 1, 2, 3 or 4 units. Using the example above, an 'A' would be depicted with the pattern 10100011000, or as widths 111323 in the tables below.The widths value is derived by counting the length of each run of 1's then 0's in the pattern, starting from the left. There will always be 6 runs and the lengths of these 6 runs form the Widths value. For example, using the pattern 10100011000, the run lengths are 1, 1, 1, 3, 2, 3. Reporting just the lengths of each run gives 1, 1, 1, 3, 2, 3, thereby producing a widths value of 111323.
The "Code A", "Code B" and "Code C" symbols cause all future symbols to be interpreted according to the corresponding subcode. The "Shift" symbol switches a single following symbol's interpretation between subcodes A and B.
The encoded ASCII char depends on the actual used barcode-font. Especially the ASCII char of value 0 and of value 95 and above may be defined differently in the font that is installed.
The FNCx codes are used for special purposes. FNC1 at the beginning of a bar code indicates a GS1-128 bar code which begins with a 2- 3- or 4-digit application identifier assigned by the Uniform Code Council, which explains the following digits. For example, application identifier 421 indicates that an ISO 3166-1 numeric country code and ship-to postal code follows. Thus, the U.S. ZIP code for the White House would generally be printed as " 840 20500", but would actually be coded as "
42 18 40 20 50 16
"Check digit calculation for the above Zip code example:
Value | Weight | Weight × Value | |
Start C | 105 | 1 | 105 |
FNC1 | 102 | 1 | 102 |
42 | 42 | 2 | 84 |
18 | 18 | 3 | 54 |
40 | 40 | 4 | 160 |
20 | 20 | 5 | 100 |
50 | 50 | 6 | 300 |
Code A | 101 | 7 | 707 |
0 | 16 | 8 | 128 |
Sum = | 1740 | ||
1740 | Mod 103 = | 92 |
Availability
For the end user, Code 128 barcodes may be generated by either an outside application to create an image of the barcode, or by a font-based barcode solution. Either solution requires the use of an application or an application add in to calculate the check digit and create the barcode.Barcode length optimization
Code set C uses one code symbol to represent two digits, so when the text contains just digits it will generally result in shorter barcodes. However, when the string contains only a few digits or it's mixed with non-digit character, it does not always produce a more compact code than code sets A or B. Using code set C saves one symbol per two digits, but costs a mode-shift symbol to enter and exit the set. Thus, it is only worth using if there are enough consecutive digits. For example, encoding the string "X00Y" with code set A or B requires 7 code symbols, while using code set C for the "00" would result in a code 8 symbols long.Using code set C is only advantageous under the following conditions:
Location of digits | Number of consecutive digits |
beginning of data | 4+ |
end of data | 4+ |
middle of data | 6+ |
entire data | either 2 or 4+ |
At the end of a string, delaying the transition to code set C until there are an even number of digits remaining avoids an extra symbol. Consider the string "...01234": a delayed switch produces
... 0 12 34
but an early switch produces ... 01 23 4
.For example, given the string "098x1234567y23", savings on barcode length using code set C are achieved only if it is applied to middle part of the string. For the beginning and ending part of the string, switching to code set C is not effective. As there are an odd number of digits in the middle of the string, the odd one must be use a different code, set, but it makes no difference whether this is the first or last; 16 symbols are required in either case:
0 9 8 x 1 23 45 67 y 2 3
, or 0 9 8 x 12 34 56 7 y 2 3
.Optimizing the length of the resulting barcode is important when barcode readers are used which must detect the entire barcode image at once in order to read it, such as common laser scanners. The longer the barcode is, the greater distance of laser barcode reader from barcode image is needed, making reading difficult or impossible above some threshold lengths/distances.
The optimal encoding can be found using a dynamic programming algorithm.
Sample code
- – Multiplatform open source barcode scanner / generator with versions available in Java and ports to ActionScript, C++, C#, ObjectiveC and Ruby.
- – This code appears to draw boxes one pixel wide. It appears it was modified from a short line long line bar code which would have drawn lines. The "Black boxes" should be the same size as the "White Boxes".
- – Free C# source code implementation of Code128. Almost all features are implemented, but is not 100% complete.
- – Ruby source code for many 1D barcode symbologies including Code 128.
- – Perl source code for many 1D barcode symbologies including Code 128.
- – Free Perl barcode generation module.
- – Free OCR with Code 128 recognition.
- – Free JavaScript source code implementation of Code128.
- – Free Java API with implementation of Code128 and other standard barcodes.
- – Open source JavaScript implementation of Code128 and other linear barcodes.