@nuintun/qrcode
Version:
A pure JavaScript QRCode encode and decode library.
99 lines (94 loc) • 2.42 kB
JavaScript
/**
* @module QRCode
* @package @nuintun/qrcode
* @license MIT
* @version 5.0.3
* @author nuintun <nuintun@qq.com>
* @description A pure JavaScript QRCode encode and decode library.
* @see https://github.com/nuintun/qrcode#readme
*/
'use strict';
const utils = require('../common/utils.cjs');
const ECLevel = require('../common/ECLevel.cjs');
/**
* @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 = utils.hammingWeight(formatInfo1 ^ maskedFormatInfo);
if (bitsDiff < bestDiff) {
bestDiff = bitsDiff;
bestFormatInfo = formatInfo;
}
if (formatInfo1 !== formatInfo2) {
// Also try the other option.
bitsDiff = utils.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');
}
exports.FormatInfo = FormatInfo;
exports.decodeFormatInfo = decodeFormatInfo;