扫码页:改用plus.barcode原生扫码(全透明无取景框)
This commit is contained in:
parent
6b4e082cd1
commit
ff9905948c
@ -1,148 +1 @@
|
||||
<!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>
|
||||
deprecated - use scan-isbn.nvue
|
||||
6
hybrid/js/quagga.min.js
vendored
6
hybrid/js/quagga.min.js
vendored
File diff suppressed because one or more lines are too long
@ -58,7 +58,10 @@
|
||||
"path": "pages/upload/scan-isbn",
|
||||
"style": {
|
||||
"navigationBarTitleText": "",
|
||||
"navigationStyle": "custom"
|
||||
"navigationStyle": "custom",
|
||||
"app-plus": {
|
||||
"render": "native"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
105
pages/upload/scan-isbn.nvue
Normal file
105
pages/upload/scan-isbn.nvue
Normal file
@ -0,0 +1,105 @@
|
||||
<template>
|
||||
<view class="page">
|
||||
<view ref="barcodeContainer" class="barcode-container"></view>
|
||||
<view class="close-btn" @click="goBack">
|
||||
<text class="close-icon">✕</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
barcode: null,
|
||||
scanned: false
|
||||
}
|
||||
},
|
||||
onReady() {
|
||||
this.initBarcode()
|
||||
},
|
||||
onUnload() {
|
||||
this.destroyBarcode()
|
||||
},
|
||||
methods: {
|
||||
initBarcode() {
|
||||
if (!window.plus) {
|
||||
uni.showToast({ title: '扫码初始化失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
// 创建原生条码扫描控件,全透明无取景框
|
||||
var options = {
|
||||
top: '0px',
|
||||
left: '0px',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
frameColor: '#00000000',
|
||||
scanbarColor: '#00000000',
|
||||
background: '#00000000'
|
||||
}
|
||||
try {
|
||||
this.barcode = new plus.barcode.Barcode('barcodeContainer', [plus.barcode.EAN13, plus.barcode.EAN8, plus.barcode.UPCA, plus.barcode.CODE128], options)
|
||||
this.barcode.onmarked = (type, result) => {
|
||||
if (this.scanned) return
|
||||
this.scanned = true
|
||||
if (result && result.length >= 10 && result.length <= 14) {
|
||||
this.barcode.cancel()
|
||||
uni.$emit('scan-isbn-result', result)
|
||||
uni.navigateBack()
|
||||
} else {
|
||||
this.scanned = false
|
||||
}
|
||||
}
|
||||
this.barcode.onerror = (e) => {
|
||||
console.error('扫码错误:', e)
|
||||
uni.showToast({ title: '扫码异常', icon: 'none' })
|
||||
}
|
||||
this.barcode.start()
|
||||
} catch (e) {
|
||||
console.error('Barcode init error:', e)
|
||||
uni.showToast({ title: '扫码初始化失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
destroyBarcode() {
|
||||
if (this.barcode) {
|
||||
try { this.barcode.cancel() } catch(e) {}
|
||||
try { this.barcode.close() } catch(e) {}
|
||||
this.barcode = null
|
||||
}
|
||||
},
|
||||
goBack() {
|
||||
this.destroyBarcode()
|
||||
uni.navigateBack()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
flex: 1;
|
||||
background-color: #000;
|
||||
position: relative;
|
||||
}
|
||||
.barcode-container {
|
||||
position: absolute;
|
||||
top: 0; left: 0; right: 0; bottom: 0;
|
||||
}
|
||||
.close-btn {
|
||||
position: fixed;
|
||||
top: 60rpx;
|
||||
left: 30rpx;
|
||||
width: 60rpx;
|
||||
height: 60rpx;
|
||||
border-radius: 50%;
|
||||
background: rgba(255,255,255,0.2);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 9999;
|
||||
}
|
||||
.close-icon {
|
||||
color: #fff;
|
||||
font-size: 36rpx;
|
||||
}
|
||||
</style>
|
||||
@ -1,33 +0,0 @@
|
||||
<template>
|
||||
<view class="page">
|
||||
<web-view src="/hybrid/html/scanner.html" @message="handleMessage"></web-view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {}
|
||||
},
|
||||
methods: {
|
||||
handleMessage(e) {
|
||||
const data = e.detail && e.detail.data && e.detail.data[0]
|
||||
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') {
|
||||
uni.navigateBack()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
flex: 1;
|
||||
background-color: #000;
|
||||
}
|
||||
</style>
|
||||
Loading…
Reference in New Issue
Block a user