js使用qrcode与canvas生成带logo的二维码

qrcode库

文档

https://www.npmjs.com/package/qrcode

安装

bash 复制代码
npm i qrcode

使用

errorCorrectionLevel: 'H' // 容错率(H是最高,其它看文档)

width: 200 // 大小

margin: 2 // 边距

javascript 复制代码
import QRCode from 'qrcode'

const testFn = async () => {
    const img= await QRCode.toDataURL('https://www.baidu.com/', { errorCorrectionLevel: 'H', width: 200, margin: 2 })
    console.log(img)
}

canvas绘图

文档

https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API

使用

html

html 复制代码
<canvas id="myCanvas" width="100" height="100" style="border: 1px solid #ccc"></canvas>

js

javascript 复制代码
const canvas = document.getElementById('myCanvas')

// 设置画布宽高(也可内联样式写标签上)
canvas.width = 200
canvas.height = 200

// 获取 2d 上下文对象
const ctx = canvas.getContext('2d')

// 绘制圆
// 设置圆的属性
const centerX = canvas.width / 2 // 圆心 X 坐标
const centerY = canvas.height / 2 // 圆心 Y 坐标
const radius = 30 // 圆的半径
ctx.beginPath() // 开始路径
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, false) // 绘制圆
ctx.fillStyle = '#f9f9f9' // 填充颜色
ctx.fill() // 填充圆
ctx.closePath() // 关闭路径

// 绘制文本
ctx.font = '10px' // 文本大小
ctx.fillStyle = 'blue' // 颜色
ctx.textAlign = 'center' // 水平对齐
ctx.textBaseline = 'middle' // 垂直对齐
ctx.fillText('绘制文本', canvas.width / 2, canvas.height / 2)

效果

合并图片方法

javascript 复制代码
function mergeImages() {
  const canvas = document.createElement('canvas')
  const ctx = canvas.getContext('2d')

  // 创建两个图片对象
  const img1 = new Image()
  const img2 = new Image()
  img1.src = 'https://www.test.com/menu.png'
  img2.src = 'https://www.test.com//logo.png'

  // 等待两张图片加载完成
  let imagesLoaded = 0

  img1.onload = img2.onload = function () {
    imagesLoaded++
    if (imagesLoaded === 2) {
      // 当两张图片都加载完成后,绘制到 Canvas 上
      ctx.drawImage(img1, 0, 0) // 绘制第一张图片
      ctx.drawImage(img2, 50, 0) // 绘制第二张图片,x偏移 50 像素

      // 下载合并后的图片
      const link = document.createElement('a')
      link.download = 'merged-image.png'
      link.href = canvas.toDataURL('image/png')
      link.click()
    }
  }
}

使用qrcode生成二维码,使用canvas绘制合并到一起

实现

html

html 复制代码
<canvas id="myCanvas" width="100" height="100" style="border: 1px solid #ccc"></canvas>

js

javascript 复制代码
const onMounted = async () => {
  // await nextTick() // 使用vue加上
  const canvas = document.getElementById('myCanvas')
  canvas.width = 200
  canvas.height = 200
  const ctx = canvas.getContext('2d')
  
  // 生成二维码并绘制到canvas
  const img1 = new Image()
  img1.src = await QRCode.toDataURL('https://www.baidu.com/', { errorCorrectionLevel: 'H', width: 200, margin: 2 })
  await new Promise((resolve, reject) => {
    img1.onload = () => {
      resolve(ctx.drawImage(img1, 0, 0))
    }
  })

  // 绘制圆
  // 设置圆的属性
  const centerX = canvas.width / 2 // 圆心 X 坐标
  const centerY = canvas.height / 2 // 圆心 Y 坐标
  const radius = 30 // 圆的半径
  ctx.beginPath() // 开始路径
  ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, false) // 绘制圆
  ctx.fillStyle = '#f9f9f9' // 填充颜色
  ctx.fill() // 填充圆
  ctx.closePath() // 关闭路径

  // 绘制文本
  ctx.font = '10px'
  ctx.fillStyle = 'blue'
  ctx.textAlign = 'center'
  ctx.textBaseline = 'middle'
  ctx.fillText('绘制文本', canvas.width / 2, canvas.height / 2)
}

效果

如需把文本替换成图片,参考上面 合并图片方法 即可

相关推荐
云小逸11 分钟前
【nmap源码解析】Nmap OS识别核心模块深度解析:osscan2.cc源码剖析(1)
开发语言·网络·学习·nmap
冰暮流星12 分钟前
javascript之二重循环练习
开发语言·javascript·数据库
风指引着方向13 分钟前
自定义算子开发入门:基于 CANN op-plugin 的扩展实践
开发语言
Fairy要carry18 分钟前
面试-GRPO强化学习
开发语言·人工智能
Mr Xu_25 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
Liekkas Kono30 分钟前
RapidOCR Python 贡献指南
开发语言·python·rapidocr
张张努力变强36 分钟前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
xyq202439 分钟前
Matplotlib 绘图线
开发语言
m0_6948455742 分钟前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
春日见1 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器