vue3 生成二维码(可添加文字和logo)并可以下载二维码

1.安装 vue - qr

 npm install vue-qr -s

2.引入和使用 


 
<template>
 
      <div class="qrBox">
        //模版中使用
        <vue-qr 
            id="payQR" 
            v-if="codeText" 
            :text="codeText" 
            :size="248" 
            colorDark="#5559FF" 
            colorLight="#ffffff"
            :logoSrc="logoImg" 
            :callback="getImgInfo">
 
        </vue-qr>
 
      </div>
 
</template>
<script setup>
import { ref } from 'vue';
import vueQr from 'vue-qr/src/packages/vue-qr.vue' 引入vue-qr组件
 
const codeText=ref();   拿到生成二维码的数据后赋值给 codeText 
 
 // 下载二维码
function downloadQrCode(row) {
 
  const iconUrl = proxy.$refs.Qrcode.$el.src; 
  const a = document.createElement("a"); //自定义a标签
  const event = new MouseEvent("click"); //自定义鼠标点击事件
  a.download = "活动二维码";
  a.href = iconUrl;
  a.dispatchEvent(event); //触发自定义事件
}
// 生成二维码
function getImgInfo(qrUrl) {
  const img = new Image();
  img.src = qrUrl;
  img.onload = () => {
    const canvas = document.createElement("canvas");
    const ctx = canvas.getContext("2d");

    const text = "要添加的文字";

    canvas.width = img.width;
    canvas.height = img.height;

    ctx.drawImage(img, 0, 0);

    // 设置轮廓颜色和宽度
    ctx.strokeStyle = "#000000";
    ctx.lineWidth = 3;

    // 绘制文字轮廓
    ctx.strokeText(
      text,
      canvas.width / 2 - ctx.measureText(text).width / 2,
      canvas.height / 1.6
    );

    ctx.font = "bold 10px"; // 设置字体大小和类型
    ctx.fillStyle = "white"; // 设置字体颜色
    ctx.fillText(
      text,
      canvas.width / 2 - ctx.measureText(text).width / 2,
      canvas.height / 1.6
    ); // 在图片中心添加文字

    document.getElementById("payQR").src = canvas.toDataURL("image/png");
  };
}
 
<script/>

3.示例展示 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值