1// Reading a QR symbol back with jsQR, a decoder that shares nothing with2// the encoder, so it proves a phone can read it.3import jsQR from "jsqr";45// scan rasterizes a module grid and reads it.6export function scan(size: number, dark: (x: number, y: number) => boolean): string | undefined {7 const px = 4, margin = 4, n = size + margin * 2, w = n * px;8 const img = new Uint8ClampedArray(w * w * 4).fill(255);9 for (let y = 0; y < size; y++) {10 for (let x = 0; x < size; x++) {11 if (!dark(x, y)) continue;12 for (let dy = 0; dy < px; dy++) for (let dx = 0; dx < px; dx++) {13 const i = (((y + margin) * px + dy) * w + (x + margin) * px + dx) * 4;14 img[i] = img[i + 1] = img[i + 2] = 0;15 }16 }17 }18 return jsQR(img, w, w)?.data;19}