2021SC@SDUSC Zxing open source code PDF417 QR code


This blog briefly introduces the relevant knowledge of PDF417 QR code and the coding steps and processes.

1, PDF417 overview


PDF417 is a two-dimensional bar code invented by Symbol Technologies, Inc., which is a stacked two-dimensional bar code. PDF(Portable Data File) means "portable data file". Each bar code character constituting the bar code is composed of 4 bars and 4 blanks, a total of 17 modules, so it is called PDF417 bar code. PDF417 barcode can only be recognized by a barcode reader with 417 decoding function. The inventor is Dr. Wang Yinjun, who went to Taiwan for aesthetics. Dr. Wang graduated from the Department of information of national Jiaotong University in 1984 and obtained the computer master's and doctoral degrees from the University of New York at Stony Brook. In 1988, he entered symbol technology to carry out the research and development of two-dimensional bar code, and launched PDF417 at the end of 1992, He led the development of the world's first two-dimensional bar code laser code reading system from 1989 to 1992. In March 1993, the author officially introduced PDF417 into Taiwan and handed it over to Xiangji information promotion and research and development of related package software (Huang Qingxiang, 1995).

At present, PDF417, Maxicode and Datamatrix have been selected as the development scope of two-dimensional bar code international standard by American National Standards Institute (ANSI) mh10 sbc-8 Committee. Among them, PDF417 is mainly intended to be applied to transportation packages and commodity information labels (Burnell, 1995). PDF417 not only has error detection capability, but also can read back complete data from damaged bar codes (Moore, 1994), that is, "error recovery capability", and its error recovery rate is up to 50%.

PDF417 bar code is a portable data file with high density and high information content. It is an ideal means to realize the automatic storage, carrying and automatic reading of large capacity and high reliability information such as certificates and cards.

PDF417 QR code can be applied to: certificate management, annual license inspection, report management, production and assembly of mechanical and electrical products, bank bill management, transportation and mailing of luggage and goods. The biggest advantage of PDF417 barcode lies in its huge data capacity and strong error correction ability.

1.2 structure of PDF417 two-dimensional bar code

Due to the large capacity of PDF417 two-dimensional bar code, in addition to encoding people's name, unit, address, telephone and other basic data, it can also store human characteristics such as fingerprints, retinal scanning, photos and other personal records in the bar code, which can not only realize the automatic input of certificate data, but also prevent the forgery of certificates and reduce crime. PDF417 has been applied in annual license review, vehicle violation registration, fines and periodic inspection of transportation departments in the United States, Canada and New Zealand. The United States also applies PDF417 to ID card, driver's license and military card. In addition, Mexico also applies PDF417 to customs declaration documents and certificates, so as to prevent counterfeiting and crime.

PDF417 is a public code. Anyone can use its algorithm without paying. Therefore, it is an open bar code system. PDF of PDF417 is the abbreviation of portable data file. Its bar code is similar to a data file. It can store more data and can be carried with you or with the product (Paclidis, 1992). As its name suggests, the storage capacity of each PDF code can be as high as 1108 bytes. If the number is compressed, it can be stored to 2729 bytes.

Each PDF417 code is stacked in 3 ~ 90 rows. For scanning convenience, there is a static space around it. The static space is divided into horizontal static space and vertical static space, which should be at least 0.020 inch, as shown in the figure:

Each floor consists of the following five parts:

Start code.
Left label area: after the start code, it is an indicator character.
Data area: it can hold 1 ~ 30 data characters.
Right label area: after the data area, it is an indicator character.
End code: at the far right of the column.

In addition to the start code and end code, the constituent characters of the left label area, data area and right label area can be called codeword. Each code is composed of 17 modules. Each code can be divided into 4 lines (or black lines) and 4 blank (or white lines). Each line can not exceed the width of 6 modules at most. Due to different data sizes, the number of rows, data modules and word codes of each 417 code can range from 1 to 30. The composition of word code is shown in the figure:

1.3 dimension of PDF417 2D barcode

Because the combination of symbols is more flexible, each PDF417 two-dimensional bar code can be printed into different length width ratio and density according to different physical equipment to meet the requirements of printing conditions and scanning conditions. The width x of each module is one of the most important dimensions in PDF417 code. The minimum limit of x value is 0.0075 inch (about 0.191mm). In the same bar code symbol, the value of X is fixed.

The minimum height and length of PDF417 can be calculated by the following formula:

W= (17C+69)X+ 2Q
H = R ×Y+ 2Q

Of which:
W = bar code width, H = bar code height, X = bar code module width, Y = number of layers
C = total number of symbol characters of each layer (including left and right standard areas), R = floor height, Q = size of dead space

1.4 error correction capability of PDF417 two-dimensional bar code

An important feature of PDF417 two-dimensional bar code is its high ability to automatically correct errors. However, the error correction ability of PDF417 is related to the amount of data that can be stored in each bar code. PDF417 code divides error recovery into 9 levels, and its value ranges from 0 to 8. The higher the level, the stronger the error correction ability, but the less data that can be stored. It is generally recommended to include at least 10% of the check word code. The relationship between data storage and error correction level is shown in the table below:

Error correction levelCorrected code numberData storage capacity (bits)
Automatic setting641024

