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

效果

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

相关推荐
z落落17 小时前
C# 泛型方法(原理、类型推断、多泛型参数)+泛型效率(普通类型 VS Object装箱 VS 泛型)
开发语言·c#
L_090717 小时前
【C++】异常
开发语言·c++
疯狂SQL17 小时前
JWT 在线解码、验签、生成一篇讲透:附前端实现、工具架构与在线体验地址
javascript·jwt·编解码·jwt测试
前端一小卒18 小时前
不手写代码的第 30 天,我才明白前端这个岗位还剩什么
前端·javascript·ai编程
Ajie'Blog18 小时前
Copilot Agent Tasks API 开放:AI 编程开始进入后台任务时代
服务器·前端·javascript·人工智能·copilot·ai编程
世辰辰辰18 小时前
批量修改图片/文本名子
开发语言·python·批量修改文件名
老毛肚18 小时前
jeecgboot vue TS & 模板化 04
前端·javascript·vue.js
晓131318 小时前
【Cocos Creator 2.x】篇——第二章 入门
javascript·游戏引擎
z落落20 小时前
C# 四种特殊类:抽象类、密封类、静态类、部分类
开发语言·c#
Electrolux20 小时前
[onlyoffice-v9]纯前端怎么实现编辑预览office
前端·javascript·github