前端生成二维码

直接img标签显示
npm i use_qrcode
npm包地址

<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' })
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值