(matrix: BitMatrix)
| 140 | } |
| 141 | |
| 142 | function readVersion(matrix: BitMatrix): Version { |
| 143 | const dimension = matrix.height; |
| 144 | |
| 145 | const provisionalVersion = Math.floor((dimension - 17) / 4); |
| 146 | if (provisionalVersion <= 6) { // 6 and under dont have version info in the QR code |
| 147 | return VERSIONS[provisionalVersion - 1]; |
| 148 | } |
| 149 | |
| 150 | let topRightVersionBits = 0; |
| 151 | for (let y = 5; y >= 0; y--) { |
| 152 | for (let x = dimension - 9; x >= dimension - 11; x--) { |
| 153 | topRightVersionBits = pushBit(matrix.get(x, y), topRightVersionBits); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | let bottomLeftVersionBits = 0; |
| 158 | for (let x = 5; x >= 0; x--) { |
| 159 | for (let y = dimension - 9; y >= dimension - 11; y--) { |
| 160 | bottomLeftVersionBits = pushBit(matrix.get(x, y), bottomLeftVersionBits); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | let bestDifference = Infinity; |
| 165 | let bestVersion: Version; |
| 166 | for (const version of VERSIONS) { |
| 167 | if (version.infoBits === topRightVersionBits || version.infoBits === bottomLeftVersionBits) { |
| 168 | return version; |
| 169 | } |
| 170 | |
| 171 | let difference = numBitsDiffering(topRightVersionBits, version.infoBits); |
| 172 | if (difference < bestDifference) { |
| 173 | bestVersion = version; |
| 174 | bestDifference = difference; |
| 175 | } |
| 176 | |
| 177 | difference = numBitsDiffering(bottomLeftVersionBits, version.infoBits); |
| 178 | if (difference < bestDifference) { |
| 179 | bestVersion = version; |
| 180 | bestDifference = difference; |
| 181 | } |
| 182 | } |
| 183 | // We can tolerate up to 3 bits of error since no two version info codewords will |
| 184 | // differ in less than 8 bits. |
| 185 | if (bestDifference <= 3) { |
| 186 | return bestVersion; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | function readFormatInformation(matrix: BitMatrix) { |
| 191 | let topLeftFormatInfoBits = 0; |
no test coverage detected
searching dependent graphs…