2021SC@SDUSC Zxing open source code PDF417 QR code

2021SC@SDUSC

This blog continues to introduce the coding steps and process of PDF417 QR code.

1, BarcodeMatrix

The BarcodeMatrix class holds all information about barcodes in an easily accessible format.
It includes the following four member variables, which respectively represent the corresponding matrix of QR code, the current row, and the height and width of QR code.

  private final BarcodeRow[] matrix;
  private int currentRow;
  private final int height;
  private final int width;

Its constructor is:
Height and width are the height (row) and width (column) of the matrix, respectively.

  BarcodeMatrix(int height, int width) {
    matrix = new BarcodeRow[height];
    //Initialize the array to the correct width
    for (int i = 0, matrixLength = matrix.length; i < matrixLength; i++) {
      matrix[i] = new BarcodeRow((width + 4) * 17 + 1);
    }
    this.width = width * 17;
    this.height = height;
    this.currentRow = -1;
  }

2, BarcodeRow

The BarcodeRow class is used to create rows of barcodes.

Its member variables are:

  private final byte[] row;
  private int currentLocation;

The main methods are:
Where x represents the position in the bar; Black is a Boolean value. If the bar is black, it is true; False if the bar is white; width indicates how many points the bar is wide.

The set(int x, boolean black) method is used to set a specific position in the column; addBar(boolean black, int width) is used to add a bar.

  private void set(int x, boolean black) {
    row[x] = (byte) (black ? 1 : 0);
  }

  void addBar(boolean black, int width) {
    for (int ii = 0; ii < width; ii++) {
      set(currentLocation++, black);
    }
  }

The getScaledRow(int scale) method is used to scale rows. Scale represents the size of the scaled image, which must be greater than or equal to 1; This function returns a scaled row.

  byte[] getScaledRow(int scale) {
    byte[] output = new byte[row.length * scale];
    for (int i = 0; i < output.length; i++) {
      output[i] = row[i / scale];
    }
    return output;
  }

3, Comparison

The comparison class is an enumeration class used to represent possible PDF417 barcode compression types.
There are four types: AUTO, TEXT, BYTE and NUMERIC

public enum Compaction {
  AUTO,
  TEXT,
  BYTE,
  NUMERIC
}

4, Dimensions

Dimensions is a data object used to specify the minimum number of rows and maximum number of columns of the PDF417 barcode.

The four member variables represent the minimum number of columns, the maximum number of columns, the minimum number of rows, and the maximum number of rows.

public final class Dimensions {

  private final int minCols;
  private final int maxCols;
  private final int minRows;
  private final int maxRows;

  public Dimensions(int minCols, int maxCols, int minRows, int maxRows) {
    this.minCols = minCols;
    this.maxCols = maxCols;
    this.minRows = minRows;
    this.maxRows = maxRows;
  }
  public int getMinCols() {return minCols;}
  public int getMaxCols() {return maxCols;}
  public int getMinRows() {return minRows;}
  public int getMaxRows() {return maxRows;}
}

5, PDF417ErrorCorrection

PDF417ErrorCorrection is the error correction code of PDF417 and follows the algorithm described in Chapter 4.10 of ISO/IEC 15438:2001 (E).

Geterrorcorrecorrectioncodewordcount (int errorcorrecorrectionlevel) is used to determine the number of error correction code words of the specified error correction level.
errorCorrectionLevel is the error correction level (0-8);
Function returns the number of codewords generated for error correction.

  static int getErrorCorrectionCodewordCount(int errorCorrectionLevel) {
    if (errorCorrectionLevel < 0 || errorCorrectionLevel > 8) {
      throw new IllegalArgumentException("Error correction level must be between 0 and 8!");
    }
    return 1 << (errorCorrectionLevel + 1);
  }

The getRecommendedMinimumErrorCorrectionLevel(int n) function returns the recommended minimum error correction level described in Appendix E of ISO/IEC 15438:2001 (E).
n is the number of data codewords.

  static int getRecommendedMinimumErrorCorrectionLevel(int n) throws WriterException {
    if (n <= 0) {
      throw new IllegalArgumentException("n must be > 0");
    }
    if (n <= 40) {return 2;}
    if (n <= 160) {return 3;}
    if (n <= 320) {return 4;}
    if (n <= 863) {return 5;}
    throw new WriterException("No recommendation possible");
  }

