基于QRcode的带有文字+图片的二维码的Vue组件
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
基于QRcode的带有文字+图片的二维码的Vue组件的更多相关文章
- iOS-带图片的二维码的生成(QRCode)
https://blog.csdn.net/feng512275/article/details/82824650 2018年09月23日 20:29:45 筝风放风筝 阅读数:91 版权声明:本 ...
- 基于SignalR的消息推送与二维码描登录实现
1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...
- .NET使用ZXing.NET生成中间带图片的二维码
很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...
- 在云平台上基于Go语言+Google图表API提供二维码生成应用
二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下 ...
- Flutter生成带图片的二维码
现在的APP中经常需要用自己的信息生成一个二维码给别人扫,下面就介绍一下Flutter中怎么生成一个带图片的二维码. 需要用到的插件qr_flutter 首先在 pubspec.yaml 文件中添加以 ...
- jquery.qrcode.min.js(支持中文转化二维码)
详情请看:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/jqueryqrcodeminjs/ 今天还是要讲一下关于二维码的知识,前 ...
- QRCode.js:使用 JavaScript 生成二维码
什么是 QRCode.js? QRCode.js 是一个用于生成二维码的 JavaScript 库.主要是通过获取 DOM 的标签,再通过 HTML5 Canvas 绘制而成,不依赖任何库. 基本用法 ...
- js实现生成中间带图片的二维码
之前需要实现生成中间带图片的二维码,所以找了半天终于找到一个可以用的.于是在这里记录一下. 下面是需要注意的几点: 1.使用的js为jquery-qrcode 但是已经经过别人的修改,和网上原来的那些 ...
- js生成二维码的jquery组件–qrcode
js生成二维码的jquery组件–qrcode 2015/01/30 / 2508 VIEWS / JAVASCRIPT, JQUERY 有一些耗cpu的计算,完全可以在客户端上计算,比如生成二维码. ...
随机推荐
- redis的hmset乐观锁的实现
1.lua脚本(集成实现了乐观锁,hmset ,expire等) local key=KEYS[1]; local oldVerion=tonumber(ARGV[1]); local seconds ...
- 毕设问题(2) fastjson 的过滤器使用(SSH hibernate)以及转换对象出现 $ref
毕业设计,用SSH框架,但是想要做出异步请求数据的效果,使用了ajax方式,其中数据传递的类型为json.ajax使用用的jQuery的ajax.其实struts里也支持ajax功能,但是我觉得用太多 ...
- linux安装jdk环境(多种方式)
通过tar.gz压缩包安装 此方法适用于绝大部分的linux系统 1.先下载tar.gz的压缩包,这里使用官网下载. 进入: http://www.oracle.com/technetwork/jav ...
- 《x的奇幻之旅》:有趣的数学科普
本书是相对比较少见的数学方面的科普书.从最简单的阿拉伯数字.加减法,一直到概率统计.微积分.群论.拓扑.微分几何,每个主题都用几千字做一些深入浅出的介绍.写的相当的有趣. 在书中又一次看到这个有趣的事 ...
- rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例
// 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...
- 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)
Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...
- Sass-注释
注释对于一名程序员来说,是极其重要,良好的注释能帮助自己或者别人阅读源码.在 Sass 中注释有两种方式,我暂且将其命名为: 1.类似 CSS 的注释方式,使用 ”/* ”开头,结属使用 ”*/ ”2 ...
- deepin开机进入intramfs无法正常开机
问题原因:由于非正常关机导致文件系统受损 解决方法: fsck /dev/sda6 注释:如果输入上面的指令只是出现如下提示 fsck from util-linux-ng 2.17.2(后面的数字可 ...
- Codeforces 963B Destruction of a Tree 思维+dfs
题目大意: 给出一棵树,每次只能摧毁有偶数个度的节点,摧毁该节点后所有该节点连着的边都摧毁,判断一棵树能否被摧毁,若能,按顺序输出摧毁的点,如果有多种顺序,输出一种即可 基本思路: 1)我一开始自然而 ...
- 【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
题目如下: In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pair ...