Minor change to enable manual selection of the QR code mask pattern (#1176)

* Minor change to enable manual selection of the QR code mask pattern via encoding hints. Manual selection of the mask pattern greatly improves performance.

* fixed style
This commit is contained in:
anrcode 2019-06-13 14:02:10 +02:00 committed by Sean Owen
parent 723b65fe3d
commit 6075d5b2f6
2 changed files with 21 additions and 3 deletions

View file

@ -102,6 +102,14 @@ public enum EncodeHintType {
*/
QR_VERSION,
/**
* Specifies the QR code mask pattern to be used. Allowed values are
* 0..QRCode.NUM_MASK_PATTERNS-1. By default the code will automatically select
* the optimal mask pattern.
* * (Type {@link Integer}, or {@link String} representation of the integer value).
*/
QR_MASK_PATTERN,
/**
* Specifies whether the data should be encoded to the GS1 standard (type {@link Boolean}, or "true" or "false"
* {@link String } value).

View file

@ -155,7 +155,17 @@ public final class Encoder {
// Choose the mask pattern and set to "qrCode".
int dimension = version.getDimensionForVersion();
ByteMatrix matrix = new ByteMatrix(dimension, dimension);
int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
// Enable manual selection of the pattern to be used via hint
int maskPattern = -1;
if (hints != null && hints.containsKey(EncodeHintType.QR_MASK_PATTERN)) {
int hintMaskPattern = Integer.parseInt(hints.get(EncodeHintType.QR_MASK_PATTERN).toString());
maskPattern = QRCode.isValidMaskPattern(hintMaskPattern) ? hintMaskPattern : -1;
}
if (maskPattern == -1) {
maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
}
qrCode.setMaskPattern(maskPattern);
// Build the matrix and set it to "qrCode".
@ -285,7 +295,7 @@ public final class Encoder {
}
throw new WriterException("Data too big");
}
/**
* @return true if the number of input bits will fit in a code with the specified version and
* error correction level.
@ -317,7 +327,7 @@ public final class Encoder {
}
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
// If the last byte isn't 8-bit aligned, we'll add padding bits.
int numBitsInLastByte = bits.getSize() & 0x07;
int numBitsInLastByte = bits.getSize() & 0x07;
if (numBitsInLastByte > 0) {
for (int i = numBitsInLastByte; i < 8; i++) {
bits.appendBit(false);