The generateerrorcorrection (charsequence datacodews, int errorcorrectionlevel) function generates an error correction codeword according to 4.10 in ISO/IEC 15438:2001 (E).
Data codewords are data codewords; errorCorrectionLevel is the error correction level (0-8).

  static String generateErrorCorrection(CharSequence dataCodewords, int errorCorrectionLevel) {
    int k = getErrorCorrectionCodewordCount(errorCorrectionLevel);
    char[] e = new char[k];
    int sld = dataCodewords.length();
    for (int i = 0; i < sld; i++) {
      int t1 = (dataCodewords.charAt(i) + e[e.length - 1]) % 929;
      int t2;
      int t3;
      for (int j = k - 1; j >= 1; j--) {
        t2 = (t1 * EC_COEFFICIENTS[errorCorrectionLevel][j]) % 929;
        t3 = 929 - t2;
        e[j] = (char) ((e[j - 1] + t3) % 929);
      }
      t2 = (t1 * EC_COEFFICIENTS[errorCorrectionLevel][0]) % 929;
      t3 = 929 - t2;
      e[0] = (char) (t3 % 929);
    }
    StringBuilder sb = new StringBuilder(k);
    for (int j = k - 1; j >= 0; j--) {
      if (e[j] != 0) {
        e[j] = (char) (929 - e[j]);
      }
      sb.append(e[j]);
    }
    return sb.toString();
  }

6, PDF417HighLevelEncoder

The PDF417HighLevelEncoder class is the advanced encoder of PDF417.

Contains multiple member variables.

Member variableValueexplain
TEXT_COMPACTION0Text compression code
BYTE_COMPACTION1Byte compression code
NUMERIC_COMPACTION2Digital compression code
SUBMODE_ALPHA0Text compression sub mode Alpha
SUBMODE_LOWER1Text compression sub mode
SUBMODE_MIXED2Text compression sub mode mixing
SUBMODE_PUNCTUATION3Text compression sub mode punctuation
LATCH_TO_TEXT900Mode latch to text compression mode
LATCH_TO_BYTE_PADDED901Mode latched to byte compression mode (the number of characters is not a multiple of 6)
LATCH_TO_NUMERIC902Mode latched to digital compression mode
SHIFT_TO_BYTE913The mode is converted to byte compression mode
LATCH_TO_BYTE924Mode latch to byte compression mode (multiple of 6 characters)
ECI_USER_DEFINED925Identifier of the user-defined extended channel interpretation (ECI)
ECI_GENERAL_PURPOSE926Identifier in common ECO format
ECI_CHARSET927ECI identifier of the code page character set
TEXT_MIXED_RAWOriginal code table of text compression mixed sub mode
TEXT_PUNCTUATION_RAWOriginal code table for text compression: punctuation mode

The member functions are:

Member functionexplain
encodeHighLevel(String msg, Compaction compaction, Charset encoding)Perform advanced encoding of the PDF417 message using the algorithm described in Appendix P of ISO/IEC 15438:2001 (E). If byte compression is selected, only byte compression is used. Returns the encoded message (character values range from 0 to 928)
encodeText(CharSequence msg, int startpos,int count,StringBuilder sb, int initialSubmode)Part of the information is encoded using text compression. Returns the text sub mode at the end of this method
encodeBinary(byte[] bytes, int startpos,int count,int startmode,StringBuilder sb)Some information is encoded using byte compression. Unicode characters are converted to binary using the cp437 code page. Returns the received encoded codeword.
determineConsecutiveDigitCount(CharSequence msg, int startpos)Determines the number of consecutive characters that can be encoded using digital compression. Returns the number of characters requested.
determineConsecutiveTextCount(CharSequence msg, int startpos)Determines the number of consecutive characters that can be encoded using text compression. Returns the number of characters requested.
determineConsecutiveBinaryCount(String msg, int startpos, Charset encoding)Determines the number of consecutive characters that can be encoded using binary compression. Returns the number of characters requested.

Keywords: Java

Added by mrkite on Thu, 23 Dec 2021 23:17:51 +0200