From ba6ce9fb25b37914cbd6429725d80465d662eb5c Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Mon, 8 Aug 2016 11:30:18 +0100 Subject: [PATCH] Minor follow on to last QR code version hint commit --- .../java/com/google/zxing/EncodeHintType.java | 6 ++--- .../google/zxing/qrcode/encoder/Encoder.java | 20 +++++++---------- .../zxing/qrcode/encoder/EncoderTestCase.java | 22 +++++++------------ 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/com/google/zxing/EncodeHintType.java b/core/src/main/java/com/google/zxing/EncodeHintType.java index 0fa701f76..32b50f6be 100644 --- a/core/src/main/java/com/google/zxing/EncodeHintType.java +++ b/core/src/main/java/com/google/zxing/EncodeHintType.java @@ -97,8 +97,8 @@ public enum EncodeHintType { AZTEC_LAYERS, /** - * Specifies the exact version of QR code to be encoded. An integer. If the data specified - * cannot fit within the required version, a WriterException will be thrown. + * Specifies the exact version of QR code to be encoded. + * (Type {@link Integer}, or {@link String} representation of the integer value). */ - QR_VERSION + QR_VERSION, } diff --git a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java index b98e2b01b..90d6517ce 100644 --- a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java +++ b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java @@ -106,13 +106,12 @@ public final class Encoder { BitArray dataBits = new BitArray(); appendBytes(content, mode, dataBits, encoding); - Version version = null; + Version version; if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) { - Version requestedVersion = Version.getVersionForNumber((Integer) hints.get(EncodeHintType.QR_VERSION)); - int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, requestedVersion); - if (willFit(bitsNeeded, requestedVersion, ecLevel)) { - version = requestedVersion; - } else { + int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString()); + version = Version.getVersionForNumber(versionNumber); + int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version); + if (!willFit(bitsNeeded, version, ecLevel)) { throw new WriterException("Data too big for requested version"); } } else { @@ -160,6 +159,7 @@ public final class Encoder { /** * 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 */ 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. int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion); - Version version = chooseVersion(bitsNeeded, ecLevel); - return version; + return chooseVersion(bitsNeeded, ecLevel); } private static int calculateBitsNeeded(Mode mode, BitArray headerBits, BitArray dataBits, Version version) { - int bitsNeeded = headerBits.getSize() - + mode.getCharacterCountBits(version) - + dataBits.getSize(); - return bitsNeeded; + return headerBits.getSize() + mode.getCharacterCountBits(version) + dataBits.getSize(); } /** diff --git a/core/src/test/java/com/google/zxing/qrcode/encoder/EncoderTestCase.java b/core/src/test/java/com/google/zxing/qrcode/encoder/EncoderTestCase.java index 51d4003df..96c785e60 100644 --- a/core/src/test/java/com/google/zxing/qrcode/encoder/EncoderTestCase.java +++ b/core/src/test/java/com/google/zxing/qrcode/encoder/EncoderTestCase.java @@ -28,7 +28,6 @@ import org.junit.Test; import java.io.UnsupportedEncodingException; import java.util.EnumMap; -import java.util.HashMap; import java.util.Map; /** @@ -131,22 +130,17 @@ public final class EncoderTestCase extends Assert { @Test public void testEncodeWithVersion() throws WriterException { - Map hints = new HashMap<>(); - hints.put(EncodeHintType.QR_VERSION, 7); - QRCode qrCode = Encoder.encode("ABCDEF", ErrorCorrectionLevel.H, hints); - assertTrue(qrCode.toString().contains(" version: 7\n")); + Map hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.QR_VERSION, 7); + QRCode qrCode = Encoder.encode("ABCDEF", ErrorCorrectionLevel.H, hints); + assertTrue(qrCode.toString().contains(" version: 7\n")); } - @Test + @Test(expected = WriterException.class) public void testEncodeWithVersionTooSmall() throws WriterException { - Map hints = new HashMap<>(); - hints.put(EncodeHintType.QR_VERSION, 3); - try { - Encoder.encode("THISMESSAGEISTOOLONGFORAQRCODEVERSION3", ErrorCorrectionLevel.H, hints); - fail(); - } catch (WriterException e) { - assertEquals("Data too big for requested version", e.getMessage()); - } + Map hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.QR_VERSION, 3); + Encoder.encode("THISMESSAGEISTOOLONGFORAQRCODEVERSION3", ErrorCorrectionLevel.H, hints); } @Test