Class Encoder


  • public final class Encoder
    extends java.lang.Object
    • Field Detail

      • ALPHANUMERIC_TABLE

        private static final int[] ALPHANUMERIC_TABLE
      • DEFAULT_BYTE_MODE_ENCODING

        static final java.nio.charset.Charset DEFAULT_BYTE_MODE_ENCODING
    • Constructor Detail

      • Encoder

        private Encoder()
    • Method Detail

      • calculateMaskPenalty

        private static int calculateMaskPenalty​(ByteMatrix matrix)
      • encode

        public static QRCode encode​(java.lang.String content,
                                    ErrorCorrectionLevel ecLevel)
                             throws WriterException
        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
      • calculateBitsNeeded

        private static int calculateBitsNeeded​(Mode mode,
                                               BitArray headerBits,
                                               BitArray dataBits,
                                               Version version)
      • getAlphanumericCode

        static int getAlphanumericCode​(int code)
        Returns:
        the code point of the table used in alphanumeric mode or -1 if there is no corresponding code in the table.
      • chooseMode

        public static Mode chooseMode​(java.lang.String content)
      • chooseMode

        private static Mode chooseMode​(java.lang.String content,
                                       java.nio.charset.Charset 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.
      • isOnlyDoubleByteKanji

        static boolean isOnlyDoubleByteKanji​(java.lang.String content)
      • willFit

        static boolean willFit​(int numInputBits,
                               Version version,
                               ErrorCorrectionLevel ecLevel)
        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).
        Throws:
        WriterException
      • 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)
        Throws:
        WriterException
      • 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.
        Throws:
        WriterException
      • generateECBytes

        static byte[] generateECBytes​(byte[] dataBytes,
                                      int numEcBytesInBlock)
      • appendModeInfo

        static void appendModeInfo​(Mode mode,
                                   BitArray bits)
        Append mode info. On success, store the result in "bits".
      • appendBytes

        static void appendBytes​(java.lang.String content,
                                Mode mode,
                                BitArray bits,
                                java.nio.charset.Charset encoding)
                         throws WriterException
        Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".
        Throws:
        WriterException
      • appendNumericBytes

        static void appendNumericBytes​(java.lang.CharSequence content,
                                       BitArray bits)
      • append8BitBytes

        static void append8BitBytes​(java.lang.String content,
                                    BitArray bits,
                                    java.nio.charset.Charset encoding)