当前位置: 首页 > 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.dtcms.com/a/234705.html

相关文章:

  • html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码
  • JS设计模式(4):观察者模式
  • LabVIEW工业级多任务实时测控系统
  • ruoyi-plus-could 负载均衡 通过 Gateway模块配置负载均衡
  • UniApp系列
  • 华为OD最新机试真题-食堂供餐-OD统一考试(B卷)
  • Bootstrap 4 文件结构与 API 使用指南
  • 如何使用索引和条件批量更改Series数据
  • 交易系统开发:跨境资本的高速通道架构解密
  • HarmonyOS运动开发:如何用mpchart绘制运动配速图表
  • 父组件prop传向子组件的值,被子组件直接v-model绑定 功能不生效
  • 前端实现视频/直播预览
  • 深入理解 Python `asyncio` 的子进程协议(Subprocess Protocol)
  • Java-IO流之压缩与解压缩流详解
  • 阿里联合上海AI Lab提出DMM!多个模型压缩成一个通用T2I模型!可控任意风格生成!
  • Tomcat全方位监控实施方案指南
  • Python内置函数ord()详解
  • 数据库系统学习
  • M3T联邦基础模型用于具身智能:边缘集成的潜力与挑战
  • 8. 线性表的类型定义
  • 面向开发者的提示词工程④——文本推断(Inferring)
  • Qt(part1)Qpushbutton,信号与槽,对象树,自定义信号与槽,lamda表达式。
  • 人员定位系统解决方案
  • 记录一个用了很久的git提交到github和gitee比较方便的方法
  • 基于Javamail的邮件收发系统设计与实现【源码+文档】
  • NLP学习路线图(二十九):BERT及其变体
  • 三种读写传统xls格式文件开源库libxls、xlslib、BasicExcel的比较
  • 20250606-C#知识:List排序
  • 使用 Ansible 在 Windows 服务器上安装 SSL 证书
  • Opencv中的addweighted函数