Code 128 is a very high-density barcode symbology.
It is used for alphanumeric or numeric-only barcodes.
It can encode all 128 characters of ASCII and, by use of an extension character, the
Latin-1 characters defined in ISO/IEC 8859-1.
GS1-128 is a subset of Code 128 and is used extensively world wide in shipping and packaging
industries as a product identification code for the container and pallet levels in the
supply chain.
The symbology was formerly defined as ISO/IEC 15417:2007.
Specification
A Code 128 barcode will have six sections: Quiet Zone
Start Character Encoded Data
Check Character Stop Character
Quiet Zone The check character is calculated from a weighted
sum of all the characters.
Subtypes Code 128 includes 108 symbols: 103 data symbols,
3 start symbols, and 2 stop symbols.
The symbols consist of three black bars and three white spaces.
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 read the symbol in reverse.
To represent all 128 ASCII values, there are three code sets, which can be mixed within
a single barcode: 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 The quiet zone should be at least seven times
the width of the narrowest bar/space element.
It is mandatory at the left and right side of the barcode.
The quiet zone must not be less than 4mm-1.65mm depending on the type of bar code.
Source: Figure 5.2.1.4.4 – 1 of GS1 General Specifications
Start/stop and encoded data Each character in the barcode symbol 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 character.
For instance, encoding the ASCII value 0 can be viewed as 10011101100, where a 1 is a bar
and a 0 is a space.
A single 1 would be the thinnest line in the bar code.
Three 1 in sequence indicates a bar three times as thick as a single 1 bar.
The code uses 107 of the 108 possible patterns that fulfill the required criteria, with the
pattern 211133 deliberately remaining unused.
This is done to enhance omnidirectional scanning and help prevent misreads.
Check digit calculation The check digit is a Modulo 103 checksum.
It is calculated by summing the start code 'value' to the products of each character's
'value' multiplied by its position in the barcode string.
The start character and first encoded value are in position 1.
The sum of the start code value and the products is then divided by 103.
The remainder of the division is the check digit's 'value' which is then converted into
a character and appended to the end of the barcode.
For example, in the following table, the code 128 variant A checksum value is calculated
for the alphanumeric string PJJ123C Calculating check digit with multiple variants
As Code 128 allows multiple variants, as well as switching between variants within a single
barcode, it is important to remember that the absolute Code 128 value of a character
is completely independent of its value within a given variant.
For instance the Variant C value "33" and the Variant B value "A" are both considered
to be a Code 128 value of 33, and the check digit would be computed based on the value
of 33 times the character's position within the barcode.
Using FNC4 to encode high characters Function 4 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 GS128.
When a single FNC4 is present in a string, the following character will be converted
to ASCII as usual and then have 128 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 bars
and spaces for each character except the Stop character, which uses 7.
Thus, each character begins with a bar and ends with a space.
The following tables detail the widths associated with each bar and space for each character.
The width of each bar or space can be 1, 2, 3 or 4 units.
Using the example above, an 'A' would be depicted with the pattern 10100011000, or as 111323
in the tables below.
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 accoding ASCII char depends on the actual used barcode-font.
Especially the ASCII char of value 0 and of value 95 and above can 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 that it 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.
For example, the U.S. ZIP code for the White House would generally be printed as "(421)
840 20500", but would actually be coded as "[Start C] [FNC1] 42 18 40 20 50 [Code A]
0 [Check symbol 92] [Stop]" Check Digit Calculation for above Zip Code
Example 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 Although code set C can uses one code symbol
to represent two digit, its usage does not necessarily make a more compact code compared
to code sets A or B. While encoding "X00Y" with code set A or B results in 7 code symbols,
an encoding the inner "00" with code set C will result in code 8 symbols long due to
the overhead in switching in and out of code set C. Although one symbol is saved on encoding
"00" to code symbol 0, two code set switching symbols are required: the first to switch
into code set C and the second to switch back to code set A or B.
Analogically, applying of code set C to a sequence of digits produces code with fewer
symbols compared to code sets A or B only under these conditions:
Example: For data "098X1234567Y23", savings on barcode length using code set C are achieved
only if it is applied to middle part of data.
For beginning and ending part of the code, switching to code set C is not effective.
Code symbols: [Start B] 16 25 24 56 17 [Code C] 23 45 67 [Code B] 57 18 19 [checksum] [Stop].
The description above is incomplete.
The mistake is a transition from Code A or Code B to Code C is not done when there are
an odd number of digits remaining.
It is better to code the first digit in A/B and then switch to C. The delay avoids an
extra symbol.
Consider the string "...01234": a delayed switch produces ... 0 [Code C] 12 34 [checksum]
[Stop] but an early switch produces ... [Code C] 01 23 [Code A] 4 [checksum] [Stop].
Optimizing length of resulting barcode is important where barcode readers are used which
require to detect the entire barcode image at once in order to read a barcode, such as
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.
References
External links GS1-128 Specification Detailed List of Application
Identifiers Barcodesoft Font mapping of Barcodesoft, which
differs from the common ascii mapping.
The 128 code Learn the Code 128 encoding algorithm with a font-based barcode solution.
Online barcode generator Free online Barcode generator for the various barcode types.
Free Barcode Generator for Excel Free Excel Macro and font for Multiple Barcode 128 Generation.
Sample code ZXing Multiplatform open source barcode scanner
/ generator with versions available in Java and ports to ActionScript, C++, C#, ObjectiveC
and Ruby.
Python Bar Code 128 This code appears to draw boxes 1 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".
GenCode128 Free C# source code implementation of Code128.
Almost all features are implemented, but is not 100% complete.
Barcode1DTools Ruby gem Ruby source code for many 1D barcode symbologies including Code
128.
Perl barcode generation code Perl source code for many 1D barcode symbologies including
Code 128.
Barcode::Code128 Free Perl barcode generation module.
GOCR Free OCR with Code 128 recognition.
Barcode Code 128 Free JavaScript source code implementation of Code128.
Barcode4J Free Java API with implementation of Code128 and other standard barcodes.
JavaScript Code 128 Open source JavaScript implementation of Code128 and other linear
barcodes.