mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Minor follow on to last QR code version hint commit
This commit is contained in:
parent
d60c0f14bd
commit
ba6ce9fb25
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<EncodeHintType, Object> 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<EncodeHintType, Object> 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<EncodeHintType, Object> 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<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
|
||||
hints.put(EncodeHintType.QR_VERSION, 3);
|
||||
Encoder.encode("THISMESSAGEISTOOLONGFORAQRCODEVERSION3", ErrorCorrectionLevel.H, hints);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue