可以实现,使用这个插件html5-qrcode就能实现
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>二维码扫描器</title>
<!-- 引入 html5-qrcode.js -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/html5-qrcode.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
background-color: #f0f0f0;
}
#qr-reader {
width: 100%;
max-width: 500px;
margin-bottom: 20px;
}
#qr-reader__dashboard_section {
border: 2px solid #007bff;
border-radius: 10px;
overflow: hidden;
}
#start-button, #stop-button {
padding: 10px 20px;
font-size: 16px;
margin: 5px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
}
#stop-button {
background-color: #dc3545;
}
#start-button:hover, #stop-button:hover {
opacity: 0.9;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: white;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
word-break: break-all;
min-height: 20px;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<h1>二维码扫描器</h1>
<!-- 扫描区域容器 -->
<div id="qr-reader"></div>
<!-- 控制按钮 -->
<button id="start-button">开始扫描</button>
<button id="stop-button" class="hidden">停止扫描</button>
<!-- 扫描结果 -->
<div id="result">扫描结果将显示在这里</div>
<script>
// 初始化变量
let html5QrCode = null;
const qrReaderId = "qr-reader";
const startButton = document.getElementById("start-button");
const stopButton = document.getElementById("stop-button");
const resultDiv = document.getElementById("result");
// 成功扫描回调函数
function onScanSuccess(decodedText, decodedResult) {
resultDiv.innerHTML = `扫描成功: ${decodedText}`;
console.log(`扫描结果: ${decodedText}`, decodedResult);
// 可选:扫描成功后停止摄像头
// stopScanning();
}
// 扫描失败回调函数(可选)
function onScanFailure(error) {
// 扫描失败时的处理(通常是未检测到二维码)
// console.warn(`扫描失败: ${error}`);
}
// 开始扫描函数
function startScanning() {
// 如果已经存在实例,先清除
if (html5QrCode) {
stopScanning();
}
// 创建新的 Html5Qrcode 实例
html5QrCode = new Html5Qrcode(qrReaderId);
// 配置参数
const config = {
fps: 10, // 每秒尝试解码的帧数
qrbox: { // 二维码扫描框的尺寸
width: 250,
height: 250
},
// 只支持某些摄像头(可选)
// aspectRatio: 1.0 // 正方形比例
};
// 开始扫描
html5QrCode.start(
{ facingMode: "environment" }, // 优先使用后置摄像头
config,
onScanSuccess,
onScanFailure
).then(() => {
// 成功启动摄像头
resultDiv.innerHTML = "摄像头已启动,正在扫描...";
startButton.classList.add("hidden");
stopButton.classList.remove("hidden");
}).catch((err) => {
// 启动失败
console.error("无法启动摄像头", err);
resultDiv.innerHTML = `错误: ${err.message || err}`;
});
}
// 停止扫描函数
function stopScanning() {
if (html5QrCode) {
html5QrCode.stop().then(() => {
// 停止成功
html5QrCode.clear(); // 清除UI元素
html5QrCode = null;
resultDiv.innerHTML = "扫描已停止";
stopButton.classList.add("hidden");
startButton.classList.remove("hidden");
}).catch((err) => {
// 停止失败
console.error("无法停止扫描", err);
resultDiv.innerHTML = `停止失败: ${err}`;
});
}
}
// 绑定按钮事件
startButton.addEventListener("click", startScanning);
stopButton.addEventListener("click", stopScanning);
// 页面加载完成后自动开始扫描(可选)
// window.addEventListener("load", startScanning);
</script>
</body>
</html> 网友回复
如何让ai帮我自动在小红书或抖音上自动根据需求截流与潜在客户聊天拉客?
如果用go编写一个在virtualbox中启动的简单操作系统?
go如何搭建一个零信任网络?
如何用python实现一个公网代理访问软件?
如何用go实现一个公网代理访问软件?
如何用python实现一个内网穿透打洞程序,实现内网的80端口暴露到公网上可以访问?
如何用go实现一个内网穿透打洞程序,实现内网的80端口暴露到公网上可以访问?
何为Shadowsocks 代理?
python如何实现类似php的opendir目录相互隔离的fastcgi多租户虚拟空间?
nodejs如何实现类似php的opendir目录相互隔离的fastcgi多租户虚拟空间?


