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>
相关推荐
Setsuna_F_Seiei4 小时前
前端转型 Agent 开发 04 之 MCP 与 Skill(赋予 Agent 更广工作能力)
前端·agent
刘发财5 小时前
前端2秒生成500页矢量PDF,rust真的强到没朋友
前端·javascript·rust
郑州光合科技余经理6 小时前
国际版外卖系统:税率字段怎么和订单主流程解耦
android·java·开发语言·前端·后端·php·ai编程
梦想平凡6 小时前
百游棋牌源代码开发搭建教程(十):隔离部署、备份恢复与双端验收
java·前端·javascript·数据库·源代码管理
yume_sibai7 小时前
06-Rust Web 开发实战(Axum 框架 + 数据库 + JWT 认证 + 中间件 + 部署)
前端·数据库·rust
计算机魔术师9 小时前
特朗普上台打给黄仁勋:AI末日论是骗局,我们绝不让它发生
前端
troy1289 小时前
Python 基础语法(八):Web 后端开发、数据分析与可视化、网络爬虫、人工智能 / 大模型应用
前端·python·数据分析
计算机魔术师9 小时前
CEO说要慢下来,黑客说别做梦了——同一篇论文,两种命运
前端
kyriewen10 小时前
我花3天抓了一个幽灵bug,凶手藏在第4层
前端·javascript·程序员
IT_陈寒10 小时前
Java里用Stream.parallel()翻车实录,这性能还不如单线程
前端·人工智能·后端