Merge pull request #1 from gerzees/master

pull fork repository
pull/90/head
minsu4107 5 years ago committed by GitHub
commit 3249a4dae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 &#x2212;1 for automatic mask * @param mask the mask number to use (between 0 and 7 (inclusive)), or &#x2212;1 for automatic mask
@ -141,61 +147,64 @@ 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 isMaskValid = mask < -1 || mask > 7;
if (!isVersionInRange || isMaskValid)
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
// Find the minimal version number to use // Find the minimal version number to use
int version, dataUsedBits; int version, dataUsedBits;
for (version = minVersion; ; version++) { for (version = minVersion; ; version++) {
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; // Number of data bits available
dataUsedBits = QrSegment.getTotalBits(segs, version); dataUsedBits = QrSegment.getTotalBits(segments, version);
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
break; // This version number is found to be suitable break; // This version number is found to be suitable
if (version >= maxVersion) { // All versions in the range could not fit the given data if (version >= maxVersion) { // All versions in the range could not fit the given data
String msg = "Segment too long"; String message = "Segment too long";
if (dataUsedBits != -1) if (dataUsedBits != -1)
msg = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits); message = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits);
throw new DataTooLongException(msg); throw new DataTooLongException(message);
} }
} }
assert dataUsedBits != -1; assert dataUsedBits != -1;
// Increase the error correction level while the data still fits in the current version number // Increase the error correction level while the data still fits in the current version number
for (Ecc newEcl : Ecc.values()) { // From low to high for (Ecc newEcl : Ecc.values()) { // From low to high
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8) final boolean canIncreaseErrorCorrectionLevel = dataUsedBits <= getNumDataCodewords(version, newEcl) * 8;
ecl = newEcl; if (boostEcl && canIncreaseErrorCorrectionLevel)
errorCorrectionLevel = newEcl;
} }
// Concatenate all segments to create the data bit string // Concatenate all segments to create the data bit string
BitBuffer bb = new BitBuffer(); BitBuffer bitBuffer = new BitBuffer();
for (QrSegment seg : segs) { for (QrSegment segment : segments) {
bb.appendBits(seg.mode.modeBits, 4); bitBuffer.appendBits(segment.mode.modeBits, 4);
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version));
bb.appendData(seg.data); bitBuffer.appendData(segment.data);
} }
assert bb.bitLength() == dataUsedBits; assert bitBuffer.bitLength() == dataUsedBits;
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8;
assert bb.bitLength() <= dataCapacityBits; assert bitBuffer.bitLength() <= dataCapacityBits;
bb.appendBits(0, Math.min(4, dataCapacityBits - bb.bitLength())); bitBuffer.appendBits(0, Math.min(4, dataCapacityBits - bitBuffer.bitLength()));
bb.appendBits(0, (8 - bb.bitLength() % 8) % 8); bitBuffer.appendBits(0, (8 - bitBuffer.bitLength() % 8) % 8);
assert bb.bitLength() % 8 == 0; assert bitBuffer.bitLength() % 8 == 0;
// Pad with alternating bytes until data capacity is reached // Pad with alternating bytes until data capacity is reached
for (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) for (int padByte = 0xEC; bitBuffer.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
bb.appendBits(padByte, 8); bitBuffer.appendBits(padByte, 8);
// Pack bits into bytes in big endian // Pack bits into bytes in big endian
byte[] dataCodewords = new byte[bb.bitLength() / 8]; byte[] dataCodewords = new byte[bitBuffer.bitLength() / 8];
for (int i = 0; i < bb.bitLength(); i++) for (int i = 0; i < bitBuffer.bitLength(); i++)
dataCodewords[i >>> 3] |= bb.getBit(i) << (7 - (i & 7)); dataCodewords[i >>> 3] |= bitBuffer.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);
} }
/*---- Instance fields ----*/ /*---- Instance fields ----*/
@ -236,32 +245,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 &#x2212;1 for automatic choice or from 0 to 7 for fixed choice * @param mask the mask pattern to use, which is either &#x2212;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;
} }
@ -352,22 +361,25 @@ 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 // Draw numerous alignment patterns
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]);
} }
} }

Loading…
Cancel
Save