let ws = plus.webview.create("./camera.html", "camera");
ws.show();
ws.addEventListener(
"loaded",
function() {
//页面加载完成后才显示
setTimeout(function() {
ws.show();
}, 200);
},
false
);
ws.addEventListener(
"close",
function() {
ws = null;
},
false
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>camera</title>
<style>
html,
body,
div,
span,
img {
margin: 0;
padding: 0;
}
body {
background: #000;
}
.tips {
margin-top: 50%;
color: #fff;
text-align: center
}
.action {
position: fixed;
z-index: 777;
width: 100%;
left: 0;
bottom: 0;
}
.action .items {
display: flex;
justify-content: space-around;
background: rgba(0, 0, 0, 0.35);
width: 60%;
padding: 4px;
margin: 4px auto;
}
.action .items .item {
flex-basis: 50px;
text-align: center;
}
.action .items img {
width: 27px;
}
</style>
</head>
<body>
<div id="camera">
<div id="scan"></div>
<div class="tips">加载中...</div>
<div class="action">
<div class="items">
<div class="item" onclick="openLight"><img src="./src/assets/img/png-60@3x.png"></div>
<div class="item" onclick="getPicture"><img src="./src/assets/img/png-59@3x.png"></div>
<!-- <div
class="item"
@click="showInput"
><img src="../assets/img/png-68@3x.png">
</div> -->
<div class="item" onclick="cancelScan"><img src="./src/assets/img/png-61@3x.png"></div>
<!-- <d class="item"><img src="../../assets/img/png-25@3x.png"></d -->
</div>
</div>
</div>
</body>
<script>
var isLight = false, scan = null;
// 打开闪光灯
function openLight() {
isLight = !isLight;
scan.setFlash(isLight);
}
//创建扫描控件
function startRecognize() {
if (!window.plus) return;
scan = null;
scan = new plus.barcode.Barcode(
"scan",
[plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
{
frameColor: "#1294cb",
scanbarColor: "#1294cb",
top: "100px",
left: "0px",
width: "100%",
height: "500px",
position: "fixed"
}
);
// 条码识别成功
scan.onmarked = onmarked;
function onmarked(type, result, file) {
result = result.replace(/\n/g, "");
localStorage.setItem("cameraData", result);
let ws = plus.webview.getWebviewById("camera");
ws.close()
}
}
// //开始扫描
function startScan() {
if (!window.plus) return;
startRecognize(); //创建控件
setTimeout(() => {
scan.start();
}, 100);
}
// 取消扫描
function cancelScan() {
if (!window.plus) return;
plus.navigator.setStatusBarStyle("dark");
if (scan) {
scan.cancel(); //关闭扫描
scan.close(); //关闭条码识别控件
}
let ws = plus.webview.getWebviewById("camera");
ws.close()
}
// 从相册选取二维码相片
function getPicture() {
plus.gallery.pick(src => {
// scan.cancel(); //关闭扫描
plus.barcode.scan(
src,
(type, result) => {
scan.cancel(); //关闭扫描
scan.close();
localStorage.setItem("cameraData", result);
plus.navigator.setStatusBarStyle("dark");
let ws = plus.webview.getWebviewById("camera");
ws.close()
}
);
});
}
window.onload = function () {
setTimeout(() => {
plus.navigator.setStatusBarStyle("dark");
startScan()
}, 400);
}
</script>
</html>