This commit is contained in:
iqudoo
2026-04-30 15:35:30 +08:00
parent 0d1698439e
commit 51738b1936
9 changed files with 102 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
import './polyfill';
import { setConfig, getVersion } from './services/config';
import { onScanListener, offScanListener, setStatusListener, getStatus, startScan, stopScan, clear } from './services/provider/scan';
import { onScanListener, offScanListener, setStatusListener, getStatus, startScan, stopScan, scanImage, clear } from './services/provider/scan';
import { initWxJssdk } from './services/wx';
import { printDebug } from './utils/logger';
@@ -41,5 +41,6 @@ export default Object.assign({}, {
getStatus,
startScan,
stopScan,
scanImage,
clear,
});

View File

@@ -3,7 +3,7 @@ import _global from './polyfill/_global';
import { exportSDK } from './_export';
const IScan = exportSDK(core, null, "config", "setStatusListener", "onScanListener",
"offScanListener", "stopScan", "startScan", "clear");
"offScanListener", "stopScan", "startScan", "scanImage", "clear");
function dispatchIScanReady() {
_global.__IScanReady__ && _global.__IScanReady__();

View File

@@ -344,4 +344,18 @@ export function startScan() {
_scan_resolve = null;
__closed();
});
}
export function scanImage() {
if (!isSupportImageScan()) {
console.log("not support image scanner");
return;
}
startScanForImage().then(resp => {
if (resp && resp.result) {
__result(resp.result);
}
}).catch(err => {
console.log("scan image error", err);
});
}

View File

@@ -8,6 +8,9 @@ const scanWeb = {
}
const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio;
const BARCODE_DETECTOR_POLYFILL_URL = "https://fastly.jsdelivr.net/npm/barcode-detector@3/dist/iife/index.min.js";
let barcodeDetectorPolyfillPromise = null;
function removeEl(id) {
try {
@@ -63,22 +66,66 @@ function getBarcodeFormats(scanType) {
return formats;
}
function getBarcodeDetectorClass() {
if (typeof BarcodeDetector !== 'undefined') {
return BarcodeDetector;
}
if (typeof window !== 'undefined'
&& window.BarcodeDetectionAPI
&& window.BarcodeDetectionAPI.BarcodeDetector) {
return window.BarcodeDetectionAPI.BarcodeDetector;
}
return null;
}
function loadBarcodeDetectorPolyfill() {
if (getBarcodeDetectorClass()) {
return Promise.resolve(getBarcodeDetectorClass());
}
if (barcodeDetectorPolyfillPromise) {
return barcodeDetectorPolyfillPromise;
}
barcodeDetectorPolyfillPromise = new Promise((resolve, reject) => {
if (typeof document === 'undefined') {
reject(new Error("BarcodeDetector is not supported"));
return;
}
const scriptUrl = getConfig("webBarcodeDetectorPolyfillUrl") || BARCODE_DETECTOR_POLYFILL_URL;
const script = document.createElement("script");
script.src = scriptUrl;
script.onload = () => {
const BarcodeDetectorClass = getBarcodeDetectorClass();
if (BarcodeDetectorClass) {
resolve(BarcodeDetectorClass);
} else {
reject(new Error("BarcodeDetector polyfill is not ready"));
}
};
script.onerror = reject;
document.head.appendChild(script);
}).catch(err => {
barcodeDetectorPolyfillPromise = null;
throw err;
});
return barcodeDetectorPolyfillPromise;
}
function createBarcodeDetector(scanType) {
return Promise.resolve().then(() => {
if (typeof BarcodeDetector === 'undefined') {
return loadBarcodeDetectorPolyfill().then(BarcodeDetectorClass => {
if (!BarcodeDetectorClass) {
throw new Error("BarcodeDetector is not supported");
}
const formats = getBarcodeFormats(scanType);
if (BarcodeDetector.getSupportedFormats) {
return BarcodeDetector.getSupportedFormats().then(supportedFormats => {
if (BarcodeDetectorClass.getSupportedFormats) {
return BarcodeDetectorClass.getSupportedFormats().then(supportedFormats => {
const supported = formats.filter(format => supportedFormats.indexOf(format) !== -1);
if (!supported.length) {
throw new Error("No supported barcode formats");
}
return new BarcodeDetector({ formats: supported });
return new BarcodeDetectorClass({ formats: supported });
});
}
return new BarcodeDetector({ formats });
return new BarcodeDetectorClass({ formats });
});
}
@@ -126,12 +173,11 @@ export function isSupportWebScan() {
return typeof navigator !== 'undefined'
&& navigator.mediaDevices
&& navigator.mediaDevices.getUserMedia
&& typeof BarcodeDetector !== 'undefined';
&& !!getBarcodeDetectorClass();
}
export function isSupportImageScan() {
return typeof document !== 'undefined'
&& typeof BarcodeDetector !== 'undefined'
&& typeof URL !== 'undefined'
&& URL.createObjectURL;
}