|
|
@ -52,6 +52,12 @@ import java.util.Objects;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public final class QrCode {
|
|
|
|
public final class QrCode {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int FINDER_SIZE = 3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int TIMING_COORDINATE = 6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Static factory functions (high level) ----*/
|
|
|
|
/*---- Static factory functions (high level) ----*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -128,8 +134,8 @@ public final class QrCode {
|
|
|
|
* between modes (such as alphanumeric and byte) to encode text in less space.
|
|
|
|
* between modes (such as alphanumeric and byte) to encode text in less space.
|
|
|
|
* This is a mid-level API; the high-level API is {@link #encodeText(String,Ecc)}
|
|
|
|
* This is a mid-level API; the high-level API is {@link #encodeText(String,Ecc)}
|
|
|
|
* and {@link #encodeBinary(byte[],Ecc)}.</p>
|
|
|
|
* and {@link #encodeBinary(byte[],Ecc)}.</p>
|
|
|
|
* @param segs the segments to encode
|
|
|
|
* @param segments the segments to encode
|
|
|
|
* @param ecl the error correction level to use (not {@code null}) (boostable)
|
|
|
|
* @param errorCorrectionLevel the error correction level to use (not {@code null}) (boostable)
|
|
|
|
* @param minVersion the minimum allowed version of the QR Code (at least 1)
|
|
|
|
* @param minVersion the minimum allowed version of the QR Code (at least 1)
|
|
|
|
* @param maxVersion the maximum allowed version of the QR Code (at most 40)
|
|
|
|
* @param maxVersion the maximum allowed version of the QR Code (at most 40)
|
|
|
|
* @param mask the mask number to use (between 0 and 7 (inclusive)), or −1 for automatic mask
|
|
|
|
* @param mask the mask number to use (between 0 and 7 (inclusive)), or −1 for automatic mask
|
|
|
@ -141,64 +147,87 @@ public final class QrCode {
|
|
|
|
* @throws DataTooLongException if the segments fail to fit in
|
|
|
|
* @throws DataTooLongException if the segments fail to fit in
|
|
|
|
* the maxVersion QR Code at the ECL, which means they are too long
|
|
|
|
* the maxVersion QR Code at the ECL, which means they are too long
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersion, int maxVersion, int mask, boolean boostEcl) {
|
|
|
|
public static QrCode encodeSegments(List<QrSegment> segments, Ecc errorCorrectionLevel, int minVersion, int maxVersion, int mask, boolean boostEcl) {
|
|
|
|
Objects.requireNonNull(segs);
|
|
|
|
Objects.requireNonNull(segments);
|
|
|
|
Objects.requireNonNull(ecl);
|
|
|
|
Objects.requireNonNull(errorCorrectionLevel);
|
|
|
|
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)
|
|
|
|
final boolean isVersionInRange = MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION;
|
|
|
|
|
|
|
|
final boolean isMaskOutOfRange = mask < -1 || mask > 7;
|
|
|
|
|
|
|
|
if (!isVersionInRange || isMaskOutOfRange)
|
|
|
|
throw new IllegalArgumentException("Invalid value");
|
|
|
|
throw new IllegalArgumentException("Invalid value");
|
|
|
|
|
|
|
|
|
|
|
|
// Find the minimal version number to use
|
|
|
|
|
|
|
|
int version, dataUsedBits;
|
|
|
|
|
|
|
|
for (version = minVersion; ; version++) {
|
|
|
|
|
|
|
|
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
|
|
|
|
|
|
|
dataUsedBits = QrSegment.getTotalBits(segs, version);
|
|
|
|
|
|
|
|
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
|
|
|
|
|
|
|
|
break; // This version number is found to be suitable
|
|
|
|
|
|
|
|
if (version >= maxVersion) { // All versions in the range could not fit the given data
|
|
|
|
|
|
|
|
String msg = "Segment too long";
|
|
|
|
|
|
|
|
if (dataUsedBits != -1)
|
|
|
|
|
|
|
|
msg = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits);
|
|
|
|
|
|
|
|
throw new DataTooLongException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert dataUsedBits != -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Increase the error correction level while the data still fits in the current version number
|
|
|
|
int version = findMinimalVersion(segments, errorCorrectionLevel, minVersion, maxVersion);
|
|
|
|
for (Ecc newEcl : Ecc.values()) { // From low to high
|
|
|
|
|
|
|
|
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
|
|
|
|
|
|
|
|
ecl = newEcl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Concatenate all segments to create the data bit string
|
|
|
|
int dataUsedBits = QrSegment.getTotalBits(segments, version);
|
|
|
|
BitBuffer bb = new BitBuffer();
|
|
|
|
|
|
|
|
for (QrSegment seg : segs) {
|
|
|
|
errorCorrectionLevel = findMaximalErrorCorrectionLevel(errorCorrectionLevel, boostEcl, version, dataUsedBits);
|
|
|
|
bb.appendBits(seg.mode.modeBits, 4);
|
|
|
|
|
|
|
|
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
|
|
|
|
|
|
|
|
bb.appendData(seg.data);
|
|
|
|
BitBuffer bitBuffer = segmentsToBitBuffer(segments, version);
|
|
|
|
}
|
|
|
|
assert bitBuffer.bitLength() == dataUsedBits;
|
|
|
|
assert bb.bitLength() == dataUsedBits;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8;
|
|
|
|
|
|
|
|
assert bitBuffer.bitLength() <= dataCapacityBits;
|
|
|
|
|
|
|
|
bitBuffer.addTerminator(dataCapacityBits);
|
|
|
|
|
|
|
|
|
|
|
|
// Add terminator and pad up to a byte if applicable
|
|
|
|
|
|
|
|
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
|
|
|
|
|
|
|
assert bb.bitLength() <= dataCapacityBits;
|
|
|
|
|
|
|
|
bb.appendBits(0, Math.min(4, dataCapacityBits - bb.bitLength()));
|
|
|
|
|
|
|
|
bb.appendBits(0, (8 - bb.bitLength() % 8) % 8);
|
|
|
|
|
|
|
|
assert bb.bitLength() % 8 == 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pad with alternating bytes until data capacity is reached
|
|
|
|
bitBuffer.addPad(dataCapacityBits);
|
|
|
|
for (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
|
|
|
|
|
|
|
|
bb.appendBits(padByte, 8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pack bits into bytes in big endian
|
|
|
|
|
|
|
|
byte[] dataCodewords = new byte[bb.bitLength() / 8];
|
|
|
|
byte[] dataCodewords = bitBuffer.toCodewords();
|
|
|
|
for (int i = 0; i < bb.bitLength(); i++)
|
|
|
|
|
|
|
|
dataCodewords[i >>> 3] |= bb.getBit(i) << (7 - (i & 7));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the QR Code object
|
|
|
|
// Create the QR Code object
|
|
|
|
return new QrCode(version, ecl, dataCodewords, mask);
|
|
|
|
return new QrCode(version, errorCorrectionLevel, dataCodewords, mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Private helper methods for encodeSegments ----*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Concatenate all segments to create the data bit string
|
|
|
|
|
|
|
|
private static BitBuffer segmentsToBitBuffer(List<QrSegment> segments, int version) {
|
|
|
|
|
|
|
|
BitBuffer bitBuffer = new BitBuffer();
|
|
|
|
|
|
|
|
for (QrSegment segment : segments) {
|
|
|
|
|
|
|
|
bitBuffer.appendBits(segment.mode.modeBits, 4);
|
|
|
|
|
|
|
|
bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version));
|
|
|
|
|
|
|
|
bitBuffer.appendData(segment.data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitBuffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Increase the error correction level while the data still fits in the current version number
|
|
|
|
|
|
|
|
private static Ecc findMaximalErrorCorrectionLevel(Ecc errorCorrectionLevel, boolean boostEcl, int version,
|
|
|
|
|
|
|
|
int dataUsedBits) {
|
|
|
|
|
|
|
|
for (Ecc newEcl : Ecc.values()) { // From low to high
|
|
|
|
|
|
|
|
final boolean canIncreaseErrorCorrectionLevel = dataUsedBits <= getNumDataCodewords(version, newEcl) * 8;
|
|
|
|
|
|
|
|
if (boostEcl && canIncreaseErrorCorrectionLevel)
|
|
|
|
|
|
|
|
errorCorrectionLevel = newEcl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorCorrectionLevel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Returns the minimal version number to use
|
|
|
|
|
|
|
|
private static int findMinimalVersion(List<QrSegment> segments, Ecc errorCorrectionLevel, int minVersion,
|
|
|
|
|
|
|
|
int maxVersion) {
|
|
|
|
|
|
|
|
int version, dataUsedBits;
|
|
|
|
|
|
|
|
for (version = minVersion; ; version++) {
|
|
|
|
|
|
|
|
int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; // Number of data bits available
|
|
|
|
|
|
|
|
dataUsedBits = QrSegment.getTotalBits(segments, version);
|
|
|
|
|
|
|
|
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
|
|
|
|
|
|
|
|
break; // This version number is found to be suitable
|
|
|
|
|
|
|
|
if (version >= maxVersion) { // All versions in the range could not fit the given data
|
|
|
|
|
|
|
|
String message = "Segment too long";
|
|
|
|
|
|
|
|
if (dataUsedBits != -1)
|
|
|
|
|
|
|
|
message = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits);
|
|
|
|
|
|
|
|
throw new DataTooLongException(message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert dataUsedBits != -1;
|
|
|
|
|
|
|
|
return version;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Instance fields ----*/
|
|
|
|
/*---- Instance fields ----*/
|
|
|
|
|
|
|
|
|
|
|
@ -238,32 +267,32 @@ public final class QrCode {
|
|
|
|
* error correction level, data codeword bytes, and mask number.
|
|
|
|
* error correction level, data codeword bytes, and mask number.
|
|
|
|
* <p>This is a low-level API that most users should not use directly. A mid-level
|
|
|
|
* <p>This is a low-level API that most users should not use directly. A mid-level
|
|
|
|
* API is the {@link #encodeSegments(List,Ecc,int,int,int,boolean)} function.</p>
|
|
|
|
* API is the {@link #encodeSegments(List,Ecc,int,int,int,boolean)} function.</p>
|
|
|
|
* @param ver the version number to use, which must be in the range 1 to 40 (inclusive)
|
|
|
|
* @param version the version number to use, which must be in the range 1 to 40 (inclusive)
|
|
|
|
* @param ecl the error correction level to use
|
|
|
|
* @param errorCorrectionLevel the error correction level to use
|
|
|
|
* @param dataCodewords the bytes representing segments to encode (without ECC)
|
|
|
|
* @param dataCodewords the bytes representing segments to encode (without ECC)
|
|
|
|
* @param msk the mask pattern to use, which is either −1 for automatic choice or from 0 to 7 for fixed choice
|
|
|
|
* @param mask the mask pattern to use, which is either −1 for automatic choice or from 0 to 7 for fixed choice
|
|
|
|
* @throws NullPointerException if the byte array or error correction level is {@code null}
|
|
|
|
* @throws NullPointerException if the byte array or error correction level is {@code null}
|
|
|
|
* @throws IllegalArgumentException if the version or mask value is out of range,
|
|
|
|
* @throws IllegalArgumentException if the version or mask value is out of range,
|
|
|
|
* or if the data is the wrong length for the specified version and error correction level
|
|
|
|
* or if the data is the wrong length for the specified version and error correction level
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int msk) {
|
|
|
|
public QrCode(int version, Ecc errorCorrectionLevel, byte[] dataCodewords, int mask) {
|
|
|
|
// Check arguments and initialize fields
|
|
|
|
// Check arguments and initialize fields
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
if (version < MIN_VERSION || version > MAX_VERSION)
|
|
|
|
throw new IllegalArgumentException("Version value out of range");
|
|
|
|
throw new IllegalArgumentException("Version value out of range");
|
|
|
|
if (msk < -1 || msk > 7)
|
|
|
|
if (mask < -1 || mask > 7)
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
version = ver;
|
|
|
|
this.version = version;
|
|
|
|
size = ver * 4 + 17;
|
|
|
|
this.size = version * 4 + 17;
|
|
|
|
errorCorrectionLevel = Objects.requireNonNull(ecl);
|
|
|
|
this.errorCorrectionLevel = Objects.requireNonNull(errorCorrectionLevel);
|
|
|
|
Objects.requireNonNull(dataCodewords);
|
|
|
|
Objects.requireNonNull(dataCodewords);
|
|
|
|
modules = new boolean[size][size]; // Initially all white
|
|
|
|
this.modules = new boolean[size][size]; // Initially all white
|
|
|
|
isFunction = new boolean[size][size];
|
|
|
|
this.isFunction = new boolean[size][size];
|
|
|
|
|
|
|
|
|
|
|
|
// Compute ECC, draw modules, do masking
|
|
|
|
// Compute ECC, draw modules, do masking
|
|
|
|
drawFunctionPatterns();
|
|
|
|
drawFunctionPatterns();
|
|
|
|
byte[] allCodewords = addEccAndInterleave(dataCodewords);
|
|
|
|
byte[] allCodewords = addEccAndInterleave(dataCodewords);
|
|
|
|
drawCodewords(allCodewords);
|
|
|
|
drawCodewords(allCodewords);
|
|
|
|
this.mask = handleConstructorMasking(msk);
|
|
|
|
this.mask = handleConstructorMasking(mask);
|
|
|
|
isFunction = null;
|
|
|
|
isFunction = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -354,29 +383,37 @@ public final class QrCode {
|
|
|
|
private void drawFunctionPatterns() {
|
|
|
|
private void drawFunctionPatterns() {
|
|
|
|
// Draw horizontal and vertical timing patterns
|
|
|
|
// Draw horizontal and vertical timing patterns
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
setFunctionModule(6, i, i % 2 == 0);
|
|
|
|
setFunctionModule(TIMING_COORDINATE, i, i % 2 == 0);
|
|
|
|
setFunctionModule(i, 6, i % 2 == 0);
|
|
|
|
setFunctionModule(i, TIMING_COORDINATE, i % 2 == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
|
|
|
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
|
|
|
drawFinderPattern(3, 3);
|
|
|
|
drawFinderPattern(FINDER_SIZE, FINDER_SIZE);
|
|
|
|
drawFinderPattern(size - 4, 3);
|
|
|
|
drawFinderPattern(size - 1 - FINDER_SIZE, FINDER_SIZE);
|
|
|
|
drawFinderPattern(3, size - 4);
|
|
|
|
drawFinderPattern(FINDER_SIZE, size - 1 - FINDER_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
// Draw numerous alignment patterns
|
|
|
|
drawAlignmentsPatterns();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw configuration data
|
|
|
|
|
|
|
|
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
|
|
|
|
|
|
|
drawVersion();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw numerous alignment patterns
|
|
|
|
|
|
|
|
private void drawAlignmentsPatterns() {
|
|
|
|
int[] alignPatPos = getAlignmentPatternPositions();
|
|
|
|
int[] alignPatPos = getAlignmentPatternPositions();
|
|
|
|
int numAlign = alignPatPos.length;
|
|
|
|
int numAlign = alignPatPos.length;
|
|
|
|
for (int i = 0; i < numAlign; i++) {
|
|
|
|
for (int i = 0; i < numAlign; i++) {
|
|
|
|
for (int j = 0; j < numAlign; j++) {
|
|
|
|
for (int j = 0; j < numAlign; j++) {
|
|
|
|
// Don't draw on the three finder corners
|
|
|
|
final boolean isLeftTop = i == 0 && j == 0;
|
|
|
|
if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))
|
|
|
|
final boolean isLeftBottom = i == 0 && j == numAlign - 1;
|
|
|
|
|
|
|
|
final boolean isRightTop = i == numAlign - 1 && j == 0;
|
|
|
|
|
|
|
|
final boolean onThreeFinderCorners = isLeftTop || isLeftBottom || isRightTop;
|
|
|
|
|
|
|
|
if (!onThreeFinderCorners)
|
|
|
|
drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
|
|
|
drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw configuration data
|
|
|
|
|
|
|
|
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
|
|
|
|
|
|
|
drawVersion();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -384,7 +421,7 @@ public final class QrCode {
|
|
|
|
// based on the given mask and this object's error correction level field.
|
|
|
|
// based on the given mask and this object's error correction level field.
|
|
|
|
private void drawFormatBits(int msk) {
|
|
|
|
private void drawFormatBits(int msk) {
|
|
|
|
// Calculate error correction code and pack bits
|
|
|
|
// Calculate error correction code and pack bits
|
|
|
|
int data = errorCorrectionLevel.formatBits << 3 | msk; // errCorrLvl is uint2, mask is uint3
|
|
|
|
int data = errorCorrectionLevel.getFormatBits() << 3 | msk; // errCorrLvl is uint2, mask is uint3
|
|
|
|
int rem = data;
|
|
|
|
int rem = data;
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
|
|
rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
|
@ -475,8 +512,8 @@ public final class QrCode {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate parameter numbers
|
|
|
|
// Calculate parameter numbers
|
|
|
|
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version];
|
|
|
|
int numBlocks = errorCorrectionLevel.getNumberOfBlock(version);
|
|
|
|
int blockEccLen = ECC_CODEWORDS_PER_BLOCK [errorCorrectionLevel.ordinal()][version];
|
|
|
|
int blockEccLen = errorCorrectionLevel.getBlockLength(version);
|
|
|
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
|
|
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
|
|
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
|
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
|
|
int shortBlockLen = rawCodewords / numBlocks;
|
|
|
|
int shortBlockLen = rawCodewords / numBlocks;
|
|
|
@ -543,23 +580,20 @@ public final class QrCode {
|
|
|
|
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
|
|
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
|
|
// the same mask value a second time will undo the mask. A final well-formed
|
|
|
|
// the same mask value a second time will undo the mask. A final well-formed
|
|
|
|
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
|
|
|
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
|
|
|
|
|
|
|
|
|
|
|
private void applyMask(int msk) {
|
|
|
|
private void applyMask(int msk) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (msk < 0 || msk > 7)
|
|
|
|
if (msk < 0 || msk > 7)
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
boolean invert;
|
|
|
|
boolean invert;
|
|
|
|
switch (msk) {
|
|
|
|
|
|
|
|
case 0: invert = (x + y) % 2 == 0; break;
|
|
|
|
Command mskCommand = MskCommandFactory.getCommand(msk);
|
|
|
|
case 1: invert = y % 2 == 0; break;
|
|
|
|
Button button = new Button(mskCommand);
|
|
|
|
case 2: invert = x % 3 == 0; break;
|
|
|
|
invert = button.pressed(y, x, msk);
|
|
|
|
case 3: invert = (x + y) % 3 == 0; break;
|
|
|
|
|
|
|
|
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
|
|
|
|
|
|
|
|
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
|
|
|
|
|
|
|
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
|
|
|
|
|
|
|
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
|
|
|
|
|
|
|
default: throw new AssertionError();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
modules[y][x] ^= invert & !isFunction[y][x];
|
|
|
|
modules[y][x] ^= invert & !isFunction[y][x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -569,51 +603,69 @@ public final class QrCode {
|
|
|
|
// A messy helper function for the constructor. This QR Code must be in an unmasked state when this
|
|
|
|
// A messy helper function for the constructor. This QR Code must be in an unmasked state when this
|
|
|
|
// method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
|
|
|
|
// method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
|
|
|
|
// This method applies and returns the actual mask chosen, from 0 to 7.
|
|
|
|
// This method applies and returns the actual mask chosen, from 0 to 7.
|
|
|
|
private int handleConstructorMasking(int msk) {
|
|
|
|
private int handleConstructorMasking(int mask) {
|
|
|
|
if (msk == -1) { // Automatically choose best mask
|
|
|
|
if (mask == -1) {
|
|
|
|
int minPenalty = Integer.MAX_VALUE;
|
|
|
|
mask = findBestMask();
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
}
|
|
|
|
applyMask(i);
|
|
|
|
assert 0 <= mask && mask <= 7;
|
|
|
|
drawFormatBits(i);
|
|
|
|
applyMask(mask); // Apply the final choice of mask
|
|
|
|
int penalty = getPenaltyScore();
|
|
|
|
drawFormatBits(mask); // Overwrite old format bits
|
|
|
|
if (penalty < minPenalty) {
|
|
|
|
return mask; // The caller shall assign this value to the final-declared field
|
|
|
|
msk = i;
|
|
|
|
}
|
|
|
|
minPenalty = penalty;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
applyMask(i); // Undoes the mask due to XOR
|
|
|
|
// Automatically choose best mask
|
|
|
|
|
|
|
|
private int findBestMask() {
|
|
|
|
|
|
|
|
int mask = -1;
|
|
|
|
|
|
|
|
int minPenalty = Integer.MAX_VALUE;
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
|
|
|
|
applyMask(i);
|
|
|
|
|
|
|
|
drawFormatBits(i);
|
|
|
|
|
|
|
|
int penalty = getPenaltyScore();
|
|
|
|
|
|
|
|
if (penalty < minPenalty) {
|
|
|
|
|
|
|
|
mask = i;
|
|
|
|
|
|
|
|
minPenalty = penalty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyMask(i); // Undoes the mask due to XOR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert 0 <= msk && msk <= 7;
|
|
|
|
return mask;
|
|
|
|
applyMask(msk); // Apply the final choice of mask
|
|
|
|
|
|
|
|
drawFormatBits(msk); // Overwrite old format bits
|
|
|
|
|
|
|
|
return msk; // The caller shall assign this value to the final-declared field
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int havingSameColor(int run, boolean runColor, int[] runHistory, int result, int y, int x) {
|
|
|
|
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
|
|
|
|
run++;
|
|
|
|
|
|
|
|
if (run == 5)
|
|
|
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
|
|
|
else if (run > 5)
|
|
|
|
|
|
|
|
result++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
finderPenaltyAddHistory(run, runHistory);
|
|
|
|
|
|
|
|
if (!runColor)
|
|
|
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
|
|
|
run = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
|
|
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
|
|
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
|
|
|
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
|
|
|
|
|
|
|
|
|
|
|
private int getPenaltyScore() {
|
|
|
|
private int getPenaltyScore() {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Adjacent modules in row having same color, and finder-like patterns
|
|
|
|
// Adjacent modules in row having same color, and finder-like patterns
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
boolean runColor = false;
|
|
|
|
boolean runColor = false;
|
|
|
|
int runX = 0;
|
|
|
|
int runX = 0;
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
for (int x = 0; x < size; x++) {
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
result += havingSameColor(runX, runColor, runHistory, result, y, x);
|
|
|
|
runX++;
|
|
|
|
|
|
|
|
if (runX == 5)
|
|
|
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
|
|
|
else if (runX > 5)
|
|
|
|
|
|
|
|
result++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
finderPenaltyAddHistory(runX, runHistory);
|
|
|
|
|
|
|
|
if (!runColor)
|
|
|
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
|
|
|
runX = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -623,34 +675,14 @@ public final class QrCode {
|
|
|
|
int runY = 0;
|
|
|
|
int runY = 0;
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
int[] runHistory = new int[7];
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
if (modules[y][x] == runColor) {
|
|
|
|
result += havingSameColor(runY, runColor, runHistory, result, y, x);
|
|
|
|
runY++;
|
|
|
|
|
|
|
|
if (runY == 5)
|
|
|
|
|
|
|
|
result += PENALTY_N1;
|
|
|
|
|
|
|
|
else if (runY > 5)
|
|
|
|
|
|
|
|
result++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
finderPenaltyAddHistory(runY, runHistory);
|
|
|
|
|
|
|
|
if (!runColor)
|
|
|
|
|
|
|
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
|
|
|
|
|
|
runColor = modules[y][x];
|
|
|
|
|
|
|
|
runY = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
|
|
|
|
result += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2*2 blocks of modules having same color
|
|
|
|
result += twobytwoHavingSameColor(modules);
|
|
|
|
for (int y = 0; y < size - 1; y++) {
|
|
|
|
|
|
|
|
for (int x = 0; x < size - 1; x++) {
|
|
|
|
|
|
|
|
boolean color = modules[y][x];
|
|
|
|
|
|
|
|
if ( color == modules[y][x + 1] &&
|
|
|
|
|
|
|
|
color == modules[y + 1][x] &&
|
|
|
|
|
|
|
|
color == modules[y + 1][x + 1])
|
|
|
|
|
|
|
|
result += PENALTY_N2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Balance of black and white modules
|
|
|
|
// Balance of black and white modules
|
|
|
|
int black = 0;
|
|
|
|
int black = 0;
|
|
|
|
for (boolean[] row : modules) {
|
|
|
|
for (boolean[] row : modules) {
|
|
|
@ -665,11 +697,24 @@ public final class QrCode {
|
|
|
|
result += k * PENALTY_N4;
|
|
|
|
result += k * PENALTY_N4;
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int twobytwoHavingSameColor(boolean[][] modules) {
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// 2*2 blocks of modules having same color.
|
|
|
|
|
|
|
|
for (int y = 0; y < size - 1; y++) {
|
|
|
|
|
|
|
|
for (int x = 0; x < size - 1; x++) {
|
|
|
|
|
|
|
|
boolean color = modules[y][x];
|
|
|
|
|
|
|
|
if ( color == modules[y][x + 1] &&
|
|
|
|
|
|
|
|
color == modules[y + 1][x] &&
|
|
|
|
|
|
|
|
color == modules[y + 1][x + 1])
|
|
|
|
|
|
|
|
result += PENALTY_N2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Private helper functions ----*/
|
|
|
|
/*---- Private helper functions ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// Returns an ascending list of positions of alignment patterns for this version number.
|
|
|
|
// Returns an ascending list of positions of alignment patterns for this version number.
|
|
|
|
// Each position is in the range [0,177), and are used on both the x and y axes.
|
|
|
|
// Each position is in the range [0,177), and are used on both the x and y axes.
|
|
|
|
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
|
|
|
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
|
|
@ -690,15 +735,22 @@ public final class QrCode {
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
|
|
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
|
|
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
|
|
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
|
|
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
|
|
|
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
|
|
|
private static int getNumRawDataModules(int ver) {
|
|
|
|
private static int getNumRawDataModules(int ver) {
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
throw new IllegalArgumentException("Version number out of range");
|
|
|
|
throw new IllegalArgumentException("Version number out of range");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int result = calculateNumOfModules(ver);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert 208 <= result && result <= 29648;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static int calculateNumOfModules(int ver) {
|
|
|
|
int size = ver * 4 + 17;
|
|
|
|
int size = ver * 4 + 17;
|
|
|
|
int result = size * size; // Number of modules in the whole QR Code square
|
|
|
|
int result = size * size; // Number of modules in the whole QR Code square
|
|
|
|
result -= 8 * 8 * 3; // Subtract the three finders with separators
|
|
|
|
result -= 8 * 8 * 3; // Subtract the three finders with separators
|
|
|
@ -713,11 +765,9 @@ public final class QrCode {
|
|
|
|
if (ver >= 7)
|
|
|
|
if (ver >= 7)
|
|
|
|
result -= 6 * 3 * 2; // Subtract version information
|
|
|
|
result -= 6 * 3 * 2; // Subtract version information
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert 208 <= result && result <= 29648;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
|
|
|
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
|
|
|
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
|
|
|
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
|
|
|
private static byte[] reedSolomonComputeDivisor(int degree) {
|
|
|
|
private static byte[] reedSolomonComputeDivisor(int degree) {
|
|
|
@ -727,7 +777,7 @@ public final class QrCode {
|
|
|
|
// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
|
|
|
|
// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
|
|
|
|
byte[] result = new byte[degree];
|
|
|
|
byte[] result = new byte[degree];
|
|
|
|
result[degree - 1] = 1; // Start off with the monomial x^0
|
|
|
|
result[degree - 1] = 1; // Start off with the monomial x^0
|
|
|
|
|
|
|
|
|
|
|
|
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
|
|
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
|
|
// and drop the highest monomial term which is always 1x^degree.
|
|
|
|
// and drop the highest monomial term which is always 1x^degree.
|
|
|
|
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
|
|
|
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
|
|
@ -743,8 +793,8 @@ public final class QrCode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
|
|
|
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
|
|
|
private static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) {
|
|
|
|
private static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) {
|
|
|
|
Objects.requireNonNull(data);
|
|
|
|
Objects.requireNonNull(data);
|
|
|
@ -759,33 +809,33 @@ public final class QrCode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
|
|
|
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
|
|
|
private static int reedSolomonMultiply(int x, int y) {
|
|
|
|
private static int reedSolomonMultiply(int x, int y) {
|
|
|
|
assert x >> 8 == 0 && y >> 8 == 0;
|
|
|
|
assert x >> 8 == 0 && y >> 8 == 0;
|
|
|
|
// Russian peasant multiplication
|
|
|
|
// Russian peasant multiplication
|
|
|
|
int z = 0;
|
|
|
|
int z = 0;
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
z = (z << 1) ^ ((z >>> 7) * 0x11D);
|
|
|
|
z = (z << 1) ^ ((z >>> 7) * 0x11D);
|
|
|
|
z ^= ((y >>> i) & 1) * x;
|
|
|
|
z ^= ((y >>> i) & 1) * x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert z >>> 8 == 0;
|
|
|
|
assert z >>> 8 == 0;
|
|
|
|
return z;
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
|
|
|
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
|
|
|
// QR Code of the given version number and error correction level, with remainder bits discarded.
|
|
|
|
// QR Code of the given version number and error correction level, with remainder bits discarded.
|
|
|
|
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
|
|
|
|
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
|
|
|
|
static int getNumDataCodewords(int ver, Ecc ecl) {
|
|
|
|
static int getNumDataCodewords(int ver, Ecc ecl) {
|
|
|
|
return getNumRawDataModules(ver) / 8
|
|
|
|
return getNumRawDataModules(ver) / 8
|
|
|
|
- ECC_CODEWORDS_PER_BLOCK [ecl.ordinal()][ver]
|
|
|
|
- ecl.getBlockLength(ver)
|
|
|
|
* NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver];
|
|
|
|
* ecl.getNumberOfBlock(ver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Can only be called immediately after a white run is added, and
|
|
|
|
// Can only be called immediately after a white run is added, and
|
|
|
|
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
|
|
|
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
|
|
|
private int finderPenaltyCountPatterns(int[] runHistory) {
|
|
|
|
private int finderPenaltyCountPatterns(int[] runHistory) {
|
|
|
@ -793,10 +843,10 @@ public final class QrCode {
|
|
|
|
assert n <= size * 3;
|
|
|
|
assert n <= size * 3;
|
|
|
|
boolean core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
|
|
|
boolean core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
|
|
|
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
|
|
|
|
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
|
|
|
|
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
|
|
|
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
|
|
|
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
|
|
|
private int finderPenaltyTerminateAndCount(boolean currentRunColor, int currentRunLength, int[] runHistory) {
|
|
|
|
private int finderPenaltyTerminateAndCount(boolean currentRunColor, int currentRunLength, int[] runHistory) {
|
|
|
|
if (currentRunColor) { // Terminate black run
|
|
|
|
if (currentRunColor) { // Terminate black run
|
|
|
@ -807,8 +857,8 @@ public final class QrCode {
|
|
|
|
finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
|
|
finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
|
|
return finderPenaltyCountPatterns(runHistory);
|
|
|
|
return finderPenaltyCountPatterns(runHistory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
|
|
|
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
|
|
|
private void finderPenaltyAddHistory(int currentRunLength, int[] runHistory) {
|
|
|
|
private void finderPenaltyAddHistory(int currentRunLength, int[] runHistory) {
|
|
|
|
if (runHistory[0] == 0)
|
|
|
|
if (runHistory[0] == 0)
|
|
|
@ -816,70 +866,27 @@ public final class QrCode {
|
|
|
|
System.arraycopy(runHistory, 0, runHistory, 1, runHistory.length - 1);
|
|
|
|
System.arraycopy(runHistory, 0, runHistory, 1, runHistory.length - 1);
|
|
|
|
runHistory[0] = currentRunLength;
|
|
|
|
runHistory[0] = currentRunLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns true iff the i'th bit of x is set to 1.
|
|
|
|
// Returns true iff the i'th bit of x is set to 1.
|
|
|
|
static boolean getBit(int x, int i) {
|
|
|
|
static boolean getBit(int x, int i) {
|
|
|
|
return ((x >>> i) & 1) != 0;
|
|
|
|
return ((x >>> i) & 1) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Constants and tables ----*/
|
|
|
|
/*---- Constants and tables ----*/
|
|
|
|
|
|
|
|
|
|
|
|
/** The minimum version number (1) supported in the QR Code Model 2 standard. */
|
|
|
|
/** The minimum version number (1) supported in the QR Code Model 2 standard. */
|
|
|
|
public static final int MIN_VERSION = 1;
|
|
|
|
public static final int MIN_VERSION = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/** The maximum version number (40) supported in the QR Code Model 2 standard. */
|
|
|
|
/** The maximum version number (40) supported in the QR Code Model 2 standard. */
|
|
|
|
public static final int MAX_VERSION = 40;
|
|
|
|
public static final int MAX_VERSION = 40;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// For use in getPenaltyScore(), when evaluating which mask is best.
|
|
|
|
// For use in getPenaltyScore(), when evaluating which mask is best.
|
|
|
|
private static final int PENALTY_N1 = 3;
|
|
|
|
private static final int PENALTY_N1 = 3;
|
|
|
|
private static final int PENALTY_N2 = 3;
|
|
|
|
private static final int PENALTY_N2 = 3;
|
|
|
|
private static final int PENALTY_N3 = 40;
|
|
|
|
private static final int PENALTY_N3 = 40;
|
|
|
|
private static final int PENALTY_N4 = 10;
|
|
|
|
private static final int PENALTY_N4 = 10;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
private static final byte[][] ECC_CODEWORDS_PER_BLOCK = {
|
|
|
|
|
|
|
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
|
|
|
|
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
|
|
|
|
|
|
|
{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
|
|
|
|
|
|
|
|
{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium
|
|
|
|
|
|
|
|
{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile
|
|
|
|
|
|
|
|
{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final byte[][] NUM_ERROR_CORRECTION_BLOCKS = {
|
|
|
|
|
|
|
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
|
|
|
|
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
|
|
|
|
|
|
|
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
|
|
|
|
|
|
|
|
{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
|
|
|
|
|
|
|
|
{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
|
|
|
|
|
|
|
|
{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Public helper enumeration ----*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* The error correction level in a QR Code symbol.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public enum Ecc {
|
|
|
|
|
|
|
|
// Must be declared in ascending order of error protection
|
|
|
|
|
|
|
|
// so that the implicit ordinal() and values() work properly
|
|
|
|
|
|
|
|
/** The QR Code can tolerate about 7% erroneous codewords. */ LOW(1),
|
|
|
|
|
|
|
|
/** The QR Code can tolerate about 15% erroneous codewords. */ MEDIUM(0),
|
|
|
|
|
|
|
|
/** The QR Code can tolerate about 25% erroneous codewords. */ QUARTILE(3),
|
|
|
|
|
|
|
|
/** The QR Code can tolerate about 30% erroneous codewords. */ HIGH(2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// In the range 0 to 3 (unsigned 2-bit integer).
|
|
|
|
|
|
|
|
final int formatBits;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
|
|
|
|
private Ecc(int fb) {
|
|
|
|
|
|
|
|
formatBits = fb;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|