更新记录
1.0.0(2026-07-20)
下载此版本
- 首次发布,迁移至 uni_modules 标准化结构
- 支持 fullscreen / inline 双模式
- 支持闪光灯自动检测与控制
- 重复扫码过滤机制
- 扫码成功动效 + 提示音(含 Web Audio API 降级)+ 震动反馈
- 完整的 Props 配置体系(5 大类 15+ 项)
- 5 个事件回调 + 4 个可调用方法
- 自动资源管理与释放
平台兼容性
uni-app(5.12)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
| √ |
√ |
√ |
√ |
× |
× |
× |
× |
× |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| × |
× |
× |
× |
× |
× |
- |
× |
× |
× |
- |
- |
QrScan — H5 扫码组件
基于 html5-qrcode 封装的 uni-app H5 扫码组件,支持全屏与内嵌两种模式。
平台兼容性
| H5 (Chrome) |
H5 (Safari) |
App |
微信小程序 |
其他小程序 |
| ✅ |
✅ |
❌ |
❌ |
❌ |
App / 小程序端请使用 uni-app 原生扫码 API(uni.scanCode)。
快速开始
安装依赖
npm install html5-qrcode
使用组件
将插件导入 uni_modules 后,通过 easycom 自动引入,无需手动注册:
<template>
<view>
<h5-qrcode-scanner @scan-success="onResult" />
</view>
</template>
<script>
export default {
methods: {
onResult(code) {
uni.showToast({ title: '扫码结果: ' + code, icon: 'none' })
}
}
}
</script>
目录结构
uni_modules/h5-qrcode-scanner/
├── package.json # 插件元数据
├── readme.md # 本文档
├── changelog.md # 更新日志
├── components/
│ └── h5-qrcode-scanner/
│ └── h5-qrcode-scanner.vue # 组件主文件
├── demo/
│ └── index.vue # 演示页面
└── static/
└── scanvideo.mp3 # 默认提示音
Props
| 属性 |
类型 |
默认值 |
说明 |
| 核心配置 |
mode |
String |
'fullscreen' |
显示模式:fullscreen / inline |
scanColor |
String |
'#00e676' |
扫码主题色 |
scanBoxWidth |
Number |
280 |
扫码框宽度(px) |
scanBoxRatio |
Number |
1 |
扫码框宽高比,1 为正方形 |
| 行为配置 |
repeatInterval |
Number |
2000 |
重复扫码过滤间隔(ms) |
fps |
Number |
15 |
扫码帧率,1~60 |
autoStart |
Boolean |
true |
组件挂载后自动启动摄像头 |
| 音效配置 |
enableScanSound |
Boolean |
true |
是否开启提示音 |
scanSoundUrl |
String |
'' |
自定义提示音 URL,默认用内置 |
scanSoundVolume |
Number |
0.6 |
提示音音量,0~1 |
| 文案配置 |
title |
String |
'扫码' |
全屏模式标题 |
tipText |
String |
'将二维码放入框内…' |
底部提示文字 |
| 生命周期 |
autoStopAfterScan |
Boolean |
false |
扫码成功后自动停止 |
autoCloseDelay |
Number |
0 |
成功后延迟关闭(ms),0=不自动关闭 |
Events
| 事件名 |
参数 |
说明 |
scan-success |
decodedText: String |
扫码成功,返回解码内容 |
scan-error |
error: Error |
扫码器启动失败 |
scan-started |
— |
扫码器成功启动 |
close |
— |
点击关闭按钮 |
torch-changed |
isOn: Boolean |
闪光灯状态变化 |
Methods(ref 调用)
| 方法 |
返回值 |
说明 |
initScanner() |
Promise<void> |
手动初始化扫码器 |
stopScanner() |
Promise<void> |
停止扫码,释放资源 |
restartScanner() |
Promise<void> |
停止后重新启动 |
toggleTorch() |
Promise<void> |
切换闪光灯 |
使用示例
全屏扫码,扫到即关
<template>
<view>
<button @click="open = true">打开扫码</button>
<h5-qrcode-scanner
v-if="open"
mode="fullscreen"
scanColor="#2979ff"
title="扫描二维码"
:autoStopAfterScan="true"
:autoCloseDelay="800"
@scan-success="handleResult"
@close="open = false"
/>
</view>
</template>
内嵌模式
<template>
<view style="padding:20px">
<h5-qrcode-scanner mode="inline" :scanBoxWidth="260" @scan-success="handleResult" />
</view>
</template>
手动控制启停
<template>
<view>
<h5-qrcode-scanner ref="scanner" mode="inline" :autoStart="false" @scan-success="onScan" />
<button @click="$refs.scanner.initScanner()">开始</button>
<button @click="$refs.scanner.stopScanner()">停止</button>
</view>
</template>
常见问题
摄像头无法启动
- 确保页面运行在 HTTPS 或 localhost 环境
- 浏览器已授权摄像头权限
- 监听
scan-error 事件查看具体错误
识别率低
- 增大
fps(推荐 15~25)
- 确保二维码清晰、光线充足
- 适当调整
scanBoxWidth
闪光灯不显示
- 仅后置摄像头 + 硬件支持时可用
- 需在真机上测试,模拟器不支持
技术依赖
| 依赖 |
说明 |
html5-qrcode |
^2.x,底层扫码引擎 |
| uni-app |
Vue 2 / Vue 3 |
许可证
MIT