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)
}

效果

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

相关推荐
m0_73812072几秒前
CTFshow系列——PHP特性Web93-96
开发语言·安全·web安全·php·ctfshow
乖女子@@@9 分钟前
React笔记_组件之间进行数据传递
javascript·笔记·react.js
m0_5704664111 分钟前
代码随想录算法训练营第二十八天 | 买卖股票的最佳实际、跳跃游戏、K次取反后最大化的数组和
java·开发语言·算法
程序喵大人12 分钟前
分享个C++线程池的实现源码
开发语言·c++·线程池
F2E_Zhangmo23 分钟前
基于cornerstone3D的dicom影像浏览器 第二章 加载本地文件夹中的dicom文件并归档
前端·javascript·css
念念不忘 必有回响29 分钟前
js设计模式-装饰器模式
javascript·设计模式·装饰器模式
不会吃萝卜的兔子37 分钟前
go webrtc - 1 go基本概念
开发语言·golang·webrtc
weixin_5841214344 分钟前
vue3+ts导出PDF
javascript·vue.js·pdf
要做朋鱼燕1 小时前
【C++】 priority_queue 容器模拟实现解析
开发语言·c++·笔记·职场和发展
jiaway1 小时前
【C语言】第四课 指针与内存管理
c语言·开发语言·算法