位置: IT常识 - 正文

js实现web页面扫描二维码(html5-qrcode)(html 调用扫码)

编辑:rootadmin
js实现web页面扫描二维码(html5-qrcode)

推荐整理分享js实现web页面扫描二维码(html5-qrcode)(html 调用扫码),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:js调用扫一扫,js调用扫码,web前端实现扫一扫功能,js调用扫码,js调用微信扫一扫,js调用扫码,js调用扫码,web前端实现扫一扫功能,内容如对您有帮助,希望把文章链接给更多的朋友!

js实现web页面扫描二维码(html5-qrcode)

以vue3项目使用为例 这种还是推荐IOS和安卓原生实现,做桥接文件调用 这种实现方式只支持localhost和https的浏览器网页版 而且经过测试兼容性也不是很好,具体参见官网 下面附了官网链接

js实现web页面扫描二维码(html5-qrcode)(html 调用扫码)

1.下载依赖

npm install html5-qrcode

2.二次封装

<template> <div> <div class="readerDialog" v-show="dialogShow"> <div id="reader" width="600px"></div> </div> </div></template><script>// 调起摄像头扫描二维码import { ref, reactive, toRefs, watch, getCurrentInstance } from 'vue';import { Html5Qrcode } from 'html5-qrcode';export default { name: 'Qrcode', props: [], emits: ['success'], setup (props, { emit }) { const params = reactive({ dialogShow: false, codeUrl: '', cameraId: '', showText: '', devices: [], getCamerasText: '', html5QrCode: null }); // 外部调用此方法打开 const open = () => { params.dialogShow = true; console.log('params.dialogShow', params.dialogShow); getCameras(); }; const getCameras = () => { params.getCamerasText = JSON.stringify(Html5Qrcode.getCameras()); Html5Qrcode.getCameras() .then((devices) => { params.showText = JSON.stringify(devices); /** * devices would be an array of objects of type: * { id: "id", label: "label" } */ if (devices && devices.length) { // 如果有2个摄像头,1为前置的 if (devices.length > 1) { params.cameraId = devices[1].id; } else { params.cameraId = devices[0].id; } params.devices = devices; start(); // .. use this to start scanning. } }) .catch((err) => { params.showText = err; console.log('getCameras err:', err); // 获取设备信息失败 }); }; const start = () => { params.html5QrCode = new Html5Qrcode('reader'); params.html5QrCode.start( params.cameraId, // retreived in the previous step. { fps: 10, // sets the framerate to 10 frame per second qrbox: { width: 250, height: 250 }, // sets only 250 X 250 region of viewfinder to // scannable, rest shaded. }, (decodedText, decodedResult) => { // do something when code is read. For example: // if (qrCodeMessage) { // this.getCode(qrCodeMessage); // this.stop(); // } console.log('===成功日志:', decodedText); console.log(typeof decodedResult, decodedResult); emit('success', decodedText); stop(); params.dialogShow = false; }, (errorMessage) => { // parse error, ideally ignore it. For example: // console.log(`QR Code no longer in front of camera.`); console.log('非扫描成功日志:', errorMessage); } ) .catch((err) => { // Start failed, handle it. For example, console.log(`Unable to start scanning, error: ${err}`); }); }; const stop = () => { params.html5QrCode .stop() .then((ignore) => { // QR Code scanning is stopped. console.log('QR Code scanning stopped.'); }) .catch((err) => { // Stop failed, handle it. console.log('Unable to stop scanning.'); }); }; return { ...toRefs(params), getCameras, open }; },};</script><style lang="scss" scoped>.readerDialog { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; z-index: 999999; background: #333; #reader { top: 25%; }}</style>

3.使用(以下只演示部分核心代码)

html部分

<van-button type="success" @click="cameraClick">开始扫描</van-button><Qrcode ref="QrcodeRef" @success="qrcodeSuccess" />

js部分

import Qrcode from '@/components/Qrcode/index.vue';const QrcodeRef = ref();const qrcodeText = ref('');// 扫描调起const cameraClick = () => { QrcodeRef.value.open();};// 扫描成功回调const qrcodeSuccess = (txt) => { qrcodeText.value = txt;};

官网:https://github.com/mebjas/html5-qrcode

本文链接地址:https://www.jiuchutong.com/zhishi/283334.html 转载请保留说明!

上一篇:一体机、笔记本、品牌机、组装机各自的优缺点(一体机笔记本电脑)

下一篇:修改iframe内部元素的样式(iframe更改自身src)

  • 建筑垃圾清运费税率是多少
  • 企业并购需要交什么税
  • 定额怎么确定
  • 补交以前年度房产税和滞纳金需要更改以前年度报表吗
  • 怎么看医疗报销单
  • 无形资产是金融性资产还是经营性资产
  • 贷款未收属于什么科目
  • 住房贷款利息专项附加扣除是返钱吗
  • 库存商品进行非货币资产交换收入怎么确定
  • 享受所得税减免优惠的生产性外商投资企业包括
  • 公司账户资金
  • 货车挂靠企业需要交增值税吗?
  • 抄税和上报汇总一样吗
  • 高新技术企业认定条件
  • 金蝶软件如何成批反审核
  • 参展补助
  • 劳务和工资合并扣税吗
  • 基建管理费如何进行结转?
  • 实收金额比应收金额多
  • 成本算错了
  • 自行生产的存货转固定资产怎么做账
  • 购销合同没写签订日期违法吗
  • win10指纹传感器在哪里
  • 如何在Excel中进行单元格格式化?
  • 营改增后一般纳税人税率
  • 跟a签订合同可以撤销吗
  • 苹果macOS 13.3 RC 发河北承德市承德县华夏电器
  • flash是什么文件
  • 结算劳务外包的工作内容
  • 购进油漆,用于装饰本企业办公楼
  • php注册和登录界面
  • 提供劳务取得劳务收入10万元
  • thinkphp教程
  • thinkPHP中_initialize方法实例分析
  • 如何把html数据导入表格
  • thinkphp d方法
  • 确认收入的五个原则
  • 如何登记现金明细账
  • sql性能优化方法
  • 劳动法里病假工资怎么算
  • 零税率和免税的例子
  • 怎么登记现金日记账和银行存款日记账
  • 长期待摊费用怎么算出来的
  • 电脑入账如何做凭证
  • 支付宝和微信的财务统计
  • 保函分为几种
  • 劳动保护费是否可以抵扣
  • 已认证抵扣的发票如何做账
  • 公交公司财政补贴
  • 商品的结构
  • 如何查看员工社保
  • 总公司与分公司账务处理
  • 对公账户分为几类
  • 资产负债表税务局能看到吗
  • 离职员工未休完的年假如何支付
  • 现金流量为负的原因
  • 套现给现金还是转账好
  • 新成立的公司工会经费需要交吗
  • 建筑业分包税收政策
  • mysql 内存模型
  • 苹果电脑 输入
  • win7系统调节亮度快捷键
  • 图解在OS X中管理窗口大小的多种方法
  • perform.exe是什么进程
  • windows8.1开始
  • 安卓布局优化
  • linux中shell脚本编写
  • linux执行多个脚本
  • jquery点击移动div
  • sequelize-cli
  • js 运算符
  • interview分析方法
  • android 加密算法
  • 胡世军简历年龄多大
  • 国税局下设几个部门
  • 广东省电子居住证怎么查看
  • 经信委和科技局的区别
  • 烟台税务局举报电话号码
  • 电子税务局房产税怎么申报
  • 新华保险有返本金吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设