优化:支持控制是否显示

This commit is contained in:
iqudoo
2026-04-30 11:17:05 +08:00
parent 8ebe8b0b34
commit f3c0856506
11 changed files with 142 additions and 26 deletions

View File

@@ -1,11 +1,14 @@
import { createUUID } from "../../utils/uuid";
import { getConfig } from "../config";
import scanBeepAudio from "../../../res/scan_beep.ogg";
const scanWeb = {
uuid: null,
finish: true
}
const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio;
function removeEl(id) {
try {
let el = document.getElementById(id);
@@ -101,6 +104,24 @@ function drawBarcode(context, width, barcode) {
}
}
function playScanBeep() {
if (getConfig("webScanBeepEnabled") === false) {
return;
}
const audioSrc = getConfig("webScanBeepAudio") || DEFAULT_SCAN_BEEP_AUDIO;
if (!audioSrc || typeof Audio === 'undefined') {
return;
}
try {
const audio = new Audio(audioSrc);
const playPromise = audio.play();
playPromise && playPromise.catch && playPromise.catch(() => {
// no thing
});
} catch (e) {
}
}
export function isSupportWebScan() {
return typeof navigator !== 'undefined'
&& navigator.mediaDevices
@@ -184,7 +205,7 @@ function detectImageFile(detector, file) {
}
export function startScanForImage() {
return createBarcodeDetector(getConfig("scanType")).then(detector => {
return createBarcodeDetector(getConfig("webScanType")).then(detector => {
return chooseImageFile().then(file => detectImageFile(detector, file));
}).then(code => {
if (code && code.rawValue) {
@@ -207,6 +228,7 @@ export function startScanForWeb(canvasStyle, onResult) {
let videoEl = createEl("video",
"__webscan_video__",
"display: none", false);
let canvasEnabled = getConfig("webCanvasEnabled") !== false;
let canvasDisplay = "";
let canvasBaseStyle = canvasStyle || "position: fixed; width: 300px; height: 240px; top: 0; left: 0; z-index: 9999;";
let canvasEl = createEl("canvas",
@@ -221,7 +243,7 @@ export function startScanForWeb(canvasStyle, onResult) {
videoEl.width = 300;
videoEl.height = 300;
videoEl.uuid = scanWeb.uuid;
createBarcodeDetector(getConfig("scanType")).then(detector => {
createBarcodeDetector(getConfig("webScanType")).then(detector => {
return navigator.mediaDevices.getUserMedia({
video: {
facingMode: "environment"
@@ -238,15 +260,6 @@ export function startScanForWeb(canvasStyle, onResult) {
videoEl.srcObject = stream;
videoEl.setAttribute("playsinline", true); // iOS使用
videoEl.play();
let closeWebScan = getConfig("closeWebScan", () => {
// no thing
});
let displayWebScan = getConfig("displayWebScan", (canvasEl, cancal) => {
// no thing
});
displayWebScan && displayWebScan(canvasEl, () => {
stopScanForWeb();
});
canvasEl.style.display = "none";
let detecting = false;
let displayed = false;
@@ -259,7 +272,6 @@ export function startScanForWeb(canvasStyle, onResult) {
try {
stream.getTracks()[0].stop();
} catch (_e) { }
closeWebScan && closeWebScan();
};
let tick = () => {
try {
@@ -269,7 +281,7 @@ export function startScanForWeb(canvasStyle, onResult) {
context.setTransform(-1, 0, 0, 1, canvasEl.width, 0);
context.drawImage(videoEl, 0, 0, canvasEl.width, canvasEl.height);
context.setTransform(1, 0, 0, 1, 0, 0);
if (!displayed) {
if (canvasEnabled && !displayed) {
displayed = true;
canvasEl.style.display = canvasDisplay || "";
}
@@ -277,10 +289,11 @@ export function startScanForWeb(canvasStyle, onResult) {
detector.detect(videoEl).then(barcodes => {
const code = barcodes && barcodes[0];
if (code && code.rawValue && scanWeb.uuid == currentUuid) {
if (onResult && !onResult(code.rawValue)) {
if (!onResult || !onResult(code.rawValue)) {
return;
}
drawBarcode(context, canvasEl.width, code);
playScanBeep();
scanWeb.uuid = null;
scanWeb.finish = true;
close();