Vue如何將當(dāng)前窗口截圖并將數(shù)據(jù)base64轉(zhuǎn)為png格式傳給服務(wù)器
更新時(shí)間:2023年10月11日 14:42:58 作者:vanora1111
這篇文章主要介紹了Vue如何將當(dāng)前窗口截圖并將數(shù)據(jù)base64轉(zhuǎn)為png格式傳給服務(wù)器,通過實(shí)例代碼介紹了將當(dāng)前窗口截圖,并將數(shù)據(jù)存儲(chǔ)下來(lái),需要的朋友可以參考下
前言
記錄來(lái)源于需求
1、將當(dāng)前窗口截圖,并將數(shù)據(jù)存儲(chǔ)下來(lái)
export default {
data() {
return {
image: '' // 存儲(chǔ)數(shù)據(jù)
}
}
mounted() {
setTimeout(() => {
// 拿到當(dāng)前dom里 你需要截取的位置的id
const dom = document.querySelector("#cesium canvas");
if (dom) {
// 將選擇的dom元素轉(zhuǎn)換為PNG格式的DataURL字符串,打印出來(lái)是base64數(shù)據(jù)
this.image = dom.toDataURL("image/png");
}
},500)
}
}2、定義將base64轉(zhuǎn)png的方法
methods: {
base64ImgtoFile(dataurl, filename = "file") {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = dataurl;
image.onload = () => {
const canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
canvas.toBlob((blob) => {
const file = new File([blob], `${filename}.png`, {
type: "image/png"
});
resolve(file);
}, "image/png");
};
image.onerror = (error) => reject(error);
});
},
async convertBase64ToPNG(dataurl, fileName) {
try {
const file = await this.base64ImgtoFile(dataurl, fileName);
// 使用轉(zhuǎn)換后的文件進(jìn)行后續(xù)操作
return file;
} catch (error) {
console.error(error);
}
},
}3、完整代碼
<template>
<el-button @click="handleSaveMap">提交</el-button>
</template>
<script>
export default {
data() {
return {
image: "" // 存儲(chǔ)數(shù)據(jù)
};
},
mounted() {
setTimeout(() => {
// 拿到當(dāng)前dom里 你需要截取的位置的id
const dom = document.querySelector("#cesium canvas");
if (dom) {
// 將選擇的dom元素轉(zhuǎn)換為PNG格式的DataURL字符串,打印出來(lái)是base64數(shù)據(jù)
this.image = dom.toDataURL("image/png");
}
}, 500);
},
methods: {
base64ImgtoFile(dataurl, filename = "file") {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = dataurl;
image.onload = () => {
const canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
canvas.toBlob((blob) => {
const file = new File([blob], `${filename}.png`, {
type: "image/png"
});
resolve(file);
}, "image/png");
};
image.onerror = (error) => reject(error);
});
},
async convertBase64ToPNG(dataurl, fileName) {
try {
const file = await this.base64ImgtoFile(dataurl, fileName);
// 使用轉(zhuǎn)換后的文件進(jìn)行后續(xù)操作
return file;
} catch (error) {
console.error(error);
}
},
// 存儲(chǔ)數(shù)據(jù)到服務(wù)端
async generateImages(image) {
const file = new FileReader(); // 創(chuàng)建對(duì)象
file.readAsDataURL(image); // 讀取文件
const res = await uploadApi.uploadImage(image); // 對(duì)接上傳接口
const { object, msg, success } = res.data;
if (success) {
this.areaPicture = object.key;
} else this.$message.error(msg);
},
// 保存數(shù)據(jù)
async handleSaveMap() {
const loading = this.$loading({
lock: true,
text: "保存中,請(qǐng)稍后...",
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.7)"
});
// 截取當(dāng)前窗口數(shù)據(jù)
await this.screenshotView();
// 轉(zhuǎn)換當(dāng)前base64圖片數(shù)據(jù)為png
let image = await this.convertBase64ToPNG(this.image, this.mapName);
// 存儲(chǔ)到服務(wù)端
if (image) {
await this.generateImages(image);
console.log(this.areaPicture, "this.areaPicture");
}
loading.close();
}
}
};
</script>總結(jié)
到此這篇關(guān)于在Vue里,將當(dāng)前窗口截圖,并將數(shù)據(jù)base64轉(zhuǎn)為png格式傳給服務(wù)器的文章就介紹到這了,更多相關(guān)Vue數(shù)據(jù)base64轉(zhuǎn)為png格式傳服務(wù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-03-03
vue實(shí)現(xiàn)table表格里面數(shù)組多層嵌套取值
這篇文章主要介紹了vue實(shí)現(xiàn)table表格里面數(shù)組多層嵌套取值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue任意關(guān)系組件通信與跨組件監(jiān)聽狀態(tài)vue-communication
這篇文章主要介紹了vue任意關(guān)系組件通信與跨組件監(jiān)聽狀態(tài)vue-communication,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

