亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VUE微信H5生成二維碼海報(bào)保存在本地相冊(cè)的實(shí)現(xiàn)

 更新時(shí)間:2022年06月08日 08:39:34   作者:不許動(dòng)一二三  
本文主要介紹了VUE微信H5生成二維碼海報(bào)保存在本地相冊(cè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、效果

二、說(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)文章

最新評(píng)論