vuejs如何利用qrcode生成二维码,下面web建站小编给大家详细介绍一下具体代码!
安装脚手架
npm i qrcode -S npm install --save @types/qrcode
二维码组件介绍
<template>
<canvas id="canvas" ref="canvas" :width="width" :height="height"></canvas>
</template>
<script setup>
import QRCode from "qrcode";
import { onMounted, ref } from "vue";
const props = defineProps({
//二维码存储内容
qrUrl: {
type: String,
default: "Hello World"
},
// canvas width
width: {
type: Number,
default: 400
},
// canvas height
height: {
type: Number,
default: 400
},
// 二维码尺寸(正方形 长宽相同)
qrSize: {
type: Number,
default: 360
},
// 二维码底部文字
qrText: {
type: String,
default: "Hello World"
},
//底部说明文字字号
qrTextSize: {
type: Number,
default: 24
}
});
const qrCodeOption = {
errorCorrectionLevel: "H",
width: props.qrSize,
version: 7
};
const canvas = ref<HTMLCanvasElement>();
/**
* @argument qrUrl 二维码内容
* @argument qrSize 二维码大小
* @argument qrText 二维码中间显示文字
* @argument qrTextSize 二维码中间显示文字大小(默认16px)
*/
const handleQrcode = () => {
let dom = canvas.value as HTMLCanvasElement;
QRCode.toDataURL(props.qrUrl, qrCodeOption)
.then((url: string) => {
// 画二维码里的logo// 在canvas里进行拼接
const ctx = dom.getContext("2d") as CanvasRenderingContext2D;
const image = new Image();
image.src = url;
setTimeout(() => {
ctx.drawImage(image, (props.width - props.qrSize) / 2, 0, props.qrSize, props.qrSize);
if (props.qrText) {
//设置字体
ctx.font = "bold " + props.qrTextSize + "px Arial";
let tw = ctx.measureText(props.qrText).width; // 文字真实宽度
let ftop = props.qrSize - props.qrTextSize; // 根据字体大小计算文字top
let fleft = (props.width - tw) / 2; // 根据字体大小计算文字left
ctx.fillStyle = "#fff";
ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
ctx.fillStyle = "#333";
ctx.fillText(props.qrText, fleft, ftop);
}
}, 0);
})
.catch((err: Error) => {
console.error(err);
});
};
onMounted(() => {
handleQrcode();
});
</script>
二维码下载
const savePic = () => {
let dom = canvas.value as HTMLCanvasElement;
let a = document.createElement("a");
//将二维码面板处理为图片
a.href = dom.toDataURL("image/png", 0.5);
a.download = props.qrUrl + ".png";
a.click();
};
defineExpose({ savePic });
上面是“vuejs如何利用qrcode生成二维码”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_3090.html
workflows工作流
爱因斯坦在做实验3d动漫ComfyUI工作流
冬天的严寒里红梅枝上停留着一只鸟
stvmccrr风格的玫瑰花ComfyUI工作流
图生图工作流:粉红色梦幻家园comfyui工作流
庭院,彩色玫瑰,云雾笼罩comfyui工作流
一个十几岁的美国女孩穿着黄色连帽衫在黑暗和空虚的背景下闲逛
Latent放大comfyui工作流
一棵树从鸟笼里长出来的梦幻般场景
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

js实现table表格动态新增行和列表
利用html5+css3实现滚雪球效果(附代码)
用canvas实现画板涂鸦效果
利用js做一个炫酷音乐背景效果
纯css制作卡通头像(随鼠标转头)
css3+js菜单点击动态效果
css3实现星球旋转
利用js+css3做一个小鱼游泳特效










