149 lines
4.5 KiB
HTML
149 lines
4.5 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
|
|
<title>扫码</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
html, body { width: 100%; height: 100%; overflow: hidden; background: #000; }
|
|
#scanner-container {
|
|
position: fixed;
|
|
top: 0; left: 0;
|
|
width: 100%; height: 100%;
|
|
background: #000;
|
|
}
|
|
#scanner-container video {
|
|
width: 100% !important;
|
|
height: 100% !important;
|
|
object-fit: cover;
|
|
position: absolute;
|
|
top: 0; left: 0;
|
|
}
|
|
#scanner-container canvas { display: none !important; }
|
|
.close-btn {
|
|
position: fixed; top: 30px; left: 15px;
|
|
width: 40px; height: 40px;
|
|
border-radius: 50%;
|
|
background: rgba(255,255,255,0.2);
|
|
display: flex; align-items: center; justify-content: center;
|
|
z-index: 9999;
|
|
color: #fff; font-size: 22px;
|
|
}
|
|
.tip {
|
|
position: fixed; bottom: 60px; left: 0; right: 0;
|
|
text-align: center; z-index: 9999;
|
|
}
|
|
.tip-text {
|
|
display: inline-block;
|
|
color: #fff; font-size: 14px;
|
|
background: rgba(0,0,0,0.5);
|
|
padding: 6px 16px; border-radius: 20px;
|
|
}
|
|
.loading {
|
|
position: fixed; top: 50%; left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
color: #fff; font-size: 14px; z-index: 9999;
|
|
text-align: center;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="scanner-container"></div>
|
|
<div class="close-btn" id="closeBtn">✕</div>
|
|
<div class="tip"><span class="tip-text" id="tipText">对准条码自动识别</span></div>
|
|
<div class="loading" id="loadingText">正在启动摄像头...</div>
|
|
|
|
<script src="../js/quagga.min.js"></script>
|
|
<script>
|
|
var detected = false;
|
|
var tipText = document.getElementById('tipText');
|
|
var loadingText = document.getElementById('loadingText');
|
|
|
|
function sendToApp(action, code) {
|
|
// UniApp WebView postMessage
|
|
if (window.uni && window.uni.postMessage) {
|
|
window.uni.postMessage({ data: { action: action, code: code || '' } });
|
|
}
|
|
// fallback: 5+ webview API
|
|
if (window.plus && window.plus.webview) {
|
|
var wv = plus.webview.currentWebview();
|
|
if (wv && wv.postMessageToParent) {
|
|
wv.postMessageToParent({ action: action, code: code || '' });
|
|
}
|
|
}
|
|
}
|
|
|
|
function closeCamera() {
|
|
try { Quagga.stop(); } catch(e) {}
|
|
sendToApp('close');
|
|
}
|
|
|
|
document.getElementById('closeBtn').addEventListener('click', closeCamera);
|
|
|
|
function startQuagga() {
|
|
if (typeof Quagga === 'undefined') {
|
|
loadingText.innerText = '加载解码库失败';
|
|
return;
|
|
}
|
|
Quagga.init({
|
|
inputStream: {
|
|
name: 'Live',
|
|
type: 'LiveStream',
|
|
target: document.querySelector('#scanner-container'),
|
|
constraints: {
|
|
width: 640, height: 480, facingMode: 'environment'
|
|
}
|
|
},
|
|
decoder: {
|
|
readers: ['ean_reader', 'ean_8_reader', 'upc_reader', 'code_128_reader', 'code_39_reader'],
|
|
debug: false
|
|
},
|
|
locator: { halfSample: false, patchSize: 'large' },
|
|
numOfWorkers: 2,
|
|
frequency: 5
|
|
}, function(err) {
|
|
if (err) {
|
|
loadingText.innerText = '摄像头启动失败,请检查权限';
|
|
console.error('Quagga error:', err);
|
|
return;
|
|
}
|
|
loadingText.style.display = 'none';
|
|
Quagga.start();
|
|
// 启动成功后定期检查有没有画面
|
|
setTimeout(function() {
|
|
var video = document.querySelector('#scanner-container video');
|
|
if (video && video.videoWidth > 0) {
|
|
console.log('Camera OK:', video.videoWidth, 'x', video.videoHeight);
|
|
} else {
|
|
console.warn('Camera may not have started');
|
|
}
|
|
}, 2000);
|
|
});
|
|
}
|
|
|
|
Quagga.onDetected(function(result) {
|
|
if (detected) return;
|
|
var code = result && result.codeResult && result.codeResult.code;
|
|
if (!code) return;
|
|
code = code.replace(/\D/g, '');
|
|
if (code.length >= 10 && code.length <= 14) {
|
|
detected = true;
|
|
tipText.innerText = '识别成功';
|
|
try { Quagga.stop(); } catch(e) {}
|
|
setTimeout(function() {
|
|
sendToApp('scanned', code);
|
|
}, 300);
|
|
}
|
|
});
|
|
|
|
// 启动
|
|
if (document.readyState === 'complete') {
|
|
startQuagga();
|
|
} else {
|
|
window.addEventListener('load', startQuagga);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|