当前位置: 首页 > news >正文

Vue2 qrcode+html2canvas 实现二维码的生成和保存

1.安装

npm install qrcode

npm install html2canvas

2.引用

import QRCode from 'qrcode'

import html2canvas from 'html2canvas'

效果:

1.

二维码生成: 

 下载二维码图片:

二维码的内容:

实现代码:

<template><div class="main-body module-contain"><div>我是测试的二维码内容h5页面: http://xxxx:8080/qrcodeContain</div><div class="generateQRcode-top"ref="qrcodeBox"v-show="isShowText"><!-- 我是测试的二维码内容 --><div ref="qrcodeContainer"class="qr-code"></div><div class="qr-txt">产品编号: xxxx</div></div><el-button type="primary"@click="generateQRCode">生成二维码</el-button><el-button type="primary"@click="saveQRCode">保存二维码</el-button></div>
</template><script>
import QRCode from 'qrcode'
import html2canvas from 'html2canvas'
export default {data () {return {isShowText: false,qr: 'http://xxxx:8080/qrcodeContain' // 二维码内容-网址/数字/字母 // 可以用网上的地址测试,例如:https://www.bilibili.com/guochuang/?spm_id_from=333.1007.0.0}},components: {},mounted () {},methods: {// 生成二维码generateQRCode () {this.isShowText = true//每次生成的时候清空内容,否则会叠加,二维码背景色透明会一目了然if (this.$refs.qrcodeContainer) {this.$refs.qrcodeContainer.innerHTML = ''}QRCode.toDataURL(this.qr, { errorCorrectionLevel: 'H' }, (err, url) => {if (err) console.error(err)// 将二维码URL设置到容器的背景图片this.$refs.qrcodeContainer.style.backgroundImage = `url(${url})`})},// 保存二维码async saveQRCode () {// 使用html2canvas将二维码容器转换为图片try {const canvas = await html2canvas(this.$refs.qrcodeBox)// 创建一个图片元素const img = new Image()img.src = canvas.toDataURL('image/png')// 创建一个链接元素const link = document.createElement('a')// 设置下载的文件名link.download = '二维码.png'// 触发点击link.href = img.srclink.click()} catch (error) {console.error(error)}},}
}
</script><style lang="less" scoped>
.module-contain {.generateQRcode-top {display: flex;// justify-content: space-between;// align-items: center;width: 360px;// height: 200px;margin: 20px 0;background-color: #fff;border: 1px solid #eee;.qr-code {width: 180px;height: 180px;background-position: 50% 50%;background-repeat: no-repeat;background-size: contain;text-align: center;font-size: 20px;font-weight: bold;}.qr-txt {padding: 12px 0;font-size: 14px;}}
}
</style>

http://www.lryc.cn/news/438571.html

相关文章:

  • GEE 教程:利用Google Dynamic数据进行逐月指定区域的土地分类数据提取分析
  • Nginx 负载均衡:优化网站性能与可扩展性的利器
  • 【Python基础】Python错误和异常处理(详细实例)
  • 如何查看串口被哪个程序占用?截止目前最方便的方法
  • 深入理解SpringBoot(一)----SpringBoot的启动流程分析
  • MySql基础-单表操作
  • 【STM32系统】基于STM32设计的SD卡数据读取与上位机显示系统(SDIO接口驱动、雷龙SD卡)——文末资料下载
  • SpringBoot开发——整合Redis
  • OpenCV结构分析与形状描述符(17)判断轮廓是否为凸多边形的函数isContourConvex()的使用
  • P5425 [USACO19OPEN] I Would Walk 500 Miles G
  • Java高级Day41-反射入门
  • 在Linux系统上使用Docker部署java项目
  • 【C++】标准库IO查漏补缺
  • python简单易懂的lxml读取HTML节点及常用操作方法
  • Java | Leetcode Java题解之第406题根据身高重建队列
  • 安卓获取apk的公钥,用于申请app备案等
  • 【leetcode_python】杨辉三角
  • Parallels Desktop 20 for Mac中文版发布了?会哪些新功能
  • SpringBoot整合SSE-灵活管控连接
  • 挖矿木马-Linux
  • 【leetcode——415场周赛】——python前两题
  • 【CSS in Depth 2 精译_029】5.2 Grid 网格布局中的网格结构剖析(上)
  • ZYNQ LWIP(RAW API) TCP函数学习
  • Spring Boot,在应用程序启动后执行某些 SQL 语句
  • 【SQL】百题计划:SQL最基本的判断和查询。
  • 04_Python数据类型_列表
  • F5设备绑定EIP
  • 使用 PyCharm 新建 Python 项目详解
  • 从0开始学习 RocketMQ:分布式事务消息的实现
  • MySQL 查询数据库的数据总量