/**
* Initialize "qrCode" according to "numInputBytes", "ecLevel", and "mode". On success,
* modify "qrCode".
*/
private static void InitQRCode(int numInputBytes, ErrorCorrectionLevel ecLevel, Mode mode,
QRCode qrCode)
{
qrCode.SetECLevel(ecLevel);
qrCode.SetMode(mode);
// In the following comments, we use numbers of Version 7-H.
for (int versionNum = 1; versionNum <= 40; versionNum++)
{
Version version = Version.GetVersionForNumber(versionNum);
// numBytes = 196
int numBytes = version.GetTotalCodewords();
// getNumECBytes = 130
Version.ECBlocks ecBlocks = version.GetECBlocksForLevel(ecLevel);
int numEcBytes = ecBlocks.GetTotalECCodewords();
// getNumRSBlocks = 5
int numRSBlocks = ecBlocks.GetNumBlocks();
// getNumDataBytes = 196 - 130 = 66
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)
{
// Yay, we found the proper rs block info!
qrCode.SetVersion(versionNum);
qrCode.SetNumTotalBytes(numBytes);
qrCode.SetNumDataBytes(numDataBytes);
qrCode.SetNumRSBlocks(numRSBlocks);
// getNumECBytes = 196 - 66 = 130
qrCode.SetNumECBytes(numEcBytes);
// matrix width = 21 + 6 * 4 = 45
qrCode.SetMatrixWidth(version.GetDimensionForVersion());
return;
}
}
throw new WriterException("Cannot find proper rs block info (input data too big?)");
}