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

vue生成二维码图片+文字说明

需求:点击下载图片,上方是二维码,下方显示该二维码的相关内容,并且居中显示,支持换行

解决方案步骤:

1. 使用qrcode生成二维码的DataURL。

2. 创建canvas,将二维码图片绘制到canvas的上半部分。

3. 在canvas的下半部分绘制文字,处理换行和样式。

4. 将canvas转换为图片,触发下载。

具体实现步骤:

1、安装依赖

npm install qrcode --save

2、组件实现

<template><div><button @click="generateAndDownload">下载图片</button><canvas ref="canvas" style="display: none;"></canvas></div>
</template><script>
import QRCode from 'qrcode'export default {data() {return {canvasWidth: 400,    // 画布宽度canvasHeight: 400,   // 画布高度qrSize: 300,        // 二维码尺寸textConfig: {        // 文字配置content: '这是图片的文字说明部分,支持自动换行功能,当文字超过宽度限制时会自动换行显示。',x: 40,y: 320,lineHeight: 24,maxWidth: 320,     // canvasWidth - 80fontSize: '16px Arial',color: 'black'}}},methods: {// 文字换行处理wrapText(ctx, text, x, y, maxWidth, lineHeight) {const chars = text.split('')let line = ''let testLine = ''// 设置精确文本测量基线ctx.textBaseline = 'top'let xCoord = xfor (let i = 0; i < chars.length; i++) {testLine = line + chars[i]const metrics = ctx.measureText(testLine)if (metrics.width > maxWidth && i > 0) {ctx.fillText(line, x, y)line = chars[i]y += lineHeight} else {line = testLine// 居中显示文字xCoord = (maxWidth - metrics.width) / 2 + 40}}ctx.fillText(line, xCoord, y)},// 生成并下载图片async generateAndDownload() {const canvas = this.$refs.canvasif (!canvas) return// 初始化画布canvas.width = this.canvasWidthcanvas.height = this.canvasHeightconst ctx = canvas.getContext('2d')if (!ctx) returntry {// 绘制背景ctx.fillStyle = 'white'ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight)// 生成二维码const qrDataURL = await QRCode.toDataURL('https://example.com', { width: this.qrSize })await new Promise(resolve => {const img = new Image()img.onload = () => {// 居中绘制二维码const margin = (this.canvasWidth - this.qrSize) / 2ctx.drawImage(img, margin, 20, this.qrSize, this.qrSize)resolve()}img.src = qrDataURL})// 绘制文字ctx.fillStyle = this.textConfig.colorctx.font = this.textConfig.fontSizethis.wrapText(ctx,this.textConfig.content,this.textConfig.x,this.textConfig.y,this.textConfig.maxWidth,this.textConfig.lineHeight)// 触发下载const link = document.createElement('a')link.download = 'qr-with-text.png'link.href = canvas.toDataURL('image/png')link.click()} catch (error) {console.error('生成失败:', error)}}}
}
</script>
http://www.xdnf.cn/news/906643.html

相关文章:

  • 猜字符位置游戏-position gasses
  • 数列运算中的常见错因分析
  • 使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
  • Python训练营-Day23-Pipeline
  • Tika Server:企业级文档内容解析的轻量级服务化方案
  • js树形菜单功能总结
  • AT2659_GNSS低噪声放大器芯片
  • 字节推出统一多模态模型 BAGEL,GPT-4o 级的图像生成能力直接开源了!
  • MySQL的优化部分介绍
  • 语音合成之十九 为什么对数行列式的值可以作为Flow-based模型loss?
  • 【信息系统项目管理师-案例真题】2025上半年(第二批)案例分析答案和详解(回忆版)
  • 蓝牙攻防实战指南:发现隐藏设备
  • 【更新至2024年】2000-2024年上市公司财务困境MertonDD模型数据(含原始数据+结果)
  • LangChain【5】之工具调用:连接大模型与现实世界的桥梁
  • C#中的密封类与静态类:特性、区别与应用实例
  • (力扣)80. 删除有序数组中的重复项 II
  • Seed1.5-VL登顶,国产闭源模型弯道超车丨多模态模型5月最新榜单揭晓
  • 计算机视觉顶刊《International Journal of Computer Vision》2025年5月前沿热点可视化分析
  • C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
  • java复习 03
  • css元素的after制作斜向的删除线
  • 【Go面试陷阱】对未初始化的chan进行读写为何会卡死?
  • Jenkins 工作流程
  • NoSQL之redis哨兵
  • 《SQL基础教程》第五章 SQL进阶之路:掌握高级查询的核心技巧
  • 2.1 Windows编译环境介绍
  • Caliper 配置文件解析:config.yaml
  • 加密货币钱包开发指南:多链资产管理与非托管安全范式
  • 关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案
  • Appium+python自动化(十一)- 元素定位- 下