diff --git a/frontend/index.html b/frontend/index.html index 96161ab..a9d2326 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -18,7 +18,7 @@ - + diff --git a/frontend/js/chzn.js b/frontend/js/chzn.js index 42a6ed2..c60f23a 100644 --- a/frontend/js/chzn.js +++ b/frontend/js/chzn.js @@ -270,9 +270,40 @@ let ChZ = (function () { return prefill; } + // ------------------------------------------------------------------ + // DataMatrix decoding via bundled zxing-wasm (ZXing-C++). Far better at + // LOCATING codes than the pure-JS port. The .wasm is fetched once as an + // arrayBuffer and handed to the module (wasmBinary) — no streaming/MIME issues. + // ------------------------------------------------------------------ + let _zxReadyP = null; + function zxReady() { + if (_zxReadyP) return _zxReadyP; + _zxReadyP = (async () => { + if (typeof ZXingWASM === "undefined") throw new Error("ZXingWASM не загружен (пересобери приложение)"); + let buf = await (await fetch("lib/zxing_reader.wasm")).arrayBuffer(); + await ZXingWASM.prepareZXingModule({ overrides: { wasmBinary: new Uint8Array(buf) }, fireImmediately: true }); + return true; + })(); + return _zxReadyP; + } + + // Decode one ImageData -> raw GS1 code string (with GS 0x1D), or null. + async function decodeImageData(imageData, formats) { + let res = await ZXingWASM.readBarcodes(imageData, { + formats: formats || ["DataMatrix"], + tryHarder: true, tryRotate: true, tryInvert: true, maxNumberOfSymbols: 1, + }); + if (!res || !res.length || !res[0].isValid) return null; + let r = res[0]; + // raw payload from bytes keeps the GS separators; fall back to text + if (r.bytes && r.bytes.length) return String.fromCharCode.apply(null, r.bytes); + return r.text || null; + } + return { getSession, setSession, hasSession, importFromText, refresh, ensureSid, check, nutritionInfo, normalizeCode, extractGtin, parseCard, resolve, + zxReady, decodeImageData, _raw: raw, }; })(); diff --git a/frontend/js/main.js b/frontend/js/main.js index c14b292..02d42c2 100644 --- a/frontend/js/main.js +++ b/frontend/js/main.js @@ -1844,16 +1844,16 @@ function showAddProductModal(onSaved, prefill) { }); } -// Честный знак scanner: camera + photo/manual, decoded by bundled ZXing -// (frontend/lib/zxing.js) — fully offline, no Google Play Services (the native -// BarcodeDetector needs GMS and is absent on de-Googled ROMs). On a decoded -// DataMatrix -> ChZ.resolve -> prefill the new-product modal. +// Честный знак scanner: camera + photo/manual, decoded by bundled zxing-wasm +// (ZXing-C++: frontend/lib/zxing-wasm.js + zxing_reader.wasm) — fully offline, +// no Google Play Services, and far better at LOCATING a DataMatrix than the +// pure-JS port. Live frames are cropped to the central ROI and upscaled for +// speed/reliability. On decode -> ChZ.resolve -> prefill the new-product modal. function showChznScanModal(onSaved) { if (!ChZ.hasSession()) { appAlert("Сессия Честного знака не импортирована.\nНастройки → Честный знак → Импорт сессии (файл chzn_session.json из tools/chzn_export.sh)."); return; } - let hasZX = (typeof ZXing !== "undefined" && ZXing.BrowserDatamatrixCodeReader); let ov = document.createElement("div"); ov.className = "modal-overlay"; @@ -1864,7 +1864,7 @@ function showChznScanModal(onSaved) {