Spaces:
Running
Running
Download BeadQR/miniprogram/utils/scancheck.js from Ethscriptions/cut: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/BeadQR/miniprogram/utils/scancheck.js
- Command line
-
hf download hf://spaces/Ethscriptions/cut/BeadQR/miniprogram/utils/scancheck.js
-
curl -L -o scancheck.js https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/BeadQR/miniprogram/utils/scancheck.js
1.36 kB
| // 本地扫码自检:把生成的色号网格渲染成像素,用 jsQR 解码并比对内容 | |
| // jsQR 比微信扫码更“挑剔”,自检通过基本可以放心;未通过时提示用户调整 | |
| const jsQR = require('./vendor/jsqr') | |
| const { hexToRgb } = require('./color') | |
| function selfCheck(artwork, expectedText) { | |
| const { size, cells, pal } = artwork | |
| const px = size > 90 ? 4 : 5 | |
| const margin = 4 | |
| const dim = (size + margin * 2) * px | |
| const data = new Uint8ClampedArray(dim * dim * 4).fill(255) | |
| const colors = pal.hexes.map(hexToRgb) | |
| for (let y = 0; y < size; y += 1) { | |
| for (let x = 0; x < size; x += 1) { | |
| const c = cells[y * size + x] | |
| if (c < 0) continue // 空钉位视为白底 | |
| const [r, g, b] = colors[c] | |
| for (let dy = 0; dy < px; dy += 1) { | |
| let o = (((y + margin) * px + dy) * dim + (x + margin) * px) * 4 | |
| for (let dx = 0; dx < px; dx += 1) { | |
| data[o] = r; data[o + 1] = g; data[o + 2] = b | |
| o += 4 | |
| } | |
| } | |
| } | |
| } | |
| try { | |
| const result = jsQR(data, dim, dim, { inversionAttempts: 'dontInvert' }) | |
| if (!result) return { ok: false, reason: 'unreadable' } | |
| return { ok: result.data === expectedText, reason: result.data === expectedText ? '' : 'mismatch' } | |
| } catch (e) { | |
| return { ok: false, reason: 'error' } | |
| } | |
| } | |
| module.exports = { selfCheck } | |