VUE微信H5生成二維碼海報保存在本地相冊的實現(xiàn)
一、效果

二、說明
公司需求:宣傳海報從后臺獲取,二維碼地址也從后臺獲得,然后生成一個海報,海報上固定位置放二維碼,長按圖片可以保存在本地相冊(其實前面的需求是點擊按鈕,下載海報圖到本地相冊,然后h5下載的實現(xiàn)原理是生成一個a標(biāo)簽鏈接,然后下載,在電腦模擬器可以下載,在微信瀏覽器一點反應(yīng)沒有,所以退一步,長按保存),下面是使用版本,UI插件用的是vant(有贊)
- vue:“^2.6.11”,
- “html2canvas”: “1.4.1”,
- “vant”: “^2.8.1”,
- “vue_qrcodes”: “^1.1.3”
三、思路
從后端獲取海報圖,獲取二維碼鏈接,然后將二維碼定位到圖片固定位置,最后通過html2canvas生成canvas,然后通過canvas.toDataURL(“image/png”)生成
四、效果


從第三張圖就可以看到長按保存的就是帶有二維碼的海報圖了
五、代碼實現(xiàn)
實現(xiàn)的原理就是:先把二維碼定位到海報圖上,然后通過html2canvas生成帶二維碼的海報,最后用這個圖片覆蓋在imageWrapper1元素上,才能下載到帶二維碼的海報圖,這也是為什么要等待框顯示正在生成海報圖。
<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">長按保存圖片</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: "海報生成中,請稍等",
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>
六、遇到的問題
1.如果你把海報放到云上(騰訊云,阿里云,七牛云),就是放在CDN上,那么云一定要設(shè)置以下的頭文件,如果你不設(shè)置這個頭文件,就有跨域問題,你就一直會生成不成功,實用html2canvas的話只能生成一個空白背景的二維碼,這個搞了好久,采用了放在服務(wù)器本地,但是服務(wù)器本地圖片下載又慢,影響體驗,搞了幾天,才知道一定要設(shè)置!?。?/p>
到此這篇關(guān)于VUE微信H5生成二維碼海報保存在本地相冊的文章就介紹到這了,更多相關(guān)VUE微信H5生成二維碼海報保存在本地相冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element上傳組件循環(huán)引用及簡單時間倒計時的實現(xiàn)
這篇文章主要介紹了element上傳組件循環(huán)引用及簡單時間倒計時的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
關(guān)于vue中hash和history的區(qū)別與使用圖文詳解
vue-router中有hash模式和history模式,下面這篇文章主要給大家介紹了關(guān)于vue中hash和history的區(qū)別與使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法詳解
這篇文章主要給大家介紹了關(guān)于vue列表數(shù)據(jù)刪除后主動刷新頁面及刷新方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
關(guān)于新建的vue3項目一直提示代碼格式警告的問題
這篇文章主要介紹了關(guān)于新建的vue3項目一直提示代碼格式警告的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue項目中如何實現(xiàn)element-ui組件按需引入
這篇文章主要介紹了vue項目中如何實現(xiàn)element-ui組件按需引入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
vue3如何添加eslint校驗(eslint-plugin-vue)
這篇文章主要介紹了vue3如何添加eslint校驗(eslint-plugin-vue),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

