|
|
@ -231,16 +231,16 @@ public final class QrCode {
|
|
|
|
* @param ver the version number to use, which must be in the range 1 to 40 (inclusive)
|
|
|
|
* @param ver the version number to use, which must be in the range 1 to 40 (inclusive)
|
|
|
|
* @param ecl the error correction level to use
|
|
|
|
* @param ecl 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 mask the mask pattern to use, which is either −1 for automatic choice or from 0 to 7 for fixed choice
|
|
|
|
* @param msk 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 mask) {
|
|
|
|
public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int msk) {
|
|
|
|
// Check arguments and initialize fields
|
|
|
|
// Check arguments and initialize fields
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
|
|
throw new IllegalArgumentException("Version value out of range");
|
|
|
|
throw new IllegalArgumentException("Version value out of range");
|
|
|
|
if (mask < -1 || mask > 7)
|
|
|
|
if (msk < -1 || msk > 7)
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
throw new IllegalArgumentException("Mask value out of range");
|
|
|
|
version = ver;
|
|
|
|
version = ver;
|
|
|
|
size = ver * 4 + 17;
|
|
|
|
size = ver * 4 + 17;
|
|
|
@ -253,7 +253,7 @@ public final class QrCode {
|
|
|
|
// Compute ECC, draw modules, do masking
|
|
|
|
// Compute ECC, draw modules, do masking
|
|
|
|
byte[] allCodewords = addEccAndInterleave(dataCodewords);
|
|
|
|
byte[] allCodewords = addEccAndInterleave(dataCodewords);
|
|
|
|
drawCodewords(tpl.dataOutputBitIndexes, allCodewords);
|
|
|
|
drawCodewords(tpl.dataOutputBitIndexes, allCodewords);
|
|
|
|
this.mask = handleConstructorMasking(tpl.masks, mask);
|
|
|
|
this.mask = handleConstructorMasking(tpl.masks, msk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -345,9 +345,9 @@ public final class QrCode {
|
|
|
|
|
|
|
|
|
|
|
|
// Draws two copies of the format bits (with its own error correction code)
|
|
|
|
// Draws two copies of the format bits (with its own error correction code)
|
|
|
|
// 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 mask) {
|
|
|
|
private void drawFormatBits(int msk) {
|
|
|
|
// Calculate error correction code and pack bits
|
|
|
|
// Calculate error correction code and pack bits
|
|
|
|
int data = errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
|
|
|
|
int data = errorCorrectionLevel.formatBits << 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);
|
|
|
@ -440,35 +440,35 @@ 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[] mask) {
|
|
|
|
private void applyMask(int[] msk) {
|
|
|
|
if (mask.length != modules.length)
|
|
|
|
if (msk.length != modules.length)
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
for (int i = 0; i < mask.length; i++)
|
|
|
|
for (int i = 0; i < msk.length; i++)
|
|
|
|
modules[i] ^= mask[i];
|
|
|
|
modules[i] ^= msk[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 'mask' argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
|
|
|
|
// method is called. The 'mask' 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[][] masks, int mask) {
|
|
|
|
private int handleConstructorMasking(int[][] masks, int msk) {
|
|
|
|
if (mask == -1) { // Automatically choose best mask
|
|
|
|
if (msk == -1) { // Automatically choose best mask
|
|
|
|
int minPenalty = Integer.MAX_VALUE;
|
|
|
|
int minPenalty = Integer.MAX_VALUE;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
applyMask(masks[i]);
|
|
|
|
applyMask(masks[i]);
|
|
|
|
drawFormatBits(i);
|
|
|
|
drawFormatBits(i);
|
|
|
|
int penalty = getPenaltyScore();
|
|
|
|
int penalty = getPenaltyScore();
|
|
|
|
if (penalty < minPenalty) {
|
|
|
|
if (penalty < minPenalty) {
|
|
|
|
mask = i;
|
|
|
|
msk = i;
|
|
|
|
minPenalty = penalty;
|
|
|
|
minPenalty = penalty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
applyMask(masks[i]); // Undoes the mask due to XOR
|
|
|
|
applyMask(masks[i]); // Undoes the mask due to XOR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert 0 <= mask && mask <= 7;
|
|
|
|
assert 0 <= msk && msk <= 7;
|
|
|
|
applyMask(masks[mask]); // Apply the final choice of mask
|
|
|
|
applyMask(masks[msk]); // Apply the final choice of mask
|
|
|
|
drawFormatBits(mask); // Overwrite old format bits
|
|
|
|
drawFormatBits(msk); // Overwrite old format bits
|
|
|
|
return mask; // The caller shall assign this value to the final-declared field
|
|
|
|
return msk; // The caller shall assign this value to the final-declared field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|