Vue使用vue-qr生成二维码

这篇博客介绍了如何在Vue项目中使用vue-qr组件来生成二维码,包括设置二维码的内容、大小、边距、背景图、LOGO等属性,并提供了完整的代码示例,展示了如何导入、注册和在模板中使用vue-qr。
  • npm下载vue-qr
npm install vue-qr --save
  • 引入vue-qr
import VueQr from 'vue-qr';
  • 注册
export default {
   name:'', 
   components:{
      VueQr,
   }
}
  • vue-qr参数
  1. List item
  2. text 二维码,即扫描二维码后跳转的页面
  3. size 二维码大小
  4. margin 二维码图像的外边距, 默认 20px
  5. bgSrc 嵌入的背景图地址
  6. logoSrc 嵌入至二维码中心的 LOGO 地址
  7. logoScale 中间图的尺寸
  8. dotScale 二维码的点的大小
  9. colorDark 实点的颜色(注意:和colorLight一起设置才有效)
  10. colorLight 空白的颜色(注意:和colorDark一起设置才有效)
  11. autoColor 若为 true, 背景图的主要颜色将作为实点的颜色即 colorDark,默认 true
  • 完整代码
<template>
   <div>
      <vue-qr
         :text="imgUrl"
         :size="250"
         :logoSrc="logo"
         :logoScale="0.3">
      </vue-qr>
   </div>
</template>
<script>
import VueQr from 'vue-qr'
export default {
   name:'', 
   components:{
      VueQr,
   },
   data() {
      return {
         imgUrl: 'http://www.baidu.com', //扫码后手机显示的页面
         logo: require("@/assets/logo.png"),//二维码中间的图片
      }
   },
   methods:{
   },
}
</script>

Vue 中,如果你想使用 `vue-qr` 这个库来生成二维码,并实现 **根据文字长度动态调整中心区域大小** 的功能,可以结合 `vue-qr` 的图像嵌入能力,配合 `canvas` 或 `measureText` 实现自定义绘制。 --- ### ✅ 实现目标 - 使用 `vue-qr` 生成二维码-二维码中心嵌入文字。 - 根据文字长度动态调整中心区域大小。 - 保证二维码可扫描性(不破坏关键区域)。 --- ### 📦 安装依赖 ```bash npm install vue-qr ``` --- ### 🧩 示例代码(Vue 2 / Vue 3 通用) ```html <template> <div class="qr-container"> <h2>使用 vue-qr 生成二维码(动态调整中心区域大小)</h2> <input v-model="qrContent" placeholder="输入二维码内容" /> <input v-model="centerText" placeholder="输入中心文字" /> <button @click="generateQRCode">生成二维码</button> <div class="qr-wrapper" ref="qrWrapper"> <!-- 二维码画布 --> <canvas ref="qrCanvas" width="300" height="300"></canvas> <!-- vue-qr 生成二维码(隐藏,仅用于获取二维码图像) --> <vue-qr :text="qrContent" :size="300" :color="qrColor" :bgSrc="bgImage" ref="vueQr" style="display: none" /> </div> </div> </template> <script> import VueQr from 'vue-qr'; export default { name: 'DynamicCenterQR', components: { VueQr, }, data() { return { qrContent: 'https://www.example.com', centerText: 'Vue QR', qrColor: '#000000', bgImage: null, }; }, methods: { async generateQRCode() { const canvas = this.$refs.qrCanvas; const ctx = canvas.getContext('2d'); const vueQr = this.$refs.vueQr; // Step 1: 获取 vue-qr 生成二维码图像 const qrImg = await new Promise((resolve) => { vueQr.getBase64Url((err, url) => { const img = new Image(); img.onload = () => resolve(img); img.src = url; }); }); // Step 2: 清空 canvas 并绘制二维码图像 ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(qrImg, 0, 0, canvas.width, canvas.height); // Step 3: 动态绘制中心文字 if (this.centerText) { this.drawTextCenter(ctx, canvas, this.centerText); } }, drawTextCenter(ctx, canvas, text) { const baseFontSize = 24; ctx.font = `${baseFontSize}px Arial`; const textWidth = ctx.measureText(text).width; // 动态计算中心区域大小 const padding = 10; const centerWidth = textWidth + padding * 2; const centerHeight = baseFontSize + padding * 1.5; // 设置字体大小自适应 let fontSize = baseFontSize; ctx.font = `${fontSize}px Arial`; let measuredWidth = ctx.measureText(text).width; while (measuredWidth > centerWidth - padding * 2 && fontSize > 6) { fontSize--; ctx.font = `${fontSize}px Arial`; measuredWidth = ctx.measureText(text).width; } // 绘制白色背景(可选) ctx.fillStyle = '#FFFFFF'; ctx.fillRect( canvas.width / 2 - centerWidth / 2, canvas.height / 2 - centerHeight / 2, centerWidth, centerHeight ); // 绘制文字 ctx.fillStyle = '#000000'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(text, canvas.width / 2, canvas.height / 2); // 可选:绘制边框 // ctx.strokeStyle = '#000000'; // ctx.lineWidth = 1; // ctx.strokeRect(canvas.width / 2 - centerWidth / 2, canvas.height / 2 - centerHeight / 2, centerWidth, centerHeight); }, }, }; </script> <style scoped> .qr-container { max-width: 500px; margin: 0 auto; font-family: Arial, sans-serif; } input { display: block; width: 100%; padding: 8px; margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; } button { padding: 10px 15px; background-color: #42b983; color: white; border: none; border-radius: 4px; cursor: pointer; } canvas { margin-top: 20px; border: 1px solid #eee; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); } </style> ``` --- ### 🔍 代码解释 - **vue-qr**: 用于生成基础二维码图像(支持 base64、canvas 等格式)。 - **canvas**: 用于绘制二维码和中心文字。 - **getBase64Url**: vue-qr 提供的方法,获取二维码图像的 base64。 - **drawTextCenter**: 根据文字长度动态计算区域大小,并绘制文字。 - **ctx.measureText**: 测量文字宽度,用于动态调整区域。 - **字体自适应**: 通过循环逐步减小字体大小,直到文字适应区域。 --- ### ✅ 优点 - 利用 `vue-qr` 简化二维码生成流程。 - 支持任意长度文字,动态调整中心区域大小。 - 文字居中、字体自适应,视觉美观。 - 可扩展性强,例如支持图片嵌入、透明背景等。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值