当前位置: 首页 > 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.lqws.cn/news/185905.html

相关文章:

  • Vue 组件库发布实战(含 TypeScript 支持)
  • 课消管理软件,教培机构进入系统管理时代
  • 高温IC设计带来的挑战和问题
  • 深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
  • Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
  • JavaScript 原型与原型链:深入理解 __proto__ 和 prototype 的由来与关系
  • 区块链技术相关
  • 李沐《动手学深度学习》d2l安装教程
  • JS面试常见问题——数据类型篇
  • Java并发编程面试题
  • 【Android】Android Studio项目代码异常错乱问题处理(2020.3版本)
  • MySQL体系架构解析(二):MySQL目录与启动配置全解析
  • 模板方法模式:优雅封装不变,灵活扩展可变
  • API标准的本质与演进:从 REST 架构到 AI 服务集成
  • “一代更比一代强”:现代 RAG 架构的演进之路
  • 动力电池点焊机:驱动电池焊接高效与可靠的核心力量|比斯特自动化
  • Significant Location Change
  • 【Kotlin】协程
  • 【Kotlin】注解反射扩展
  • Android 之 kotlin 语言学习笔记四(Android KTX)
  • 深入浅出:JavaScript 中的 `window.crypto.getRandomValues()` 方法
  • C++.OpenGL (1/64) 创建窗口(Hello Window)
  • 办公提效的AI免费工具使用感悟
  • 如何让AI自己检查全文?使用OCR和LLM实现自动“全文校订”(可DIY校订规则)
  • 什么是质量管理系统?质量管理系统都有哪些功能?
  • vue-21 (使用 Vuex 模块和异步操作构建复杂应用)
  • 单例模式与锁(死锁)
  • 硅基计划2.0 学习总结 陆 抽象类与接口
  • 五子棋网络对战游戏的设计与实现设计与实现【源码+文档】
  • 高级网络工具包用户操作指南