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

  • 空调显示h5还能继续开吗(空调显示h5还能开吗)

  • 怎么查ip地址(怎么查ip地址冲突)

  • 华为mate40的屏幕尺寸是多大(华为mate40pro屏幕多少钱)

  • 华为p40pro和华为mate40pro的区别(华为p40pro和华为p60pro参数对比)

  • 上网方式dhcp是什么意思(上网模式dhcp)

  • 现在微信评论可以被别人看到吗(微信评论可以被删除吗)

  • wifi消失了搜索不出来(wifi突然消失了搜不到是不是路由器坏了)

  • 手机黑屏是咋回事oppo(手机黑屏了怎回事)

  • 苹果取消订阅后还显示(苹果取消订阅后怎么删除记录)

  • 读书郎平板电脑开不了机怎么回事(读书郎平板电脑换个屏幕多少钱)

  • 打印机打印出来有白条(打印机打印出来纸上有黑色印子)

  • 华为系统更新后还能降回去吗(华为系统更新后一直莫名重启)

  • 芒果tv赠送影片的方法(芒果tv赠片有限制)

  • iphone6支持2a快充吗(苹果6s支持2a快充吗)

  • 手机蓝牙怎么连接汽车蓝牙导航(手机蓝牙怎么连接不上)

  • 数字化转型的定义(数字化转型定义 IDC)

  • i7和i9的区别(酷睿i5和i7和i9的区别)

  • 天猫超市的东西能退吗(天猫超市的东西是正品吗比旗舰店便宜十几)

  • 苹果xr怎么不会自动锁屏(苹果xr为什么不会震动了)

  • 小米9怎么设置来电闪光灯(小米9怎么设置返回键)

  • 强制使用4xmsaa利弊(强制使用4xmass什么意思?)

  • 从Mac和iOS上的safari阻止cookies的设置方法(mac和mac之间怎么传东西)

  • Java实现Token登录验证(基于JWT的token认证实现)(java单点登录token)

  • python dump方法的序列化(python dup函数)

  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设 电脑维修 湖南楚通运网络