mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
Issue 898 be more precise about calculating data size and version
git-svn-id: https://zxing.googlecode.com/svn/trunk@1852 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
05455c7b68
commit
245d432cd0
|
@ -94,8 +94,8 @@ public final class Encoder {
|
|||
BitArray dataBits = new BitArray();
|
||||
appendBytes(content, mode, dataBits, encoding);
|
||||
// Step 3: Initialize QR code that can contain "dataBits".
|
||||
int numInputBytes = dataBits.getSizeInBytes();
|
||||
initQRCode(numInputBytes, ecLevel, mode, qrCode);
|
||||
int numInputBits = dataBits.getSize();
|
||||
initQRCode(numInputBits, ecLevel, mode, qrCode);
|
||||
|
||||
// Step 4: Build another bit vector that contains header and data.
|
||||
BitArray headerAndDataBits = new BitArray();
|
||||
|
@ -219,10 +219,10 @@ public final class Encoder {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
|
||||
* Initialize "qrCode" according to "numInputBits", "ecLevel", and "mode". On success,
|
||||
* modify "qrCode".
|
||||
*/
|
||||
private static void initQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode,
|
||||
private static void initQRCode(int numInputBits, ErrorCorrectionLevel ecLevel, Mode mode,
|
||||
QRCode qrCode) throws WriterException {
|
||||
qrCode.setECLevel(ecLevel);
|
||||
qrCode.setMode(mode);
|
||||
|
@ -241,8 +241,8 @@ public final class Encoder {
|
|||
int numDataBytes = numBytes - numEcBytes;
|
||||
// We want to choose the smallest version which can contain data of "numInputBytes" + some
|
||||
// extra bits for the header (mode info and length info). The header can be three bytes
|
||||
// (precisely 4 + 16 bits) at most. Hence we do +3 here.
|
||||
if (numDataBytes >= numInputBytes + 3) {
|
||||
// (precisely 4 + 16 bits) at most.
|
||||
if (numDataBytes >= getTotalInputBytes(numInputBits, version, mode)) {
|
||||
// Yay, we found the proper rs block info!
|
||||
qrCode.setVersion(versionNum);
|
||||
qrCode.setNumTotalBytes(numBytes);
|
||||
|
@ -257,6 +257,15 @@ public final class Encoder {
|
|||
}
|
||||
throw new WriterException("Cannot find proper rs block info (input data too big?)");
|
||||
}
|
||||
|
||||
private static int getTotalInputBytes(int numInputBits, Version version, Mode mode) {
|
||||
int modeInfoBits = 4;
|
||||
int charCountBits = mode.getCharacterCountBits(version);
|
||||
int headerBits = modeInfoBits + charCountBits;
|
||||
int totalBits = numInputBits + headerBits;
|
||||
|
||||
return (totalBits + 7) / 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
|
||||
|
|
Loading…
Reference in a new issue