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组件的更多相关文章

  1. iOS-带图片的二维码的生成(QRCode)

    https://blog.csdn.net/feng512275/article/details/82824650 2018年09月23日 20:29:45 筝风放风筝 阅读数:91   版权声明:本 ...

  2. 基于SignalR的消息推送与二维码描登录实现

    1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...

  3. .NET使用ZXing.NET生成中间带图片的二维码

    很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...

  4. 在云平台上基于Go语言+Google图表API提供二维码生成应用

    二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下 ...

  5. Flutter生成带图片的二维码

    现在的APP中经常需要用自己的信息生成一个二维码给别人扫,下面就介绍一下Flutter中怎么生成一个带图片的二维码. 需要用到的插件qr_flutter 首先在 pubspec.yaml 文件中添加以 ...

  6. jquery.qrcode.min.js(支持中文转化二维码)

    详情请看:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/jqueryqrcodeminjs/ 今天还是要讲一下关于二维码的知识,前 ...

  7. QRCode.js:使用 JavaScript 生成二维码

    什么是 QRCode.js? QRCode.js 是一个用于生成二维码的 JavaScript 库.主要是通过获取 DOM 的标签,再通过 HTML5 Canvas 绘制而成,不依赖任何库. 基本用法 ...

  8. js实现生成中间带图片的二维码

    之前需要实现生成中间带图片的二维码,所以找了半天终于找到一个可以用的.于是在这里记录一下. 下面是需要注意的几点: 1.使用的js为jquery-qrcode 但是已经经过别人的修改,和网上原来的那些 ...

  9. js生成二维码的jquery组件–qrcode

    js生成二维码的jquery组件–qrcode 2015/01/30 / 2508 VIEWS / JAVASCRIPT, JQUERY 有一些耗cpu的计算,完全可以在客户端上计算,比如生成二维码. ...

随机推荐

  1. shell设置用户自己的环境变量

  2. 手写Mybatis,还需要后面调整下

    参考博客 https://blog.csdn.net/Kurozaki_Kun/article/details/81482212 个人理解 读取Mybatis配置文件 数据库连接信息 读取Mapper ...

  3. HTML5 canvas绘制文本

    demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  4. mysql,分组后,再次进行过滤

    查出平均分大于80以上的班级 select class_id, avg(score) from students group by class_id having avg(score)>80;  ...

  5. C#将Json字符串转化为对象

    实体类: public class CheckData { public string msg; public string code; public string data; public stri ...

  6. BZOJ 1369: [Baltic2003]Gem(树形dp)

    传送门 解题思路 直接按奇偶层染色是错的,\(WA\)了好几次,所以要树形\(dp\),感觉最多\(log\)种颜色,不太会证. 代码 #include<iostream> #includ ...

  7. python 对 excel 的操作

    参考:https://www.php.cn/python-tutorials-422881.html  或 https://blog.51cto.com/wangfeng7399/2339556(使用 ...

  8. window安装nginx

    下载安装 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html: 下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如 ...

  9. Spring JDK动态代理

    1. 创建项目在 MyEclipse 中创建一个名称为 springDemo03 的 Web 项目,将 Spring 支持和依赖的 JAR 包复制到 Web 项目的 WEB-INF/lib 目录中,并 ...

  10. Django中的get()和filter()区别

    前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...