位置: 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)

  • 天猫提现一直没到账
  • 源泉扣税什么意思
  • 费用怎么分析
  • 社保次月缴纳上月算不算中断
  • 小规模纳税人代理记账一般多少钱
  • 增值税发票丢了怎么补开
  • 资产减值准备的税务处理
  • 提取法定盈余公积会计分录
  • 营改增后房地产企业如何开票
  • 预提退货实际发生时账务处理
  • 承包承租经营单位是什么意思
  • 土地使用税怎么征收标准
  • 携税宝如何网上缴费
  • 未认证进项税额转出
  • 开发项目未竣工前土地使用税怎么缴纳
  • 购买的技术服务费需要摊销吗
  • 与权益法核算相关的累计净损益
  • 零申报工会经费滞纳金怎么算
  • 税控设备指什么
  • 学校食堂承包餐饮公司
  • windows11开始菜单点击没反应
  • 财管如何区分金额大小
  • 笔记本电脑应该关机还是睡眠
  • 联想小新Air15如何进入bois
  • php+redis
  • 业务招待费汇算清缴扣除标准
  • 库存商品的主要类型
  • 带壳的栗子 (© Kai Keisuke/Shutterstock)
  • vue 移动端
  • 固定资产清查盘盈盘亏的账务处理
  • ant design vue 表单
  • php有哪些
  • 替票报销开什么发票
  • nlp track
  • java 字节流 字符流
  • 营业外支出是什么会计要素
  • 专票认证期限多长时间
  • 增值税专用发票和普通发票的区别
  • 应收账款转回坏账怎么办
  • 物流货运发票要交税吗
  • 免费赠送的产品报关金额
  • 税控盘抵扣怎么做账
  • 变更法人需要什么流程
  • python异常值处理方法
  • mongodb reference
  • 公司代扣代缴个人所得税是怎么算的
  • 出口货物不能退税的账务处理
  • 软件企业增值税计算
  • 纳税人遗失税务登记证件并按照规定
  • 个税申报更正申报如何操作
  • 文化事业建设费会计分录
  • 企业转移地点剩余房产
  • 高温补贴发放管理制度
  • 汽车三产件
  • 工会经费计提按照应发工资还是实发工资
  • 政府补贴收入确认政策
  • 部门财务独立核算原则有哪些
  • rhel7.2安装
  • fedora s9
  • vmware虚拟机不能用桥接模式
  • windows8关机在哪里
  • python sed
  • centos7修改文件内容
  • 如何在linux
  • win8怎么把c盘恢复出厂设置
  • redhat本地yum配置的基本步骤
  • mongoose怎么用
  • [置顶]bilinovel
  • perl常用模块
  • opengl中点画线算法
  • AddForceAtPosition方法:增加刚体点作用力
  • vue如何用
  • python设计作品
  • jQuery插件封装时如要实现链式编程,需要
  • 山东发票查询系统平台
  • 个人所得税中途换了公司 退税
  • 农业方面有哪些有名杂志
  • 国家税务局发票真伪查验平台
  • 常州金坛最新新闻
  • 有限公司怎么交五险
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设