* 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
(ecLevel: ErrorCorrectionLevel,
mode: Mode,
headerBits: BitArray,
dataBits: BitArray)
| 172 | * @throws WriterException if the data cannot fit in any version |
| 173 | */ |
| 174 | private static recommendVersion(ecLevel: ErrorCorrectionLevel, |
| 175 | mode: Mode, |
| 176 | headerBits: BitArray, |
| 177 | dataBits: BitArray): Version /*throws WriterException*/ { |
| 178 | // Hard part: need to know version to know how many bits length takes. But need to know how many |
| 179 | // bits it takes to know version. First we take a guess at version by assuming version will be |
| 180 | // the minimum, 1: |
| 181 | const provisionalBitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, Version.getVersionForNumber(1)); |
| 182 | const provisionalVersion = this.chooseVersion(provisionalBitsNeeded, ecLevel); |
| 183 | |
| 184 | // Use that guess to calculate the right version. I am still not sure this works in 100% of cases. |
| 185 | const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion); |
| 186 | return this.chooseVersion(bitsNeeded, ecLevel); |
| 187 | } |
| 188 | |
| 189 | private static calculateBitsNeeded(mode: Mode, |
| 190 | headerBits: BitArray, |
no test coverage detected