java.lang.Object
com.google.zxing.qrcode.encoder.Encoder
Encoder for QR codes.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int[]Alphanumeric code table from JISX0510:2004(package private) static final StringDefault byte mode encodingConstructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescription(package private) static voidappend8BitBytes(String content, BitArray bits, String encoding) Append 8-bit byte mode bytes(package private) static voidappendAlphanumericBytes(CharSequence content, BitArray bits) Append alphanumeric mode bytes(package private) static voidappendBytes(String content, Mode mode, BitArray bits, String encoding) Append "bytes" in "mode" mode (encoding) into "bits".private static voidappendECI(CharacterSetECI eci, BitArray bits) Appends the ECI header naming the character set.(package private) static voidappendKanjiBytes(String content, BitArray bits) Append Kanji mode bytes(package private) static voidappendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits) Append length info.(package private) static voidappendModeInfo(Mode mode, BitArray bits) Append mode info.(package private) static voidappendNumericBytes(CharSequence content, BitArray bits) Append numeric mode bytesprivate static intcalculateBitsNeeded(Mode mode, BitArray headerBits, BitArray dataBits, Version version) Counts the bits needed to encode the content at a version.private static intcalculateMaskPenalty(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.private static intchooseMaskPattern(BitArray bits, ErrorCorrectionLevel ecLevel, Version version, ByteMatrix matrix) Picks the mask pattern with the lowest penalty.static ModechooseMode(String content) Selects the mode that encodes the given text in the fewest bits.private static ModechooseMode(String content, String encoding) Choose the best mode by examining the content.private static VersionchooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) Picks the smallest version that fits the data.static QRCodeencode(String content, ErrorCorrectionLevel ecLevel) Encode the given content into a QR Code.static QRCodeencode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints) Encode content into a QR Code with hints for encoding options.(package private) 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.(package private) static intgetAlphanumericCode(int code) -1 if there is no corresponding code in the table.(package private) static voidgetNumDataBytesAndNumECBytesForBlockID(int numTotalBytes, int numDataBytes, int numRSBlocks, int blockID, int[] numDataBytesInBlock, int[] numECBytesInBlock) Get number of data bytes and number of error correction bytes for block id "blockID".(package private) static BitArrayinterleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) Interleave "bits" with corresponding error correction bytes.private static booleanisOnlyDoubleByteKanji(String content) Checks whether the content is all double-byte Kanji.private static VersionrecommendVersion(ErrorCorrectionLevel ecLevel, Mode mode, BitArray headerBits, BitArray dataBits) Decides the smallest version of QR code that will contain all of the provided data.(package private) static voidterminateBits(int numDataBytes, BitArray bits) Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).private static booleanwillFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) Whether the data fits in the version at this error-correction level.
Field Details
ALPHANUMERIC_TABLE
private static final int[] ALPHANUMERIC_TABLEAlphanumeric code table from JISX0510:2004DEFAULT_BYTE_MODE_ENCODING
Default byte mode encoding- See Also:
Constructor Details
Encoder
private Encoder()Private constructor for utility class
Method Details
calculateMaskPenalty
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
Encode the given content into a QR Code.- Parameters:
content- text to encodeecLevel- error correction level to use- Returns:
QRCoderepresenting 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 encodeecLevel- error correction levelhints- 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 levelmode- the data encoding modeheaderBits- the header bits counted against the version capacitydataBits- 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 modeheaderBits- the mode and length headerdataBits- the encoded contentversion- 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
chooseMode
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 returnMode.KANJI.- Parameters:
content- the text to examineencoding- the character-set hint, may be null- Returns:
- the most compact encoding mode for the content
isOnlyDoubleByteKanji
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 dataecLevel- the level in useversion- the symbol versionmatrix- 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 encodeecLevel- the level to fit within- Returns:
- the version, null if none is large enough
- Throws:
WriterException- if no version can hold the data
willFit
Whether the data fits in the version at this error-correction level.- Parameters:
numInputBits- the number of data bits to fitversion- the QR code version to testecLevel- 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
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 eachbits- the bit buffer the terminator and padding bits are appended to- Throws:
WriterException- ifbitsalready 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 levelnumDataBytes- the codeword count left after the error correction codewordsnumRSBlocks- the number of Reed-Solomon blocks the codewords are split intoblockID- the zero-based index of the block to compute, belownumRSBlocksnumDataBytesInBlock- a one-element array receiving the data codeword countnumECBytesInBlock- a one-element array receiving the error correction codeword count- Throws:
WriterException- ifblockIDis 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 exactlynumDataBytesnumTotalBytes- the total codeword count including error correctionnumDataBytes- the data codeword count withinnumTotalBytesnumRSBlocks- 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 notnumTotalByteslong
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 encodenumEcBytesInBlock- number of parity bytes to produce; must be positive- Returns:
- the trailing
numEcBytesInBlockparity bytes computed over the data
appendModeInfo
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 fieldversion- the QR version, which fixes the width of the character count fieldmode- the encoding mode, which fixes that width as wellbits- the bit buffer the character count is appended to- Throws:
WriterException- ifnumLettersdoes 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 needsmode- the encoding mode to apply, which selects one of the append methods belowbits- the bit buffer the encoded characters are appended toencoding- the charset name used by the modes that fall back to bytes- Throws:
WriterException- ifmodeis not one of the four encodable modes, or the content cannot be represented in it
appendNumericBytes
Append numeric mode bytes- Parameters:
content- the digits to encode, packed three at a time wherever a run allows itbits- the bit buffer the packed digits are appended to
appendAlphanumericBytes
Append alphanumeric mode bytes- Parameters:
content- the alphanumeric characters to encode, packed in pairs where a run allowsbits- the bit buffer the packed characters are appended to- Throws:
WriterException- if any character has no alphanumeric table entry
append8BitBytes
Append 8-bit byte mode bytes- Parameters:
content- the text to encode, first converted usingencodingbits- the bit buffer the resulting bytes are appended toencoding- the charset name to convert the text with- Throws:
WriterException- if the JVM does not supportencoding
appendKanjiBytes
Append Kanji mode bytes- Parameters:
content- the Kanji text to encode, read as Shift_JISbits- 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
Appends the ECI header naming the character set.- Parameters:
eci- the character set to namebits- the bits to append to