前端生成二维码

直接img标签显示

npm i use_qrcode
npm包地址

javascript 复制代码
<img :src="qrcode" alt="QR Code" />
const txt: any = ref('https://baidu.com')
const qrcode = useQRCode(txt)
const qrcodeLogo = useQRCode(txt, { logoSrc: 'https://www.antdv.com/assets/logo.1ef800a8.svg' })
javascript 复制代码
import { useQRCode as _useQRCode } from "@vueuse/integrations/useQRCode";
import * as QRCode from "qrcode";
import { toRef, isClient } from "@vueuse/shared";
import { ref, watch } from "vue-demi";

export function useQRCode(text: string, options?: any) {
  if (options?.logoSrc) return useLogoQRCode(text, options);
  return _useQRCode(text, options);
}

export function useLogoQRCode(text, options) {
  const src = toRef(text);
  const result = ref("");
  const canvas: any = document.createElement("canvas");
  canvas.width = 132;
  canvas.height = 132;
  const logoImgSrc = ref(options.logoSrc);
  const labelText = "";
  watch(
    src,
    async (value) => {
      if (src.value && isClient) result.value = "";
      await QRCode.toDataURL(value, options, (err: any, url: string) => {
        let imgQRCode = new Image();
        imgQRCode.src = url;
        imgQRCode.crossOrigin = "anonymous";

        let img = new Image();
        img.src = logoImgSrc.value;
        img.crossOrigin = "anonymous";
        try {
          let ctx = (<HTMLCanvasElement>canvas).getContext("2d");
       
          // ctx.clearRect(0, 0, canvas.width, canvas.height);
          setTimeout(() => {
            ctx.drawImage(
              imgQRCode,
              0,
              0,
              imgQRCode.width,
              imgQRCode.height,
              0,
              0,
              canvas.width,
              canvas.width
            );
            
            let canvas_Centre_Horizontal = canvas.width / 2;
            let canvas_Centre_Vertical = canvas.width / 2;

            let logoSize_Horizontal = canvas.width * 0.16;
            let logoSize_Vertical = canvas.width * 0.16;

            let imageStart_Horizontal =
              canvas_Centre_Horizontal - logoSize_Horizontal / 2;
            let imageStart_Vertical =
              canvas_Centre_Vertical - logoSize_Vertical / 2;

            ctx.drawImage(
              img,
              //0, 0, THIS.img.nativeElement.width, THIS.img.nativeElement.height,
              imageStart_Horizontal,
              imageStart_Vertical,
              logoSize_Horizontal,
              logoSize_Vertical
            );
            ctx.font = "10px Arial";
            ctx.textAlign = "center";
            ctx.fillText(labelText, canvas.width / 2, canvas.height - 10);

            result.value = canvas.toDataURL("image/png");
          }, 50);
        } catch (ex) {
          console.log(ex);
        }
      });
    },
    { immediate: true }
  );
  return result;
}
相关推荐
无限大.5 分钟前
c语言200例 067
java·c语言·开发语言
余炜yw7 分钟前
【Java序列化器】Java 中常用序列化器的探索与实践
java·开发语言
篝火悟者8 分钟前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
Death20011 分钟前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
六点半88812 分钟前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法
惜.己12 分钟前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5
niu_sama16 分钟前
基于muduo库函数实现protobuf协议的通信
开发语言·qt
不写八个18 分钟前
Qt教程(001):Qt概述与安装
开发语言·qt
quaer23 分钟前
Open-Sora全面开源?
开发语言·算法·机器学习·matlab·矩阵
hakesashou28 分钟前
python如何比较字符串
linux·开发语言·python