前端生成二维码

直接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;
}
相关推荐
lzhdim1 分钟前
SQL 入门 9:SQL 高级子查询:ANY、EXISTS 与多位置应用
java·开发语言·数据库·sql·mysql
Dream of maid2 分钟前
Python(11) 进程与线程
开发语言·python
踩着两条虫3 分钟前
从一行代码到一个生态:VTJ.PRO的创作之路
前端·低代码·ai编程
cici1587413 分钟前
非线性模型预测控制(NMPC)基于CasADi的MATLAB实现
开发语言·matlab
独特的螺狮粉25 分钟前
开源鸿蒙跨平台Flutter开发:量子态波函数坍缩系统-波动力学与概率云渲染架构
开发语言·flutter·华为·架构·开源·harmonyos
幼儿园技术家27 分钟前
嵌套 H5 的跨端通信:iOS / Android / 小程序 / 浏览器
前端·js or ts
冰暮流星34 分钟前
javascript之dom访问属性
开发语言·javascript·dubbo
lsx20240635 分钟前
SQL Auto Increment 自动增长
开发语言
t1987512837 分钟前
MATLAB模糊数学模型(Fuzzy Mathematical Model)实现指南
开发语言·matlab
一只小阿乐37 分钟前
TypeScript中的React开发
前端·javascript·typescript·react