qrcoder
Version:
QRCoder is *a pure browser qrcode generation* which is standalone. It is based on a <a href='http://www.d-project.com/qrcode/index.html'>library</a> which build qrcode in various language.
295 lines (245 loc) • 5.79 kB
JavaScript
import { QRErrorCorrectionLevel } from './constant'
const RS_BLOCK_TABLE = [
// L
// M
// Q
// H
// 1
[],
[],
[],
[],
// 2
[],
[],
[],
[],
// 3
[],
[],
[],
[],
// 4
[],
[],
[],
[],
// 5
[],
[],
[],
[],
// 6
[],
[],
[],
[],
// 7
[],
[],
[],
[],
// 8
[],
[],
[],
[],
// 9
[],
[],
[],
[],
// 10
[],
[],
[],
[],
// 11
[],
[],
[],
[],
// 12
[],
[],
[],
[],
// 13
[],
[],
[],
[],
// 14
[],
[],
[],
[],
// 15
[],
[],
[],
[],
// 16
[],
[],
[],
[],
// 17
[],
[],
[],
[],
// 18
[],
[],
[],
[],
// 19
[],
[],
[],
[],
// 20
[],
[],
[],
[],
// 21
[],
[],
[],
[],
// 22
[],
[],
[],
[],
// 23
[],
[],
[],
[],
// 24
[],
[],
[],
[],
// 25
[],
[],
[],
[],
// 26
[],
[],
[],
[],
// 27
[],
[],
[],
[],
// 28
[],
[],
[],
[],
// 29
[],
[],
[],
[],
// 30
[],
[],
[],
[],
// 31
[],
[],
[],
[],
// 32
[],
[],
[],
[],
// 33
[],
[],
[],
[],
// 34
[],
[],
[],
[],
// 35
[],
[],
[],
[],
// 36
[],
[],
[],
[],
// 37
[],
[],
[],
[],
// 38
[],
[],
[],
[],
// 39
[],
[],
[],
[],
// 40
[],
[],
[],
[]
]
function Qrrsblock (totalCount, dataCount) {
this.totalCount = totalCount
this.dataCount = dataCount
}
function getRsBlockTable (typeNumber, errorCorrectionLevel) {
switch (errorCorrectionLevel) {
case QRErrorCorrectionLevel.L:
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0]
case QRErrorCorrectionLevel.M:
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1]
case QRErrorCorrectionLevel.Q:
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2]
case QRErrorCorrectionLevel.H:
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3]
default :
return undefined
}
}
export const QRRSBlock = {
getRSBlocks: function (typeNumber, errorCorrectionLevel) {
let rsBlock = getRsBlockTable(typeNumber, errorCorrectionLevel)
if (typeof rsBlock === 'undefined') {
throw new Error('bad rs block @ typeNumber:' + typeNumber +
'/errorCorrectionLevel:' + errorCorrectionLevel)
}
let length = rsBlock.length / 3
const list = []
for (let i = 0; i < length; i += 1) {
let count = rsBlock[i * 3 + 0]
let totalCount = rsBlock[i * 3 + 1]
let dataCount = rsBlock[i * 3 + 2]
for (let j = 0; j < count; j += 1) {
list.push(new Qrrsblock(totalCount, dataCount))
}
}
return list
}
}