java.lang.Object
com.google.zxing.qrcode.encoder.MatrixUtil

final class MatrixUtilextends Object
Utility class for QR code matrix operations.
  • Field Details

    • POSITION_DETECTION_PATTERN

      private static final int[][] POSITION_DETECTION_PATTERN
      The 7 by 7 position detection pattern.
    • POSITION_ADJUSTMENT_PATTERN

      private static final int[][] POSITION_ADJUSTMENT_PATTERN
      The 5 by 5 position adjustment pattern.
    • POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE

      private static final int[][] POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE
      The adjustment pattern centers, by version, from JISX 0510, with the coordinates doubled.
    • TYPE_INFO_COORDINATES

      private static final int[][] TYPE_INFO_COORDINATES
      The type info bit positions in the matrix, copied at the top left corner.
    • VERSION_INFO_POLY

      private static final int VERSION_INFO_POLY
      Generator polynomial of the version bits, from JISX 0510.
      See Also:
    • TYPE_INFO_POLY

      private static final int TYPE_INFO_POLY
      Generator polynomial of the type info bits, from JISX 0510.
      See Also:
    • TYPE_INFO_MASK_PATTERN

      private static final int TYPE_INFO_MASK_PATTERN
      Mask XORed into the type info bits.
      See Also:
  • Constructor Details

    • MatrixUtil

      private MatrixUtil()
      Prevents instantiating the utility class.
  • Method Details

    • clearMatrix

      static void clearMatrix(ByteMatrix matrix)
      Set all cells to empty (-1)
      Parameters:
      matrix - the matrix to reset, every cell overwritten with the empty value -1
    • buildMatrix

      static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, Version version, int maskPattern, ByteMatrix matrix) throws WriterException
      Build the QR code matrix
      Parameters:
      dataBits - the codeword bits to lay down into the data region
      ecLevel - the error correction level whose type information is embedded
      version - the symbol version, deciding whether the version information is embedded
      maskPattern - the data mask 0-7 to apply, or -1 to leave the data unmasked
      matrix - the matrix to populate, sized for the given version
      Throws:
      WriterException - if the mask pattern is out of range, or the data bits do not exactly fill the remaining data region
    • embedBasicPatterns

      static void embedBasicPatterns(Version version, ByteMatrix matrix) throws WriterException
      Embed position detection, timing, and adjustment patterns
      Parameters:
      version - the symbol version, deciding whether the adjustment patterns appear
      matrix - the matrix to lay the function patterns into
      Throws:
      WriterException - if a corner cell is already occupied, so the detection or separation patterns do not fit
    • embedTypeInfo

      static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) throws WriterException
      Embed type information
      Parameters:
      ecLevel - the error correction level encoded into the type information
      maskPattern - the data mask 0-7 in use, also encoded into the type information
      matrix - the matrix to write the two type information copies into
      Throws:
      WriterException - if the mask pattern is out of range, or the generated type information is not the expected 15 bits
    • maybeEmbedVersionInfo

      static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix) throws WriterException
      Embed version info if version >= 7
      Parameters:
      version - the symbol version; nothing is written below version 7
      matrix - the matrix to write the two version information copies into
      Throws:
      WriterException - if the generated version information is not the expected 18 bits
    • embedDataBits

      static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix) throws WriterException
      Embed data bits into matrix
      Parameters:
      dataBits - the codeword bits to place, in the order they are consumed
      maskPattern - the data mask 0-7 to apply, or -1 to place the bits unmasked
      matrix - the matrix to fill, whose non-empty cells mark the already embedded function patterns and are skipped
      Throws:
      WriterException - if the bits are not all consumed once the data region is full, meaning they did not fit the symbol
    • findMSBSet

      static int findMSBSet(int value)
      Find the bit length of a value
      Parameters:
      value - input value
      Returns:
      the position one past the most significant bit set, so 32 for a negative value and 0 for zero
    • calculateBCHCode

      static int calculateBCHCode(int value, int poly)
      Calculate BCH code
      Parameters:
      value - the data whose error correction code is wanted
      poly - the BCH generator polynomial, whose bit length fixes the code, for example 0x537 for type information or 0x1f25 for version information
      Returns:
      the remainder of dividing value by poly, the BCH error correction code
    • makeTypeInfoBits

      static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits) throws WriterException
      Make type information bit vector
      Parameters:
      ecLevel - the error correction level to encode
      maskPattern - the data mask 0-7 to encode alongside the level
      bits - the vector to append the 15 type information bits to
      Throws:
      WriterException - if the mask pattern is out of range, or the generated type information is not the expected 15 bits
    • makeVersionInfoBits

      static void makeVersionInfoBits(Version version, BitArray bits) throws WriterException
      Make version information bit vector
      Parameters:
      version - the symbol version whose number is encoded
      bits - the vector to append the 18 version information bits to
      Throws:
      WriterException - if the generated version information is not the expected 18 bits
    • isEmpty

      private static boolean isEmpty(int value)
      Checks whether a matrix cell is unset.
      Parameters:
      value - the cell value to check
      Returns:
      true if the cell is still empty
    • embedTimingPatterns

      private static void embedTimingPatterns(ByteMatrix matrix)
      Draws the timing patterns into the matrix.
      Parameters:
      matrix - the matrix to draw on
    • embedDarkDotAtLeftBottomCorner

      private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException
      Draws the always dark bottom left module.
      Parameters:
      matrix - the matrix to draw on
      Throws:
      WriterException - if the module is already set
    • embedHorizontalSeparationPattern

      private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException
      Draws the horizontal separator beside a pattern.
      Parameters:
      xStart - the left column of the separator
      yStart - the top row of the separator
      matrix - the matrix to draw on
      Throws:
      WriterException - if a module is already set
    • embedVerticalSeparationPattern

      private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) throws WriterException
      Draws the vertical separator beside a pattern.
      Parameters:
      xStart - the left column of the separator
      yStart - the top row of the separator
      matrix - the matrix to draw on
      Throws:
      WriterException - if a module is already set
    • embedPositionAdjustmentPattern

      private static void embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix)
      Draws one position adjustment pattern.
      Parameters:
      xStart - the left column of the pattern
      yStart - the top row of the pattern
      matrix - the matrix to draw on
    • embedPositionDetectionPattern

      private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix)
      Draws one position detection pattern.
      Parameters:
      xStart - the left column of the pattern
      yStart - the top row of the pattern
      matrix - the matrix to draw on
    • embedPositionDetectionPatternsAndSeparators

      private static void embedPositionDetectionPatternsAndSeparators(ByteMatrix matrix) throws WriterException
      Draws the three position detection patterns with their separators.
      Parameters:
      matrix - the matrix to draw on
      Throws:
      WriterException - if a module is already set
    • maybeEmbedPositionAdjustmentPatterns

      private static void maybeEmbedPositionAdjustmentPatterns(Version version, ByteMatrix matrix)
      Draws the adjustment patterns the version needs.
      Parameters:
      version - the QR code version
      matrix - the matrix to draw on