@nuintun/qrcode
Version:
A pure JavaScript QRCode encode and decode library.
96 lines (92 loc) • 2.37 kB
JavaScript
/**
* @module QRCode
* @package @nuintun/qrcode
* @license MIT
* @version 5.0.2
* @author nuintun <nuintun@qq.com>
* @description A pure JavaScript QRCode encode and decode library.
* @see https://github.com/nuintun/qrcode#readme
*/
import { hammingWeight } from '../common/utils.js';
import { fromECLevelBits } from '../common/ECLevel.js';
/**
* @module FormatInfo
*/
const FORMAT_INFO_DECODE_TABLE = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
];
class FormatInfo {
constructor(formatInfo) {
this.
this.
}
get mask() {
return this.
}
get level() {
return this.
}
}
function decodeFormatInfo(formatInfo1, formatInfo2) {
// Find the int in FORMAT_INFO_DECODE_TABLE with fewest bits differing.
let bestDiff = 32;
let bestFormatInfo = 0;
for (const [maskedFormatInfo, formatInfo] of FORMAT_INFO_DECODE_TABLE) {
if (formatInfo1 === maskedFormatInfo || formatInfo2 === maskedFormatInfo) {
// Found an exact match.
return new FormatInfo(formatInfo);
}
let bitsDiff = hammingWeight(formatInfo1 ^ maskedFormatInfo);
if (bitsDiff < bestDiff) {
bestDiff = bitsDiff;
bestFormatInfo = formatInfo;
}
if (formatInfo1 !== formatInfo2) {
// Also try the other option.
bitsDiff = hammingWeight(formatInfo2 ^ maskedFormatInfo);
if (bitsDiff < bestDiff) {
bestDiff = bitsDiff;
bestFormatInfo = formatInfo;
}
}
}
// Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match.
if (bestDiff <= 3) {
return new FormatInfo(bestFormatInfo);
}
throw new Error('unable to decode format information');
}
export { FormatInfo, decodeFormatInfo };