@qrcode-ai-com/qrcode-barcode-scanner-wasm
Version:
QR and barcode decoding + scannability scoring — one portable browser WASM with camera, tracking and overlay modules, plus optional URL destination checks
500 lines (452 loc) • 20.3 kB
JavaScript
/* @ts-self-types="./qrcode-barcode-scanner.d.ts" */
/**
* The readers compiled into this module: its report `versions`, decode
* `engines` and the `symbologies` they can return (spec/01-report.md
* § Capabilities). Says which readers are included, not that an image
* will decode.
* @returns {any}
*/
export function capabilities() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.capabilities(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Scan a raw RGBA8 frame (`ImageData.data`, width, height). Defaults to the
* `frame` profile (decode-only, tight budget) — pass another to override.
* `budget_ms` overrides the profile's wall-clock budget, in the same
* domain as [`scan_image`]'s. The arguments after it are [`scan_image`]'s
* other options with the same domains and errors, appended so that a
* positional call written for the earlier five arguments keeps its
* meaning: `max_dimension`, `max_pixels`, `score_skip_axes`,
* `score_skip_checks`, `alpha_background` (transparent pixels of the
* frame flatten over it before luma), `alpha_palette`, `score_preset`,
* `formats`.
* @param {Uint8Array} data
* @param {number} width
* @param {number} height
* @param {string | null} [profile]
* @param {number | null} [budget_ms]
* @param {number | null} [max_dimension]
* @param {number | null} [max_pixels]
* @param {string[] | null} [score_skip_axes]
* @param {string[] | null} [score_skip_checks]
* @param {string | null} [alpha_background]
* @param {string[] | null} [alpha_palette]
* @param {string | null} [score_preset]
* @param {string[] | null} [formats]
* @returns {any}
*/
export function scan_frame(data, width, height, profile, budget_ms, max_dimension, max_pixels, score_skip_axes, score_skip_checks, alpha_background, alpha_palette, score_preset, formats) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export);
const len0 = WASM_VECTOR_LEN;
var ptr1 = isLikeNone(profile) ? 0 : passStringToWasm0(profile, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len1 = WASM_VECTOR_LEN;
var ptr2 = isLikeNone(score_skip_axes) ? 0 : passArrayJsValueToWasm0(score_skip_axes, wasm.__wbindgen_export);
var len2 = WASM_VECTOR_LEN;
var ptr3 = isLikeNone(score_skip_checks) ? 0 : passArrayJsValueToWasm0(score_skip_checks, wasm.__wbindgen_export);
var len3 = WASM_VECTOR_LEN;
var ptr4 = isLikeNone(alpha_background) ? 0 : passStringToWasm0(alpha_background, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len4 = WASM_VECTOR_LEN;
var ptr5 = isLikeNone(alpha_palette) ? 0 : passArrayJsValueToWasm0(alpha_palette, wasm.__wbindgen_export);
var len5 = WASM_VECTOR_LEN;
var ptr6 = isLikeNone(score_preset) ? 0 : passStringToWasm0(score_preset, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len6 = WASM_VECTOR_LEN;
var ptr7 = isLikeNone(formats) ? 0 : passArrayJsValueToWasm0(formats, wasm.__wbindgen_export);
var len7 = WASM_VECTOR_LEN;
wasm.scan_frame(retptr, ptr0, len0, width, height, ptr1, len1, !isLikeNone(budget_ms), isLikeNone(budget_ms) ? 0 : budget_ms, !isLikeNone(max_dimension), isLikeNone(max_dimension) ? 0 : max_dimension, !isLikeNone(max_pixels), isLikeNone(max_pixels) ? 0 : max_pixels, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, ptr6, len6, ptr7, len7);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Scan encoded image bytes (PNG/JPEG/WebP/GIF). Profile: full | fast |
* frame. `max_dimension` / `max_pixels` cap the input (whole numbers from
* 1; anything else rejects `[QRS-006]`). `budget_ms` overrides the
* profile's wall-clock budget (the scan runs synchronously on the calling
* thread — bound it in UI contexts; `0`, a negative, `NaN` or `Infinity` =
* unbounded, a fraction floors to whole ms, above 4294967295 rejects
* `[QRS-006]`, unset = the profile default). A report the budget cut
* says so (`trace.budget_exhausted: true`): a `null` score beside
* detections then means "not judged", never 0.
* `score_skip_axes` (wire names, e.g. `["perspective","rotation"]`)
* excludes axes from scoring: their cells never run and the composite
* renormalizes engine-side — the generated-preview integration config.
* `score_skip_checks` (`["uec","iso15415","alpha_envelope"]`) withholds
* report SECTIONS the host does not display: wire carries null, and the
* section-driven hints never fire. The UEC margin is still measured and
* caps the value, so the composite never moves.
* `alpha_background` (`auto` | `white` | `black` | `none` |
* `#rrggbb`) picks the background flattened under transparent pixels —
* `auto` (default) reads the design's own content and retries the
* opposite on a zero-detection scan; opaque inputs are untouched.
* `formats` (wire names, e.g. `["qr_code"]`) restricts the scan to the
* symbologies the host handles: no reader of another symbology runs, and
* the report says what was looked for (`trace.formats`). Unset or empty =
* every symbology; an unknown name rejects `[QRS-006]`.
* @param {Uint8Array} bytes
* @param {string | null} [profile]
* @param {number | null} [max_dimension]
* @param {number | null} [max_pixels]
* @param {number | null} [budget_ms]
* @param {string[] | null} [score_skip_axes]
* @param {string[] | null} [score_skip_checks]
* @param {string | null} [alpha_background]
* @param {string[] | null} [alpha_palette]
* @param {string | null} [score_preset]
* @param {string[] | null} [formats]
* @returns {any}
*/
export function scan_image(bytes, profile, max_dimension, max_pixels, budget_ms, score_skip_axes, score_skip_checks, alpha_background, alpha_palette, score_preset, formats) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export);
const len0 = WASM_VECTOR_LEN;
var ptr1 = isLikeNone(profile) ? 0 : passStringToWasm0(profile, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len1 = WASM_VECTOR_LEN;
var ptr2 = isLikeNone(score_skip_axes) ? 0 : passArrayJsValueToWasm0(score_skip_axes, wasm.__wbindgen_export);
var len2 = WASM_VECTOR_LEN;
var ptr3 = isLikeNone(score_skip_checks) ? 0 : passArrayJsValueToWasm0(score_skip_checks, wasm.__wbindgen_export);
var len3 = WASM_VECTOR_LEN;
var ptr4 = isLikeNone(alpha_background) ? 0 : passStringToWasm0(alpha_background, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len4 = WASM_VECTOR_LEN;
var ptr5 = isLikeNone(alpha_palette) ? 0 : passArrayJsValueToWasm0(alpha_palette, wasm.__wbindgen_export);
var len5 = WASM_VECTOR_LEN;
var ptr6 = isLikeNone(score_preset) ? 0 : passStringToWasm0(score_preset, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len6 = WASM_VECTOR_LEN;
var ptr7 = isLikeNone(formats) ? 0 : passArrayJsValueToWasm0(formats, wasm.__wbindgen_export);
var len7 = WASM_VECTOR_LEN;
wasm.scan_image(retptr, ptr0, len0, ptr1, len1, !isLikeNone(max_dimension), isLikeNone(max_dimension) ? 0 : max_dimension, !isLikeNone(max_pixels), isLikeNone(max_pixels) ? 0 : max_pixels, !isLikeNone(budget_ms), isLikeNone(budget_ms) ? 0 : budget_ms, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, ptr6, len6, ptr7, len7);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Crate version (the `versions.scanner` of every report).
* @returns {string}
*/
export function version() {
let deferred1_0;
let deferred1_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.version(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
deferred1_0 = r0;
deferred1_1 = r1;
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_export3(deferred1_0, deferred1_1, 1);
}
}
function __wbg_get_imports() {
const import0 = {
__proto__: null,
__wbg_Error_92b29b0548f8b746: function(arg0, arg1) {
const ret = Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbg_String_8564e559799eccda: function(arg0, arg1) {
const ret = String(getObject(arg1));
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
},
__wbg___wbindgen_is_undefined_c05833b95a3cf397: function(arg0) {
const ret = getObject(arg0) === undefined;
return ret;
},
__wbg___wbindgen_string_get_b0ca35b86a603356: function(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'string' ? obj : undefined;
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
var len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
},
__wbg___wbindgen_throw_344f42d3211c4765: function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
},
__wbg_getTime_d6f070c088c9b5ed: function(arg0) {
const ret = getObject(arg0).getTime();
return ret;
},
__wbg_new_0_3da9e97f24fc69be: function() {
const ret = new Date();
return addHeapObject(ret);
},
__wbg_new_32b398fb48b6d94a: function() {
const ret = new Array();
return addHeapObject(ret);
},
__wbg_new_da52cf8fe3429cb2: function() {
const ret = new Object();
return addHeapObject(ret);
},
__wbg_now_e7c6795a7f81e10f: function(arg0) {
const ret = getObject(arg0).now();
return ret;
},
__wbg_performance_3fcf6e32a7e1ed0a: function(arg0) {
const ret = getObject(arg0).performance;
return addHeapObject(ret);
},
__wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
},
__wbg_set_8a16b38e4805b298: function(arg0, arg1, arg2) {
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
},
__wbg_static_accessor_GLOBAL_4ef717fb391d88b7: function() {
const ret = typeof global === 'undefined' ? null : global;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
},
__wbg_static_accessor_GLOBAL_THIS_8d1badc68b5a74f4: function() {
const ret = typeof globalThis === 'undefined' ? null : globalThis;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
},
__wbg_static_accessor_SELF_146583524fe1469b: function() {
const ret = typeof self === 'undefined' ? null : self;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
},
__wbg_static_accessor_WINDOW_f2829a2234d7819e: function() {
const ret = typeof window === 'undefined' ? null : window;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
},
__wbindgen_cast_0000000000000001: function(arg0) {
// Cast intrinsic for `F64 -> Externref`.
const ret = arg0;
return addHeapObject(ret);
},
__wbindgen_cast_0000000000000002: function(arg0, arg1) {
// Cast intrinsic for `Ref(String) -> Externref`.
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
},
__wbindgen_object_clone_ref: function(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
},
__wbindgen_object_drop_ref: function(arg0) {
takeObject(arg0);
},
};
return {
__proto__: null,
"./qrcode-barcode-scanner_bg.js": import0,
};
}
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
function dropObject(idx) {
if (idx < 1028) return;
heap[idx] = heap_next;
heap_next = idx;
}
let cachedDataViewMemory0 = null;
function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}
function getStringFromWasm0(ptr, len) {
return decodeText(ptr >>> 0, len);
}
let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}
function getObject(idx) { return heap[idx]; }
let heap = new Array(1024).fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function isLikeNone(x) {
return x === undefined || x === null;
}
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function passArrayJsValueToWasm0(array, malloc) {
const ptr = malloc(array.length * 4, 4) >>> 0;
const mem = getDataViewMemory0();
for (let i = 0; i < array.length; i++) {
mem.setUint32(ptr + 4 * i, addHeapObject(array[i]), true);
}
WASM_VECTOR_LEN = array.length;
return ptr;
}
function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len, 1) >>> 0;
const mem = getUint8ArrayMemory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = cachedTextEncoder.encodeInto(arg, view);
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
const MAX_SAFARI_DECODE_BYTES = 2146435072;
let numBytesDecoded = 0;
function decodeText(ptr, len) {
numBytesDecoded += len;
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
numBytesDecoded = len;
}
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
const cachedTextEncoder = new TextEncoder();
if (!('encodeInto' in cachedTextEncoder)) {
cachedTextEncoder.encodeInto = function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
};
}
let WASM_VECTOR_LEN = 0;
let wasmModule, wasmInstance, wasm;
function __wbg_finalize_init(instance, module) {
wasmInstance = instance;
wasm = instance.exports;
wasmModule = module;
cachedDataViewMemory0 = null;
cachedUint8ArrayMemory0 = null;
return wasm;
}
async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
const validResponse = module.ok && expectedResponseType(module.type);
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
} else { throw e; }
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
function expectedResponseType(type) {
switch (type) {
case 'basic': case 'cors': case 'default': return true;
}
return false;
}
}
function initSync(module) {
if (wasm !== undefined) return wasm;
if (module !== undefined) {
if (Object.getPrototypeOf(module) === Object.prototype) {
({module} = module)
} else {
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
}
}
const imports = __wbg_get_imports();
if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}
const instance = new WebAssembly.Instance(module, imports);
return __wbg_finalize_init(instance, module);
}
async function __wbg_init(module_or_path) {
if (wasm !== undefined) return wasm;
if (module_or_path !== undefined) {
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
({module_or_path} = module_or_path)
} else {
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
}
}
if (module_or_path === undefined) {
module_or_path = new URL('qrcode-barcode-scanner_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
module_or_path = fetch(module_or_path);
}
const { instance, module } = await __wbg_load(await module_or_path, imports);
return __wbg_finalize_init(instance, module);
}
export { initSync, __wbg_init as default };