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

效果

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

相关推荐
_OP_CHEN14 小时前
C++基础:(八)STL简介
开发语言·c++·面试·stl
无敌最俊朗@14 小时前
Qt 多线程与并发编程详解
linux·开发语言·qt
我登哥MVP14 小时前
HTML-CSS-JS-入门学习笔记
javascript·css·笔记·学习·html
Mintopia14 小时前
架构进阶 🏗 从 CRUD 升级到“大工程师视野”
前端·javascript·全栈
光影少年14 小时前
vue生态都有哪些?
前端·javascript·vue.js
消失的旧时光-194314 小时前
Kotlin Flow 与“天然背压”(完整示例)
android·开发语言·kotlin
ClassOps15 小时前
Kotlin invoke 函数调用重载
android·开发语言·kotlin
itslife15 小时前
vite 源码 - 创建 ws 服务
前端·javascript
小苏兮15 小时前
【C++】stack与queue的使用与模拟实现
开发语言·c++
高山有多高16 小时前
栈:“后进先出” 的艺术,撑起程序世界的底层骨架
c语言·开发语言·数据结构·c++·算法