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

1.安装

npm install qrcode

npm install html2canvas

2.引用

import QRCode from 'qrcode'

import html2canvas from 'html2canvas'

效果:

二维码生成:

下载二维码图片:

二维码的内容:

实现代码:

javascript 复制代码
<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.src
        link.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>
相关推荐
康一夏40 分钟前
React面试题,封装useEffect
前端·javascript·react.js
Full Stack Developme1 小时前
Redis 持久化 备份 还原
前端·chrome
猪猪拆迁队1 小时前
2025年终总结-都在喊前端已死,这一年我的焦虑、挣扎与重组:AI 时代如何摆正自己的位置
前端·后端·ai编程
❆VE❆1 小时前
WebSocket与SSE深度对比:技术差异、场景选型及一些疑惑
前端·javascript·网络·websocket·网络协议·sse
ConardLi1 小时前
SFT、RAG 调优效率翻倍!垂直领域大模型评估实战指南
前端·javascript·后端
rgeshfgreh1 小时前
Java高性能开发:Redis7持久化实战
前端·bootstrap·mybatis
李剑一2 小时前
uni-app使用html5+创建webview,可以控制窗口大小、显隐、与uni通信
前端·trae
Hooray2 小时前
2026年,站在职业生涯十字路口的我该何去何从?
前端·后端
小二·2 小时前
Python Web 开发进阶实战:安全加固实战 —— 基于 OWASP Top 10 的全栈防御体系
前端·python·安全
over6972 小时前
🌟 JavaScript 数组终极指南:从零基础到工程级实战
前端·javascript·前端框架