vue中使用帶隱藏文本信息的圖片、圖片水印的方法
一.帶隱藏文本信息的圖片
通過RGB 分量值的小量變動(dòng),不影響對(duì)圖片的識(shí)別。因此,我們可以在圖片加入文字信息。
最終達(dá)到如下效果:

首先,在該組件中加入img用于顯示圖片
<canvas ref="canvas" v-show="0"></canvas> <img :src="imageUrl" v-if="imageUrl"/>
調(diào)用方法
encryptionImg({
width = '',
height = '',
content = '',
}){
let img = this.img
const imgRatio = img.naturalWidth / img.naturalHeight;
const ctxWidth = width || img.naturalWidth;
const ctxHeight = height || ctxWidth / imgRatio;
this.canvas.width = ctxWidth;
this.canvas.height = ctxHeight;
const ctx = this.ctx;
ctx.font = '16px Microsoft Yahei';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText(content, 12, ctxHeight/2, ctxWidth);17 const textData = ctx.getImageData(0, 0, ctxWidth, ctxHeight);
this.imageUrl = this.mergeData(textData.data, 'R',ctxWidth,ctxHeight);19 }
把文字和圖片整合成一張圖
mergeData(newData, color, width, height) {
let img = this.img
this.ctx.drawImage(img, 0, 0, width, height);
this.originalData = this.ctx.getImageData(0, 0, width, height);
var oData = this.originalData.data;
var bit, offset;
switch (color) {
case 'R':
bit = 0;
offset = 3;
break;
case 'G':
bit = 1;
offset = 2;
break;
case 'B':
bit = 2;
offset = 1;
break;
}
for (var i = 0; i < oData.length; i++) {
if (i % 4 == bit) {
if (newData[i + offset] === 0 && (oData[i] % 2 === 1)) {
if (oData[i] === 255) {
oData[i]--
} else {
oData[i]++
}
} else if (newData[i + offset] !== 0 && (oData[i] % 2 === 0)) {
if (oData[i] === 255) {
oData[i]--
} else {
oData[i]++
}
}
}
}
this.ctx.putImageData(this.originalData, 0, 0);
return this.canvas.toDataURL();
},
調(diào)用下面方法,解開圖片信息
decryptImg(){
var data = this.originalData.data;
for(var i = 0; i < data.length; i++){
if(i % 4 == 0){
if(data[i] % 2 == 0){
data[i] = 0;
} else {
data[i] = 255;
}
} else if(i % 4 == 3){
continue;
} else {
data[i] = 0;
}
}
this.ctx.putImageData(this.originalData, 0, 0);
this.imageUrl = this.canvas.toDataURL();
},
二.圖片水印
watermark({
content = '',
container = '',
width = '',
height = '',
position = 'bottom-right',
font = '16px 微軟雅黑',
fillStyle = 'rgba(255, 255, 255, 1)',
zIndex = 11000,
} = {}) {
let img = this.img
const imgRatio = img.naturalWidth / img.naturalHeight;
const ctxWidth = width || img.naturalWidth;
const ctxHeight = height || ctxWidth / imgRatio;
this.canvas.width = ctxWidth;
this.canvas.height = ctxHeight;
const ctx = this.ctx;
ctx.drawImage(img, 0, 0, ctxWidth, ctxHeight);
ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.font = font;
ctx.fillStyle = fillStyle;
if(position == 'center') {
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(content, ctxWidth / 2, ctxHeight / 2, ctxWidth)
}else if(position == 'bottom-right') {
ctx.textAlign = 'right';
ctx.textBaseline = 'alphabetic';
ctx.fillText(content, ctxWidth-12, ctxHeight-12, ctxWidth)
}
const base64Url = this.canvas.toDataURL();
if(container) {
const div = document.createElement('div');
div.setAttribute('style', ` width: ${ctxWidth}px; height: ${ctxHeight}px; z-index: ${zIndex};
pointer-events: none; background-repeat: repeat; background-image: url('${base64Url}')`);
container.insertBefore(div, null);
}
this.imageUrl = base64Url
}
參考
http://www.alloyteam.com/2016/03/image-steganography/
到此這篇關(guān)于vue中使用帶隱藏文本信息的圖片、圖片水印的文章就介紹到這了,更多相關(guān)vue 隱藏文本信息圖片水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue實(shí)現(xiàn)頁(yè)面添加水印功能
- vue實(shí)現(xiàn)頁(yè)面添加水印效果
- Vue 集成 PDF.js 實(shí)現(xiàn) PDF 預(yù)覽和添加水印的步驟
- vue輕松實(shí)現(xiàn)水印效果
- vue實(shí)現(xiàn)上傳圖片添加水印
- vue實(shí)現(xiàn)上傳圖片添加水印(升級(jí)版)
- Vue之全局水印的實(shí)現(xiàn)示例
- vue項(xiàng)目實(shí)現(xiàn)對(duì)某個(gè)區(qū)域繪制水印
- Vue使用自定義指令實(shí)現(xiàn)頁(yè)面底部加水印
- vue實(shí)現(xiàn)頁(yè)面添加水印
相關(guān)文章
vue如何修改data中的obj數(shù)據(jù)的屬性
這篇文章主要介紹了vue如何修改data中的obj數(shù)據(jù)的屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue手機(jī)端input change時(shí),無(wú)法執(zhí)行的問題及解決
這篇文章主要介紹了vue手機(jī)端input change時(shí),無(wú)法執(zhí)行的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能
這篇文章主要介紹了使用Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
element日期選擇器el-date-picker樣式圖文詳解
最近寫的項(xiàng)目里面有一個(gè)功能是日期選擇功能,第一反應(yīng)是使用element里面的el-date-picker組件,下面這篇文章主要給大家介紹了關(guān)于element日期選擇器el-date-picker樣式的相關(guān)資料,需要的朋友可以參考下2022-09-09
VUE實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)
營(yíng)運(yùn)三寶(九宮格、大轉(zhuǎn)盤、老虎機(jī),當(dāng)然此三寶當(dāng)然是最基礎(chǔ)的營(yíng)銷運(yùn)營(yíng)手段),本片文章聊聊大轉(zhuǎn)盤,轉(zhuǎn)盤的實(shí)現(xiàn)邏輯應(yīng)該是營(yíng)銷方案較為簡(jiǎn)單的一種了,本文將介紹如何實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng),感興趣的朋友可以參考下2021-05-05

