前端生成二维码

直接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;
}
相关推荐
晚风_END7 分钟前
python|利用ffmpeg按顺序合并指定目录内的ts文件
开发语言·python·青少年编程·ffmpeg·vim·objective-c
web1309332039810 分钟前
[JAVA Web] 02_第二章 HTML&CSS
java·前端·html
黑客呀13 分钟前
Go Web开发之Revel - 网页请求处理流程
开发语言·前端·web安全·golang·系统安全
和小潘一起学AI14 分钟前
c++基于过程
开发语言·c++
轻口味15 分钟前
【每日学点鸿蒙知识】PersistentStorage持久化、插槽方法、相对布局、上拉加载下拉刷新、List联动滑动
前端·华为·harmonyos
叫我阿呆就好了16 分钟前
C 实现植物大战僵尸(二)
c语言·开发语言
SomeB1oody26 分钟前
【Rust自学】8.1. Vector
开发语言·后端·rust
s91236010127 分钟前
rust windwos 两个edit框
开发语言·后端·rust
星空海绵39 分钟前
2024年阅读记录
前端·程序员·架构
匹马夕阳41 分钟前
async/await 处理异步操作详解
前端