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

public final class Encoderextends Object
Encoder for QR codes.
  • Field Details

    • ALPHANUMERIC_TABLE

      private static final int[] ALPHANUMERIC_TABLE
      Alphanumeric code table from JISX0510:2004
    • DEFAULT_BYTE_MODE_ENCODING

      static final String DEFAULT_BYTE_MODE_ENCODING
      Default byte mode encoding
      See Also:
  • Constructor Details

    • Encoder

      private Encoder()
      Private constructor for utility class
  • Method Details

    • calculateMaskPenalty

      private static int calculateMaskPenalty(ByteMatrix matrix)
      Scores a mask pattern by applying the four penalty rules and summing them; see Table 21 of JISX0510:2004 (p.45) for the details.
      Parameters:
      matrix - the masked symbol
      Returns:
      the penalty, lower is better
    • encode

      public static QRCode encode(String content, ErrorCorrectionLevel ecLevel) throws WriterException
      Encode the given content into a QR Code.
      Parameters:
      content - text to encode
      ecLevel - error correction level to use
      Returns:
      QRCode representing the encoded QR code
      Throws:
      WriterException - if encoding can't succeed, because of for example invalid content or configuration
    • encode

      public static QRCode encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints) throws WriterException
      Encode content into a QR Code with hints for encoding options.
      Parameters:
      content - text to encode
      ecLevel - error correction level
      hints - optional hints (character set, version, mask pattern, GS1 format)
      Returns:
      QRCode representing the encoded QR code
      Throws:
      WriterException - if encoding fails
    • recommendVersion

      private static Version recommendVersion(ErrorCorrectionLevel ecLevel, Mode mode, BitArray headerBits, BitArray dataBits) throws WriterException
      Decides the smallest version of QR code that will contain all of the provided data.
      Parameters:
      ecLevel - the required error-correction level
      mode - the data encoding mode
      headerBits - the header bits counted against the version capacity
      dataBits - the encoded data bits that must fit
      Returns:
      the smallest version whose capacity holds header and data bits
      Throws:
      WriterException - if the data cannot fit in any version
    • calculateBitsNeeded

      private static int calculateBitsNeeded(Mode mode, BitArray headerBits, BitArray dataBits, Version version)
      Counts the bits needed to encode the content at a version.
      Parameters:
      mode - the encoding mode
      headerBits - the mode and length header
      dataBits - the encoded content
      version - the version to size for
      Returns:
      the number of bits
    • getAlphanumericCode

      static int getAlphanumericCode(int code)
      -1 if there is no corresponding code in the table.
      Parameters:
      code - the code point of the single character to look up in the alphanumeric table
      Returns:
      the code point of the table used in alphanumeric mode or
    • chooseMode

      public static Mode chooseMode(String content)
      Selects the mode that encodes the given text in the fewest bits.
      Parameters:
      content - the text to encode, examined character by character to decide the mode
      Returns:
      the mode that encodes content in the fewest bits
    • chooseMode

      private static Mode chooseMode(String content, String encoding)
      Choose the best mode by examining the content. Note that 'encoding' is used as a hint; if it is Shift_JIS, and the input is only double-byte Kanji, then we return Mode.KANJI.
      Parameters:
      content - the text to examine
      encoding - the character-set hint, may be null
      Returns:
      the most compact encoding mode for the content
    • isOnlyDoubleByteKanji

      private static boolean isOnlyDoubleByteKanji(String content)
      Checks whether the content is all double-byte Kanji.
      Parameters:
      content - the text to check
      Returns:
      true if Kanji encoding is the only option
    • chooseMaskPattern

      private static int chooseMaskPattern(BitArray bits, ErrorCorrectionLevel ecLevel, Version version, ByteMatrix matrix) throws WriterException
      Picks the mask pattern with the lowest penalty.
      Parameters:
      bits - the encoded data
      ecLevel - the level in use
      version - the symbol version
      matrix - the symbol being built
      Returns:
      the pattern number to use
      Throws:
      WriterException - if a mask cannot be applied
    • chooseVersion

      private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException
      Picks the smallest version that fits the data.
      Parameters:
      numInputBits - the bits to encode
      ecLevel - the level to fit within
      Returns:
      the version, null if none is large enough
      Throws:
      WriterException - if no version can hold the data
    • willFit

      private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel)
      Whether the data fits in the version at this error-correction level.
      Parameters:
      numInputBits - the number of data bits to fit
      version - the QR code version to test
      ecLevel - the error-correction level the code uses
      Returns:
      true if the number of input bits will fit in a code with the specified version and error correction level.
    • terminateBits

      static void terminateBits(int numDataBytes, BitArray bits) throws WriterException
      Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
      Parameters:
      numDataBytes - the data section size in bytes, which sets the capacity at 8 bits each
      bits - the bit buffer the terminator and padding bits are appended to
      Throws:
      WriterException - if bits already exceeds that capacity, or does not end at it
    • getNumDataBytesAndNumECBytesForBlockID

      static void getNumDataBytesAndNumECBytesForBlockID(int numTotalBytes, int numDataBytes, int numRSBlocks, int blockID, int[] numDataBytesInBlock, int[] numECBytesInBlock) throws WriterException
      Get number of data bytes and number of error correction bytes for block id "blockID". Store the result in "numDataBytesInBlock", and "numECBytesInBlock". See table 12 in 8.5.1 of JISX0510:2004 (p.30)
      Parameters:
      numTotalBytes - the total codeword count for the version and error correction level
      numDataBytes - the codeword count left after the error correction codewords
      numRSBlocks - the number of Reed-Solomon blocks the codewords are split into
      blockID - the zero-based index of the block to compute, below numRSBlocks
      numDataBytesInBlock - a one-element array receiving the data codeword count
      numECBytesInBlock - a one-element array receiving the error correction codeword count
      Throws:
      WriterException - if blockID is out of range, or the block geometry implied by the other counts is inconsistent
    • interleaveWithECBytes

      static BitArray interleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) throws WriterException
      Interleave "bits" with corresponding error correction bytes. On success, store the result in "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
      Parameters:
      bits - the data codewords, which must hold exactly numDataBytes
      numTotalBytes - the total codeword count including error correction
      numDataBytes - the data codeword count within numTotalBytes
      numRSBlocks - the number of Reed-Solomon blocks to split the data across
      Returns:
      a buffer holding the data and error correction codewords interleaved
      Throws:
      WriterException - if the supplied counts disagree, or the result is not numTotalBytes long
    • generateECBytes

      static byte[] generateECBytes(byte[] dataBytes, int numEcBytesInBlock)
      Generate the error correction bytes for one block of data, appending them to a copy of the block and running Reed-Solomon encoding over the result.
      Parameters:
      dataBytes - data to encode
      numEcBytesInBlock - number of parity bytes to produce; must be positive
      Returns:
      the trailing numEcBytesInBlock parity bytes computed over the data
    • appendModeInfo

      static void appendModeInfo(Mode mode, BitArray bits)
      Append mode info. On success, store the result in "bits".
      Parameters:
      mode - the encoding mode, whose four-bit indicator is written
      bits - the bit buffer the mode indicator is appended to
    • appendLengthInfo

      static void appendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits) throws WriterException
      Append length info. On success, store the result in "bits".
      Parameters:
      numLetters - the character count, which must fit the mode and version's count field
      version - the QR version, which fixes the width of the character count field
      mode - the encoding mode, which fixes that width as well
      bits - the bit buffer the character count is appended to
      Throws:
      WriterException - if numLetters does not fit in the character count field
    • appendBytes

      static void appendBytes(String content, Mode mode, BitArray bits, String encoding) throws WriterException
      Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".
      Parameters:
      content - the text whose characters are encoded, read in whatever units the mode needs
      mode - the encoding mode to apply, which selects one of the append methods below
      bits - the bit buffer the encoded characters are appended to
      encoding - the charset name used by the modes that fall back to bytes
      Throws:
      WriterException - if mode is not one of the four encodable modes, or the content cannot be represented in it
    • appendNumericBytes

      static void appendNumericBytes(CharSequence content, BitArray bits)
      Append numeric mode bytes
      Parameters:
      content - the digits to encode, packed three at a time wherever a run allows it
      bits - the bit buffer the packed digits are appended to
    • appendAlphanumericBytes

      static void appendAlphanumericBytes(CharSequence content, BitArray bits) throws WriterException
      Append alphanumeric mode bytes
      Parameters:
      content - the alphanumeric characters to encode, packed in pairs where a run allows
      bits - the bit buffer the packed characters are appended to
      Throws:
      WriterException - if any character has no alphanumeric table entry
    • append8BitBytes

      static void append8BitBytes(String content, BitArray bits, String encoding) throws WriterException
      Append 8-bit byte mode bytes
      Parameters:
      content - the text to encode, first converted using encoding
      bits - the bit buffer the resulting bytes are appended to
      encoding - the charset name to convert the text with
      Throws:
      WriterException - if the JVM does not support encoding
    • appendKanjiBytes

      static void appendKanjiBytes(String content, BitArray bits) throws WriterException
      Append Kanji mode bytes
      Parameters:
      content - the Kanji text to encode, read as Shift_JIS
      bits - the bit buffer the encoded characters are appended to
      Throws:
      WriterException - if the text does not convert to an even number of Shift_JIS bytes, or a byte pair is outside the two ranges Kanji mode defines
    • appendECI

      private static void appendECI(CharacterSetECI eci, BitArray bits)
      Appends the ECI header naming the character set.
      Parameters:
      eci - the character set to name
      bits - the bits to append to