VUE微信H5生成二維碼海報(bào)保存在本地相冊(cè)的實(shí)現(xiàn)
一、效果
二、說(shuō)明
公司需求:宣傳海報(bào)從后臺(tái)獲取,二維碼地址也從后臺(tái)獲得,然后生成一個(gè)海報(bào),海報(bào)上固定位置放二維碼,長(zhǎng)按圖片可以保存在本地相冊(cè)(其實(shí)前面的需求是點(diǎn)擊按鈕,下載海報(bào)圖到本地相冊(cè),然后h5下載的實(shí)現(xiàn)原理是生成一個(gè)a標(biāo)簽鏈接,然后下載,在電腦模擬器可以下載,在微信瀏覽器一點(diǎn)反應(yīng)沒(méi)有,所以退一步,長(zhǎng)按保存),下面是使用版本,UI插件用的是vant(有贊)
- vue:“^2.6.11”,
- “html2canvas”: “1.4.1”,
- “vant”: “^2.8.1”,
- “vue_qrcodes”: “^1.1.3”
三、思路
從后端獲取海報(bào)圖,獲取二維碼鏈接,然后將二維碼定位到圖片固定位置,最后通過(guò)html2canvas生成canvas,然后通過(guò)canvas.toDataURL(“image/png”)生成
四、效果
從第三張圖就可以看到長(zhǎng)按保存的就是帶有二維碼的海報(bào)圖了
五、代碼實(shí)現(xiàn)
實(shí)現(xiàn)的原理就是:先把二維碼定位到海報(bào)圖上,然后通過(guò)html2canvas生成帶二維碼的海報(bào),最后用這個(gè)圖片覆蓋在imageWrapper1元素上,才能下載到帶二維碼的海報(bào)圖,這也是為什么要等待框顯示正在生成海報(bào)圖。
<template> <!--子組件--> <div class="posterpopup"> <van-popup v-model="popupVisible" @closed="closePopup"> <div id="imageWrapper1"> <img class="posterpopup-img" :src="popupObj.posterurl || popupObj.photourl" /> <div class="qrcode1" ref="qrcode" id="imageWrapper"> <qrcode :url="qrUrl" colorDark="#000" colorLight="#fff" :wid="wid" :hei="wid" :imgwid="imgwid" :imghei="imgwid" ></qrcode> </div> </div> <div class="posterpopup-img1" v-if="posterUrl"> <img :src="posterUrl" id="posterUrl" /> </div> <div class="posterpopup-wrap"> <img src="@/assets/image/download.png" alt="" /> <div class="wrap-text">長(zhǎng)按保存圖片</div> </div> </van-popup> </div> </template> <script> import qrcode from "vue_qrcodes"; import html2canvas from "html2canvas"; export default { name: "PosterPopup", props: { popupVisible: false, popupObj: { type: Object, default: {}, }, qrUrl: "", }, components: { qrcode, }, data() { return { imgwid: 30, // 二維碼logo寬度 wid: 80, // 二維碼寬度 wrapWid: 80, // 外邊框?qū)挾? posterUrl: "", controllShow: true, }; }, methods: { closePopup() { this.posterUrl = '' this.$emit("closePopup"); }, toSaveImage() { // 保存圖片 this.$nextTick(async () => { var imageWrapper = document.getElementById("imageWrapper1"); try { let canvas = await html2canvas(imageWrapper, { height: imageWrapper.offsetHeight, width: imageWrapper.offsetWidth, backgroundColor: null, useCORS: true, allowTaint: true, scrollX: 0, scrollY: 0, dpi: window.devicePixelRatio * 2, // dpi: 300 }); this.controllShow = false; this.posterUrl = canvas.toDataURL("image/png"); this.$Toast.clear(); } catch (err) {} }); }, dataURLToBlob(dataurl) { let arr = dataurl.split(","); let mime = arr[0].match(/:(.*?);/)[1]; let bstr = atob(arr[1]); let n = bstr.length; let u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); }, }, watch: { popupVisible(newVal, oldVal) { if (newVal) { this.$Toast.loading({ message: "海報(bào)生成中,請(qǐng)稍等", duration: 0, className: "zIndex", }); this.$nextTick(() => { this.toSaveImage(); }); } else { this.controllShow = true; this.$Toast.clear(); } }, }, }; </script> <style lang="scss" scoped> .posterpopup { .van-popup { background-color: transparent; overflow-y: hidden; width: 568px; height: 90vh; } #imageWrapper1 { position: absolute; min-width: 568px; min-height: 990px; } .posterpopup-img { width: 568px; height: 100%; } .posterpopup-img1 { width: 568px; height: 990px; & img { width: 100%; height: 13.2rem; } } .posterpopup-wrap { position: absolute; width: 302px; height: 70px; bottom: 50px; left: 50%; transform: translateX(-50%); // margin-top: 100px; margin: 39px auto 0; & > img { width: 100%; height: 100%; } .wrap-text { position: absolute; top: 50%; transform: translateY(-50%); right: 30px; font-size: 30px; color: #fff; } } #imageWrapper { position: absolute; right: 30px; bottom: 35px; } #posterUrl { position: absolute; z-index: 2002; top: 0; left: 0; right: 0; bottom: 90px; margin: 0 auto; width: 568px; } } </style>
<!----> <posterPopup :popupVisible="popupVisible" :popupObj="popupObj" @closePopup="closePopup" :qrUrl="qrUrl"></posterPopup>
六、遇到的問(wèn)題
1.如果你把海報(bào)放到云上(騰訊云,阿里云,七牛云),就是放在CDN上,那么云一定要設(shè)置以下的頭文件,如果你不設(shè)置這個(gè)頭文件,就有跨域問(wèn)題,你就一直會(huì)生成不成功,實(shí)用html2canvas的話只能生成一個(gè)空白背景的二維碼,這個(gè)搞了好久,采用了放在服務(wù)器本地,但是服務(wù)器本地圖片下載又慢,影響體驗(yàn),搞了幾天,才知道一定要設(shè)置!??!
到此這篇關(guān)于VUE微信H5生成二維碼海報(bào)保存在本地相冊(cè)的文章就介紹到這了,更多相關(guān)VUE微信H5生成二維碼海報(bào)保存在本地相冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element上傳組件循環(huán)引用及簡(jiǎn)單時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)
這篇文章主要介紹了element上傳組件循環(huán)引用及簡(jiǎn)單時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10關(guān)于vue中hash和history的區(qū)別與使用圖文詳解
vue-router中有hash模式和history模式,下面這篇文章主要給大家介紹了關(guān)于vue中hash和history的區(qū)別與使用的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁(yè)面及刷新方法詳解
這篇文章主要給大家介紹了關(guān)于vue列表數(shù)據(jù)刪除后主動(dòng)刷新頁(yè)面及刷新方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05關(guān)于新建的vue3項(xiàng)目一直提示代碼格式警告的問(wèn)題
這篇文章主要介紹了關(guān)于新建的vue3項(xiàng)目一直提示代碼格式警告的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue項(xiàng)目中如何實(shí)現(xiàn)element-ui組件按需引入
這篇文章主要介紹了vue項(xiàng)目中如何實(shí)現(xiàn)element-ui組件按需引入,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue使用el-upload實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了vue使用el-upload實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue3如何添加eslint校驗(yàn)(eslint-plugin-vue)
這篇文章主要介紹了vue3如何添加eslint校驗(yàn)(eslint-plugin-vue),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01vue3使用quill富文本編輯器保姆級(jí)教程(附踩坑解決)
這篇文章主要給大家介紹了關(guān)于vue3使用quill富文本編輯器保姆級(jí)教程的相關(guān)資料,在許多網(wǎng)站和應(yīng)用程序中富文本編輯器是一種常見(jiàn)的工具,它使用戶能夠以直觀的方式創(chuàng)建和編輯文本內(nèi)容,需要的朋友可以參考下2023-11-11vue2手機(jī)APP項(xiàng)目添加開屏廣告或者閃屏廣告
這篇文章主要為大家詳細(xì)介紹了vue2手機(jī)APP項(xiàng)目添加開屏廣告或者閃屏廣告的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11