Minor follow on to last QR code version hint commit

This commit is contained in:
Sean Owen 2016-08-08 11:30:18 +01:00
parent d60c0f14bd
commit ba6ce9fb25
3 changed files with 19 additions and 29 deletions

View file

@ -97,8 +97,8 @@ public enum EncodeHintType {
AZTEC_LAYERS, AZTEC_LAYERS,
/** /**
* Specifies the exact version of QR code to be encoded. An integer. If the data specified * Specifies the exact version of QR code to be encoded.
* cannot fit within the required version, a WriterException will be thrown. * (Type {@link Integer}, or {@link String} representation of the integer value).
*/ */
QR_VERSION QR_VERSION,
} }

View file

@ -106,13 +106,12 @@ public final class Encoder {
BitArray dataBits = new BitArray(); BitArray dataBits = new BitArray();
appendBytes(content, mode, dataBits, encoding); appendBytes(content, mode, dataBits, encoding);
Version version = null; Version version;
if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) { if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
Version requestedVersion = Version.getVersionForNumber((Integer) hints.get(EncodeHintType.QR_VERSION)); int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, requestedVersion); version = Version.getVersionForNumber(versionNumber);
if (willFit(bitsNeeded, requestedVersion, ecLevel)) { int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
version = requestedVersion; if (!willFit(bitsNeeded, version, ecLevel)) {
} else {
throw new WriterException("Data too big for requested version"); throw new WriterException("Data too big for requested version");
} }
} else { } else {
@ -160,6 +159,7 @@ public final class Encoder {
/** /**
* Decides the smallest version of QR code that will contain all of the provided data. * Decides the smallest version of QR code that will contain all of the provided data.
*
* @throws WriterException if the data cannot fit in any version * @throws WriterException if the data cannot fit in any version
*/ */
private static Version recommendVersion(ErrorCorrectionLevel ecLevel, private static Version recommendVersion(ErrorCorrectionLevel ecLevel,
@ -174,18 +174,14 @@ public final class Encoder {
// Use that guess to calculate the right version. I am still not sure this works in 100% of cases. // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.
int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion); int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion);
Version version = chooseVersion(bitsNeeded, ecLevel); return chooseVersion(bitsNeeded, ecLevel);
return version;
} }
private static int calculateBitsNeeded(Mode mode, private static int calculateBitsNeeded(Mode mode,
BitArray headerBits, BitArray headerBits,
BitArray dataBits, BitArray dataBits,
Version version) { Version version) {
int bitsNeeded = headerBits.getSize() return headerBits.getSize() + mode.getCharacterCountBits(version) + dataBits.getSize();
+ mode.getCharacterCountBits(version)
+ dataBits.getSize();
return bitsNeeded;
} }
/** /**

View file

@ -28,7 +28,6 @@ import org.junit.Test;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -131,22 +130,17 @@ public final class EncoderTestCase extends Assert {
@Test @Test
public void testEncodeWithVersion() throws WriterException { public void testEncodeWithVersion() throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<>(); Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.QR_VERSION, 7); hints.put(EncodeHintType.QR_VERSION, 7);
QRCode qrCode = Encoder.encode("ABCDEF", ErrorCorrectionLevel.H, hints); QRCode qrCode = Encoder.encode("ABCDEF", ErrorCorrectionLevel.H, hints);
assertTrue(qrCode.toString().contains(" version: 7\n")); assertTrue(qrCode.toString().contains(" version: 7\n"));
} }
@Test @Test(expected = WriterException.class)
public void testEncodeWithVersionTooSmall() throws WriterException { public void testEncodeWithVersionTooSmall() throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<>(); Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.QR_VERSION, 3); hints.put(EncodeHintType.QR_VERSION, 3);
try { Encoder.encode("THISMESSAGEISTOOLONGFORAQRCODEVERSION3", ErrorCorrectionLevel.H, hints);
Encoder.encode("THISMESSAGEISTOOLONGFORAQRCODEVERSION3", ErrorCorrectionLevel.H, hints);
fail();
} catch (WriterException e) {
assertEquals("Data too big for requested version", e.getMessage());
}
} }
@Test @Test