The following table suggests the error correction levels applicable to different word numbers;

Number of data codesError correction level

As mentioned earlier, the error correction level involves two error types: read rejection error (E error) and substitution error (T error). No matter which barcode machine is used, there is a certain precision limit, resulting in a deviation between the width of lines and blanks and the ideal width. The barcode scanning equipment can read the inaccurate barcode symbols within the allowable range of the decoding algorithm. At present, the minimum value of X specified in the standard is 0.0075 inch (about 0.191mm). This limit also reflects the technical status of the current standard equipment.

1.5 characteristics of PDF417

Based on the discussion in this section, the characteristics of PDF417 are shown in the following table:

Codeable character set8-bit binary data, up to 811800 different character sets or interpretations
typeContinuous, multilayer
Character Self-Checking have
sizeVariable height: 390 floor width: 130 columns
Code reading modeBidirectional readability
Error correction code number2 ~ 512
Maximum data capacityThe security level is 0, and each symbol can represent 1108 bits

2, PDF417Writer

Similar to the QR code described earlier, the PDF417Writer class inherits the parent class Writer.

PDF417Writer has two static variables:

Indicates the default white space (margin) around the code and the default error correction level, respectively.

  private static final int WHITE_SPACE = 30;
  private static final int DEFAULT_ERROR_CORRECTION_LEVEL = 2;

The value of PDF 417 is saved by an array.

input is an information byte array, 0 is black and 1 is white;
Margin is the margin border around the barcode;
Returns the input bit matrix.

  private static BitMatrix bitMatrixFromBitArray(byte[][] input, int margin) {
    // Create a bit matrix with extra spaces
    BitMatrix output = new BitMatrix(input[0].length + 2 * margin, input.length + 2 * margin);
    for (int y = 0, yOutput = output.getHeight() - margin - 1; y < input.length; y++, yOutput--) {
      byte[] inputY = input[y];
      for (int x = 0; x < input[0].length; x++) {
        // Zeros are white in the byte matrix
        if (inputY[x] == 1) {
          output.set(x + margin, yOutput);
    return output;

In addition, there is the function rotateArray(), which obtains the PDF417 QR code array and rotates it 90 degrees.
This makes the QR code in the same direction on the screen when rotating the screen.

  private static byte[][] rotateArray(byte[][] bitarray) {
    byte[][] temp = new byte[bitarray[0].length][bitarray.length];
    for (int ii = 0; ii < bitarray.length; ii++) {
      int inverseii = bitarray.length - ii - 1;
      for (int jj = 0; jj < bitarray[0].length; jj++) {
        temp[jj][inverseii] = bitarray[ii][jj];
    return temp;

3, class PDF417

PDF417 implements the top-level class of the logical part.

There are two static variables, representing start mode (17 bits) and stop mode (18 bits).

  private static final int START_PATTERN = 0x1fea8;
  private static final int STOP_PATTERN = 0x3fa29;

Next, several set methods are introduced:

setDimensions(int maxCols, int minCols, int maxRows, int minRows) is used to set the maximum / minimum row / column values. Its parameters respectively represent the maximum number of columns allowed, the minimum number of columns allowed, the maximum number of rows allowed and the minimum number of rows allowed.

Setcomparison (comparison compression) sets the compression mode to use.

setCompact(boolean compact) enables compression if compact is true.

setEncoding(Charset encoding) sets the character encoding to use.

  public void setDimensions(int maxCols, int minCols, int maxRows, int minRows) {
    this.maxCols = maxCols;
    this.minCols = minCols;
    this.maxRows = maxRows;
    this.minRows = minRows;

  public void setCompaction(Compaction compaction) {
    this.compaction = compaction;

  public void setCompact(boolean compact) {
    this.compact = compact;

  public void setEncoding(Charset encoding) {
    this.encoding = encoding;

determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords) are used to determine the best columns and rows for the specified number of codewords.
Where sourceCodeWords is the number of code words and errorCorrectionCodeWords is the number of error correction code words. The function returns the dimension object, including columns as width and rows as height.

  private int[] determineDimensions(int sourceCodeWords, int errorCorrectionCodeWords) throws WriterException {
    float ratio = 0.0f;
    int[] dimension = null;
    for (int cols = minCols; cols <= maxCols; cols++) {
      int rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCodeWords, cols);
      if (rows < minRows) {
      if (rows > maxRows) {
      float newRatio = ((float) (17 * cols + 69) * DEFAULT_MODULE_WIDTH) / (rows * HEIGHT);
      // If the previous ratio is closer to the preferred ratio, ignore
      if (dimension != null && Math.abs(newRatio - PREFERRED_RATIO) > Math.abs(ratio - PREFERRED_RATIO)) {
      ratio = newRatio;
      dimension = new int[] {cols, rows};
     // Handle cases where the minimum value is greater than the necessary value
     if (dimension == null) {
       int rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCodeWords, minCols);
       if (rows < minRows) {
         dimension = new int[]{minCols, minRows};
    if (dimension == null) {
      throw new WriterException("Unable to fit message in columns");
    return dimension;

reference material:

Detailed explanation of PDF417 2D barcode

Keywords: Java

Added by sanlove on Fri, 17 Dec 2021 13:08:06 +0200