Vue中兩種生成二維碼(帶logo)并下載方式總結(jié)
1.現(xiàn)在需要在頁(yè)面中生成一個(gè)二維碼,并附帶上公司的logo
生成的二維碼需要顯示logo,并且點(diǎn)擊可以二維碼可以下載保存,有兩種方案供選擇(vue-qr、qrcode)
2.vue-qr庫(kù)使用(方案一)
npm i vue-qr@4.0.9
我的nodejs版本12.13.0,大家可以使用cnpm下載更好。
html代碼:
<template> <div class="main"> <div> <vue-qr :text="codeText" :size="150" :margin="20" colorDark="#333" backgroundColor="#eee" :logoSrc="lgoImg" logoScale="0.21" logoMargin="25px" :callback="getCode" ></vue-qr> </div> </div> </template>
JS代碼:
<script> import VueQr from 'vue-qr' export default { name:'QR', components:{VueQr}, data() { return { codeText: 'https://blog.csdn.net/yuansusu_?spm=1000.2115.3001.53', lgoImg: require('../assets/111.png'), }; }, methods: { getCode(codeImg) { console.log('二維碼圖片', codeImg); }, } } </script>
- text是要生成的內(nèi)容
- size表示二維碼的寬高,寬高一致
- margin二維碼圖像的外邊距, 默認(rèn) 20px
- colorDark實(shí)點(diǎn)的顏色
- backgroundColor背景顏色(背景圖片使用bgSrc或gifBgSrc)
- logoSrc嵌入中間的logo地址
- logoScale用于計(jì)算 LOGO 大小的值, 過(guò)大將導(dǎo)致解碼失敗, LOGO 尺寸計(jì)算公式
logoScale*(size-2*margin)
, 默認(rèn) 0.2 - logoMargin設(shè)置LOGO 標(biāo)識(shí)周?chē)目瞻走吙? 默認(rèn)為0
- callback生成的二維碼 Data URI 可以在回調(diào)中取得,第一個(gè)參數(shù)為二維碼 data URL, 第二個(gè)參數(shù)為 props 傳過(guò)來(lái)的 qid(因?yàn)槎S碼生成是異步的,所以加個(gè) id 用于排序)
官網(wǎng)文檔:vue-qr - npm(文檔是中文,可以放心食用)
3.qrcode庫(kù)使用并帶下載方式(方案二)(推薦)
npm i qrcode@1.5.1
使用qecode配置的logo更為靈活,并且在不是vue項(xiàng)目中也能同樣使用
html(部分)和css代碼:
<div class="qr-code"> <canvas id="QRCode_header" ref="canvas" title="掃描二維碼" ></canvas> <div class="mask-code" @click="saveCode"> <i></i><span>保存二維碼</span> </div> </div>
<style scoped> .qr-code{ display: flex; width:fit-content; width:-webkit-fit-content; width:-moz-fit-content; position: relative; } .qr-code:hover>div{ z-index: 0; } .mask-code{ position: absolute; width: 100%; height: 100%; background: rgba(0, 0, 0,0.4); display: flex; justify-content: center; align-items: center; cursor: pointer; z-index: -1; } .mask-code i{ display: inline-block; width: 25px; height: 25px; background-image: url(../assets/save_img.png); background-size: cover; } .mask-code span{ color: white; } </style>
可以將上面的內(nèi)容封裝到組件中,通過(guò)props來(lái)傳遞想要生成的內(nèi)容
JS代碼:
<script> import QRCode from "qrcode"; //引入生成二維碼插件 export default { name:'CodeS', props:{ canvasWidth:{ default:200, type:Number }, canvasHeight:{ default:200, type:Number }, url:{ default:'', type:String, required:true }, logoUrl:{ default:'', type:String, // required:true } }, methods:{ getQRCode() { let opts = { errorCorrectionLevel: "H",//容錯(cuò)級(jí)別,指二維碼被遮擋可以?huà)叱鼋Y(jié)果的區(qū)域比例 type: "image/png",//生成的二維碼類(lèi)型 quality: 0.3,//二維碼質(zhì)量 margin: 5,//二維碼留白邊距 width: this.canvasWidth,//寬 height: this.canvasHeight,//高 text: "1111",//二維碼內(nèi)容 color: { light: "#eaeaea"http://背景色 } }; // 將獲取到的數(shù)據(jù)(val)畫(huà)到msg(canvas)上,加上時(shí)間戳動(dòng)態(tài)生成二維碼 QRCode.toCanvas(this.$refs.canvas, this.url, opts, function (error) { if(error){ console.log('加載失敗!'); } }); }, saveCode(){ //下載二維碼 let base64Img = this.$refs.canvas.toDataURL('image/jpg'); //創(chuàng)建下載標(biāo)簽,然后設(shè)置鏈接和圖片名稱(chēng) let a = document.createElement('a') a.href = base64Img a.download = '二維碼'+Date.now() a.click() //銷(xiāo)毀元素 a.remove() } }, mounted() { this.getQRCode() //設(shè)置logo圖標(biāo) if(this.logoUrl!= ''){ let myCanvas = this.$refs.canvas let ctx = myCanvas.getContext('2d') // 在Canvas畫(huà)布 添加圖片 let img = new Image() img.crossOrigin = 'Anonymous';//解決Canvas.toDataURL 圖片跨域問(wèn)題 img.src = this.logoUrl; img.onload = ()=>{ //第一個(gè)設(shè)置的元素,第二三是位置,后面兩個(gè)是寬和高 //居中的位置計(jì)算為 (二維碼寬度-img寬度)/2 let codeWidth = (this.canvasWidth *0.75)/2 let codeHeight = (this.canvasHeight * 0.75)/2 ctx.drawImage(img, codeWidth, codeHeight,this.canvasWidth*0.25,this.canvasHeight*0.25) } } }, } </script>
errorCorrectionLevel糾錯(cuò)級(jí)別有四個(gè):
- L(7%)
- M(15%)
- Q(25%)
- H(30%)
級(jí)別越高,logo占的大小就越大。
通過(guò)鼠標(biāo)點(diǎn)擊二維碼,就可以將二維碼保存在本地
文檔地址:qrcode - npm(無(wú)中文)
總結(jié)
到此這篇關(guān)于Vue中兩種生成二維碼(帶logo)并下載方式的文章就介紹到這了,更多相關(guān)Vue生成二維碼帶logo并下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn)
這篇文章主要介紹了使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)拖拽滾動(dòng)效果插件
這篇文章主要介紹了Vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)拖拽滾動(dòng)效果插件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12Vue動(dòng)態(tài)創(chuàng)建注冊(cè)component的實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Vue動(dòng)態(tài)創(chuàng)建注冊(cè)component的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06vue+iview tabs context-menu 彈出框修改樣式的方法
今天遇到一個(gè)需求說(shuō)頁(yè)面頂部的菜單右鍵彈出框離得有點(diǎn)遠(yuǎn),需要我們做調(diào)整,下面小編給大家分享下vue+iview tabs context-menu 彈出框修改樣式的方法,感興趣的朋友跟隨小編一起看看吧2024-06-06詳解Vue學(xué)習(xí)筆記入門(mén)篇之組件的內(nèi)容分發(fā)(slot)
這篇文章主要介紹了詳解Vue學(xué)習(xí)筆記入門(mén)篇之組件的內(nèi)容分發(fā)(slot),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Vue.js實(shí)戰(zhàn)之使用Vuex + axios發(fā)送請(qǐng)求詳解
這篇文章主要給大家介紹了關(guān)于Vue.js使用Vuex與axios發(fā)送請(qǐng)求的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04