1 <template>
2 <!-- 生成二维码开放接口:
3 二维码内容[通常为url]
4 二维码大小[限制为正方形]
        二维码下方显示:文字
5 二维码中间显示:图片-->
6 <div id="meQrcode" :title="qrText">
7 <div class="qrcode_box">
8 <img
9 class="qrcode_canvas"
10 id="qrcode_canvas"
11 ref="qrcode_canvas"
12 alt="二维码本图"
13 />
14 <img
15 v-if="qrLogo"
16 class="qrcode_logo"
17 ref="qrcode_logo"
18 :src="qrLogo"
19 alt="公司logo"
20 />
21 <canvas
22 :width="qrSize"
23 :height="qrSize"
24 class="canvas"
25 ref="canvas"
26 ></canvas>
27 </div>
28 </div>
29 </template>
30 <script>
31 import QRCode from "qrcode";
32 import logo from "./logo11.png";
33 export default {
34 props: {
35 qrUrl: {
36 type: String,
37 default: "http://www.baidu.com/"
38 },
39 qrSize: {
40 type: Number,
41 default: 300
42 },
43 qrText: {
44 default: "百度一下,也不知道"
45 },
46 qrLogo: {
47 type: String,
48 default: logo
49 },
50 qrLogoSize: {
51 type: Number,
52 default: 40
53 },
54 qrTextSize: {
55 type: Number,
56 default: 14
57 }
58 },
59 data() {
60 return {};
61 },
62 methods: {
63 /**
64 * @argument qrUrl 二维码内容
65 * @argument qrSize 二维码大小
66 * @argument qrText 二维码中间显示文字
67 * @argument qrTextSize 二维码中间显示文字大小(默认16px)
68 * @argument qrLogo 二维码中间显示图片
69 * @argument qrLogoSize 二维码中间显示图片大小(默认为80)
70 */
71 handleQrcodeToDataUrl() {
72 let qrcode_canvas = this.$refs.qrcode_canvas;
73 let qrcode_logo = this.$refs.qrcode_logo;
74 let canvas = this.$refs.canvas;
75 const that = this;
76 QRCode.toDataURL(
77 this.qrUrl,
78 { errorCorrectionLevel: "H" },
79 (err, url) => {
80 qrcode_canvas.src = url;
81 // 画二维码里的logo// 在canvas里进行拼接
82 let ctx = canvas.getContext("2d");
83
84 setTimeout(() => {
85 //获取图片
86 ctx.drawImage(qrcode_canvas, 0, 0, that.qrSize, that.qrSize);
87 if (that.qrLogo) {
88 //设置logo大小
89 //设置获取的logo将其变为圆角以及添加白色背景
90 ctx.fillStyle = "#fff";
91 ctx.beginPath();
92 let logoPosition = (that.qrSize - that.qrLogoSize) / 2; //logo相对于canvas居中定位
93 let h = that.qrLogoSize + 10; //圆角高 10为基数(logo四周白色背景为10/2)
94 let w = that.qrLogoSize + 10; //圆角宽
95 let x = logoPosition - 5;
96 let y = logoPosition - 5;
97 let r = 5; //圆角半径
98 ctx.moveTo(x + r, y);
99 ctx.arcTo(x + w, y, x + w, y + h, r);
100 ctx.arcTo(x + w, y + h, x, y + h, r);
101 ctx.arcTo(x, y + h, x, y, r);
102 ctx.arcTo(x, y, x + w, y, r);
103 ctx.closePath();
104 ctx.fill();
           qrcode_logo.onload = function() {
105                 ctx.drawImage(
106 qrcode_logo,
107 logoPosition,
108 logoPosition,
109 that.qrLogoSize,
110 that.qrLogoSize
111 );
           }
            
112 }
113 if (that.qrText) {
114 //设置字体
115 let fpadd = 10; //规定内间距
116 ctx.font = "bold " + that.qrTextSize + "px Arial";
117 let tw = ctx.measureText(that.qrText).width; //文字真实宽度
118 let ftop = that.qrSize - that.qrTextSize; //根据字体大小计算文字top
119 let fleft = (that.qrSize - tw) / 2; //根据字体大小计算文字left
120 let tp = that.qrTextSize / 2; //字体边距为字体大小的一半可以自己设置
121 ctx.fillStyle = "#fff";
122 ctx.fillRect(
123 fleft - tp / 2,
124 ftop - tp / 2,
125 tw + tp,
126 that.qrTextSize + tp
127 );
128 ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
129 ctx.fillStyle = "#333";
130 ctx.fillText(that.qrText, fleft, ftop);
131 }
133 qrcode_canvas.src = canvas.toDataURL();
135 }, 0);
136 }
137 );
138 }
139 },
141 mounted() {
142 this.handleQrcodeToDataUrl();
143 },
144 updated() {
145 this.handleQrcodeToDataUrl();
146 },
147 };
148 </script>
149 <style scoped>
150 .qrcode_box,
151 #meQrcode {
152 display: inline-block;
153 }
154 .qrcode_box img {
155 display: none;
156 }
157 </style>

源码地址:https://gitee.com/yolanda624/coffer/tree/master/src/components/a-qrcode

最新文章

  1. Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
  2. Eclipse添加JPDL4 Schema校验
  3. Java设计模式之行为型模式
  4. SUBLIME TEXT 2 设置文件详解
  5. Python基础学习笔记(九)常用数据类型转换函数
  6. 【转】WPS文档怎么清除格式
  7. 【BZOJ 3110】 [Zjoi2013]K大数查询(整体二分)
  8. Spring IOC的配置使用(转)
  9. Android 屏幕相关概念(1)
  10. VisualSVN Server安装过程
  11. MongoDB下,启动服务时,出现“服务没有响应控制功能”解决方法
  12. (项目六)Mha-Atlas-MySQL高可用方案实践
  13. [python] 解决pip install download速度过慢问题 更换豆瓣源
  14. kubernetes 简单yaml文件运行例子deployment
  15. SpringMVC之文件上传异常处理
  16. (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘
  17. Duilib教程-HelloDuilib及DuiDesigner的简单使用
  18. php 通过curl上传图片
  19. 使用JavaScript获取CSS伪元素属性
  20. Python 开发中easy_install的安装及使用

热门文章

  1. NVIDIA Jetson™ TX1
  2. WriteDataToFile(filename,pJsonData,strlen(pJsonData)+1)
  3. vscode舒适的字体风格
  4. CF960G Bandit Blues 第一类斯特林数+分治+FFT
  5. UOJ131 [NOI2015] 品酒大会
  6. C/C++ C++ 11 std::function和std::bind用法
  7. sql语句中取整数和小数部分
  8. Python基础教程(017)--执行Python的方式解释器运行及其他几种解释器简介
  9. [LightOJ1070]Algebraic Problem
  10. python练习题之计算字符串中所有字符得和