diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java index 58c4534..bf3a11a 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java @@ -52,6 +52,12 @@ import java.util.Objects; */ public final class QrCode { + private static final int FINDER_SIZE = 3; + + private static final int TIMING_COORDINATE = 6; + + + /*---- 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. * This is a mid-level API; the high-level API is {@link #encodeText(String,Ecc)} * and {@link #encodeBinary(byte[],Ecc)}.
- * @param segs the segments to encode - * @param ecl the error correction level to use (not {@code null}) (boostable) + * @param segments the segments to encode + * @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 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 @@ -141,61 +147,64 @@ public final class QrCode { * @throws DataTooLongException if the segments fail to fit in * the maxVersion QR Code at the ECL, which means they are too long */ - public static QrCode encodeSegments(ListThis 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.
- * @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 version the version number to use, which must be in the range 1 to 40 (inclusive) + * @param errorCorrectionLevel the error correction level to use * @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 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 */ - 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 - if (ver < MIN_VERSION || ver > MAX_VERSION) + if (version < MIN_VERSION || version > MAX_VERSION) 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"); - version = ver; - size = ver * 4 + 17; - errorCorrectionLevel = Objects.requireNonNull(ecl); + this.version = version; + this.size = version * 4 + 17; + this.errorCorrectionLevel = Objects.requireNonNull(errorCorrectionLevel); Objects.requireNonNull(dataCodewords); - modules = new boolean[size][size]; // Initially all white - isFunction = new boolean[size][size]; + this.modules = new boolean[size][size]; // Initially all white + this.isFunction = new boolean[size][size]; // Compute ECC, draw modules, do masking drawFunctionPatterns(); byte[] allCodewords = addEccAndInterleave(dataCodewords); drawCodewords(allCodewords); - this.mask = handleConstructorMasking(msk); + this.mask = handleConstructorMasking(mask); isFunction = null; } @@ -352,22 +361,25 @@ public final class QrCode { private void drawFunctionPatterns() { // Draw horizontal and vertical timing patterns for (int i = 0; i < size; i++) { - setFunctionModule(6, i, i % 2 == 0); - setFunctionModule(i, 6, i % 2 == 0); + setFunctionModule(TIMING_COORDINATE, i, i % 2 == 0); + setFunctionModule(i, TIMING_COORDINATE, i % 2 == 0); } // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules) - drawFinderPattern(3, 3); - drawFinderPattern(size - 4, 3); - drawFinderPattern(3, size - 4); + drawFinderPattern(FINDER_SIZE, FINDER_SIZE); + drawFinderPattern(size - 1 - FINDER_SIZE, FINDER_SIZE); + drawFinderPattern(FINDER_SIZE, size - 1 - FINDER_SIZE); // Draw numerous alignment patterns int[] alignPatPos = getAlignmentPatternPositions(); int numAlign = alignPatPos.length; for (int i = 0; i < numAlign; i++) { for (int j = 0; j < numAlign; j++) { - // Don't draw on the three finder corners - if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0)) + final boolean isLeftTop = i == 0 && 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]); } } @@ -541,7 +553,7 @@ public final class QrCode { // 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 // 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) throw new IllegalArgumentException("Mask value out of range"); for (int y = 0; y < size; y++) { @@ -590,7 +602,7 @@ public final class QrCode { // 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. - private int getPenaltyScore() { + private int getPenaltyScore() { int result = 0; // Adjacent modules in row having same color, and finder-like patterns @@ -693,7 +705,7 @@ public final class QrCode { // 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. // 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) throw new IllegalArgumentException("Version number out of range");