扫码页:改.vue页面+本地quagga2库,无蒙版实时扫码

This commit is contained in:
97694732@qq.com 2026-06-04 16:11:05 +08:00
parent 8ca9ac9ff9
commit 6b4e082cd1
4 changed files with 51 additions and 99 deletions

View File

@ -20,60 +20,32 @@
position: absolute;
top: 0; left: 0;
}
/* 隐藏 quagga 的取景框 canvas */
#scanner-container canvas {
display: none !important;
}
/* 关闭按钮 */
#scanner-container canvas { display: none !important; }
.close-btn {
position: fixed;
top: 60rpx;
left: 30rpx;
width: 60rpx;
height: 60rpx;
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;
display: flex; align-items: center; justify-content: center;
z-index: 9999;
color: #fff;
font-size: 36rpx;
color: #fff; font-size: 22px;
}
/* tip */
.tip {
position: fixed;
bottom: 120rpx;
left: 0;
right: 0;
text-align: center;
z-index: 9999;
position: fixed; bottom: 60px; left: 0; right: 0;
text-align: center; z-index: 9999;
}
.tip-text {
display: inline-block;
color: #fff;
font-size: 28rpx;
color: #fff; font-size: 14px;
background: rgba(0,0,0,0.5);
padding: 12rpx 30rpx;
border-radius: 40rpx;
padding: 6px 16px; border-radius: 20px;
}
/* 加载/错误提示 */
.loading {
position: fixed;
top: 50%;
left: 50%;
position: fixed; top: 50%; left: 50%;
transform: translate(-50%, -50%);
color: #fff;
font-size: 28rpx;
z-index: 9999;
color: #fff; font-size: 14px; z-index: 9999;
text-align: center;
}
/* 适配 rpx 单位:假设 750rpx = 屏幕宽 */
@media (min-width: 400px) {
.close-btn { width: 40px; height: 40px; font-size: 22px; top: 40px; left: 20px; }
.tip-text { font-size: 16px; padding: 8px 20px; border-radius: 28px; }
.tip { bottom: 80px; }
}
</style>
</head>
<body>
@ -82,17 +54,24 @@
<div class="tip"><span class="tip-text" id="tipText">对准条码自动识别</span></div>
<div class="loading" id="loadingText">正在启动摄像头...</div>
<script src="https://cdn.jsdelivr.net/npm/quagga2/dist/quagga.min.js"></script>
<script src="../js/quagga.min.js"></script>
<script>
var detected = false;
var closeBtn = document.getElementById('closeBtn');
var loadingText = document.getElementById('loadingText');
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() {
@ -100,50 +79,46 @@
sendToApp('close');
}
closeBtn.addEventListener('click', closeCamera);
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: { ideal: 640 },
height: { ideal: 480 },
facingMode: 'environment'
width: 640, height: 480, facingMode: 'environment'
}
},
decoder: {
readers: [
'ean_reader',
'ean_8_reader',
'upc_reader',
'code_128_reader',
'code_39_reader'
],
readers: ['ean_reader', 'ean_8_reader', 'upc_reader', 'code_128_reader', 'code_39_reader'],
debug: false
},
locator: {
halfSample: false,
patchSize: 'large'
},
locator: { halfSample: false, patchSize: 'large' },
numOfWorkers: 2,
frequency: 5
}, function(err) {
if (err) {
loadingText.innerText = '摄像头启动失败,请检查权限';
console.error('Quagga init error:', err);
setTimeout(function() {
loadingText.innerText = '点击返回';
closeBtn.addEventListener('click', function() {
sendToApp('close');
});
}, 2000);
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);
});
}
@ -151,48 +126,23 @@
if (detected) return;
var code = result && result.codeResult && result.codeResult.code;
if (!code) return;
// ISBN 通常是 13 位数字,但也接受其他格式
code = code.replace(/\D/g, '');
if (code.length === 13 || code.length === 12 || code.length === 10) {
if (code.length >= 10 && code.length <= 14) {
detected = true;
tipText.innerText = '识别成功';
Quagga.stop();
try { Quagga.stop(); } catch(e) {}
setTimeout(function() {
sendToApp('scanned', code);
}, 300);
}
});
// 等待 UniApp 桥接就绪后启动
function waitForBridge() {
if (window.uni && window.uni.postMessage) {
startQuagga();
} else {
document.addEventListener('UniAppJSBridgeReady', function() {
startQuagga();
});
// 降级2秒后直接启动
setTimeout(function() {
if (typeof Quagga !== 'undefined') {
startQuagga();
}
}, 2000);
}
}
// 页面加载完成后启动
// 启动
if (document.readyState === 'complete') {
waitForBridge();
startQuagga();
} else {
window.addEventListener('load', waitForBridge);
window.addEventListener('load', startQuagga);
}
// 页面不可见时停止摄像头节省资源(可选)
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
try { Quagga.stop(); } catch(e) {}
}
});
</script>
</body>
</html>

6
hybrid/js/quagga.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -58,10 +58,7 @@
"path": "pages/upload/scan-isbn",
"style": {
"navigationBarTitleText": "",
"navigationStyle": "custom",
"app-plus": {
"render": "native"
}
"navigationStyle": "custom"
}
}
],

View File

@ -15,7 +15,6 @@
if (!data) return
const action = data.action || ''
if (action === 'scanned' && data.code) {
//
uni.$emit('scan-isbn-result', data.code)
uni.navigateBack()
} else if (action === 'close') {