1. Extract Method

2. encodeSegments() in QrCode
3. encodeSegments() is long method
pull/90/head
gerzees 5 years ago
parent 580485c189
commit ef3a6f7021

@ -155,56 +155,100 @@ public final class QrCode {
if (!isVersionInRange || isMaskValid) if (!isVersionInRange || isMaskValid)
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
// Find the minimal version number to use
int version, dataUsedBits; int version = findMinimalVersion(segments, errorCorrectionLevel, minVersion, maxVersion);
for (version = minVersion; ; version++) {
int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8; // Number of data bits available int dataUsedBits = QrSegment.getTotalBits(segments, version);
dataUsedBits = QrSegment.getTotalBits(segments, version);
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) errorCorrectionLevel = findMaximalErrorCorrectionLevel(errorCorrectionLevel, boostEcl, version, dataUsedBits);
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"; BitBuffer bitBuffer = segmentsToBitBuffer(segments, version);
if (dataUsedBits != -1) assert bitBuffer.bitLength() == dataUsedBits;
message = String.format("Data length = %d bits, Max capacity = %d bits", dataUsedBits, dataCapacityBits);
throw new DataTooLongException(message);
int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8;
assert bitBuffer.bitLength() <= dataCapacityBits;
addTerminator(bitBuffer, dataCapacityBits);
addPad(bitBuffer, dataCapacityBits);
byte[] dataCodewords = bitBufferToCodewords(bitBuffer);
// Create the QR Code object
return new QrCode(version, errorCorrectionLevel, dataCodewords, mask);
} }
// Pack bits into bytes in big endian
private static byte[] bitBufferToCodewords(BitBuffer bitBuffer) {
byte[] dataCodewords = new byte[bitBuffer.bitLength() / 8];
for (int i = 0; i < bitBuffer.bitLength(); i++)
dataCodewords[i >>> 3] |= bitBuffer.getBit(i) << (7 - (i & 7));
return dataCodewords;
} }
assert dataUsedBits != -1;
// Increase the error correction level while the data still fits in the current version number
for (Ecc newEcl : Ecc.values()) { // From low to high // Pad with alternating bytes until data capacity is reached
final boolean canIncreaseErrorCorrectionLevel = dataUsedBits <= getNumDataCodewords(version, newEcl) * 8; private static void addPad(BitBuffer bitBuffer, int dataCapacityBits) {
if (boostEcl && canIncreaseErrorCorrectionLevel) for (int padByte = 0xEC; bitBuffer.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
errorCorrectionLevel = newEcl; bitBuffer.appendBits(padByte, 8);
}
/*---- Private helper methods for encodeSegments ----*/
// Add terminator and pad up to a byte if applicable
private static void addTerminator(BitBuffer bitBuffer, int dataCapacityBits) {
bitBuffer.appendBits(0, Math.min(4, dataCapacityBits - bitBuffer.bitLength()));
bitBuffer.appendBits(0, (8 - bitBuffer.bitLength() % 8) % 8);
assert bitBuffer.bitLength() % 8 == 0;
} }
// Concatenate all segments to create the data bit string // Concatenate all segments to create the data bit string
private static BitBuffer segmentsToBitBuffer(List<QrSegment> segments, int version) {
BitBuffer bitBuffer = new BitBuffer(); BitBuffer bitBuffer = new BitBuffer();
for (QrSegment segment : segments) { for (QrSegment segment : segments) {
bitBuffer.appendBits(segment.mode.modeBits, 4); bitBuffer.appendBits(segment.mode.modeBits, 4);
bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version)); bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version));
bitBuffer.appendData(segment.data); bitBuffer.appendData(segment.data);
} }
assert bitBuffer.bitLength() == dataUsedBits; return bitBuffer;
}
// Add terminator and pad up to a byte if applicable
int dataCapacityBits = getNumDataCodewords(version, errorCorrectionLevel) * 8;
assert bitBuffer.bitLength() <= dataCapacityBits;
bitBuffer.appendBits(0, Math.min(4, dataCapacityBits - bitBuffer.bitLength()));
bitBuffer.appendBits(0, (8 - bitBuffer.bitLength() % 8) % 8);
assert bitBuffer.bitLength() % 8 == 0;
// Pad with alternating bytes until data capacity is reached // Increase the error correction level while the data still fits in the current version number
for (int padByte = 0xEC; bitBuffer.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) private static Ecc findMaximalErrorCorrectionLevel(Ecc errorCorrectionLevel, boolean boostEcl, int version,
bitBuffer.appendBits(padByte, 8); 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;
}
// Pack bits into bytes in big endian
byte[] dataCodewords = new byte[bitBuffer.bitLength() / 8];
for (int i = 0; i < bitBuffer.bitLength(); i++)
dataCodewords[i >>> 3] |= bitBuffer.getBit(i) << (7 - (i & 7));
// Create the QR Code object //Returns the minimal version number to use
return new QrCode(version, errorCorrectionLevel, dataCodewords, mask); 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 ----*/

Loading…
Cancel
Save