Javascript 在图像中对隐藏信息进行编码
我想用js在图像中编码一条隐藏的信息,但不知道这是怎么回事。我一直在寻找一些算法,但没有找到。有人能解释一下如何使用js对图像中的消息进行编码吗 使用此库 它将允许您将文本隐藏到图像中 编辑-从链接添加编码代码Javascript 在图像中对隐藏信息进行编码,javascript,Javascript,我想用js在图像中编码一条隐藏的信息,但不知道这是怎么回事。我一直在寻找一些算法,但没有找到。有人能解释一下如何使用js对图像中的消息进行编码吗 使用此库 它将允许您将文本隐藏到图像中 编辑-从链接添加编码代码 Cover.prototype.encode = function(message, image, options) { // Handle image url if(image.length) { image = util.loadImg(image); } els
Cover.prototype.encode = function(message, image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}
options = options || {};
var config = this.config;
var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2,t)),
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');
var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');
shadowCanvas.style.display = 'none';
shadowCanvas.width = options.width || image.width;
shadowCanvas.height = options.height || image.height;
if(options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height );
} else {
shadowCtx.drawImage(image, 0, 0);
}
var imageData = shadowCtx.getImageData(0, 0, shadowCanvas.width, shadowCanvas.height),
data = imageData.data;
// bundlesPerChar ... Count of full t-bit-sized bundles per Character
// overlapping ... Count of bits of the currently handled character which are not handled during each run
// dec ... UTF-16 Unicode of the i-th character of the message
// curOverlapping ... The count of the bits of the previous character not handled in the previous run
// mask ... The raw initial bitmask, will be changed every run and if bits are overlapping
var bundlesPerChar = codeUnitSize/t >> 0,
overlapping = codeUnitSize%t,
modMessage = [],
decM, oldDec, oldMask, left, right,
dec, curOverlapping, mask;
var i, j;
for(i=0; i<=message.length; i+=1) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
modMessage.push(left+right);
if(i<message.length) {
mask = Math.pow(2,2*t-curOverlapping) * (1 - Math.pow(2, -t));
for(j=1; j<bundlesPerChar; j+=1) {
decM = dec & mask;
modMessage.push(decM >> (((j-1)*t)+(t-curOverlapping)));
mask <<= t;
}
if((overlapping*(i+1))%t === 0) {
mask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2,-t));
decM = dec & mask;
modMessage.push(decM >> (codeUnitSize-t));
}
else if(((((overlapping*(i+1))%t) + (t-curOverlapping)) <= t)) {
decM = dec & mask;
modMessage.push(decM >> (((bundlesPerChar-1)*t)+(t-curOverlapping)));
}
}
}
else if(i<message.length) {
mask = Math.pow(2,t) - 1;
for(j=0; j<bundlesPerChar; j+=1) {
decM = dec & mask;
modMessage.push(decM >> (j*t));
mask <<= t;
}
}
oldDec = dec;
}
// Write Data
var offset, index, subOffset, delimiter = messageDelimiter(modMessage,threshold),
q, qS;
for(offset = 0; (offset+threshold)*4 <= data.length && (offset+threshold) <= modMessage.length; offset += threshold) {
qS=[];
for(i=0; i<threshold && i+offset < modMessage.length; i+=1) {
q = 0;
for(j=offset; j<threshold+offset && j<modMessage.length; j+=1)
q+=modMessage[j]*Math.pow(args(i),j-offset);
qS[i] = (255-prime+1)+(q%prime);
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];
subOffset = qS.length;
}
// Write message-delimiter
for(index = (offset+subOffset); index-(offset+subOffset)<delimiter.length && (offset+delimiter.length)*4<data.length; index+=1)
data[(index*4)+3]=delimiter[index-(offset+subOffset)];
// Clear remaining data
for(i=((index+1)*4)+3; i<data.length; i+=4) data[i] = 255;
imageData.data = data;
shadowCtx.putImageData(imageData, 0, 0);
return shadowCanvas.toDataURL();
};
Cover.prototype.encode=函数(消息、图像、选项){
//句柄图像url
if(image.length){
image=util.loadImg(image);
}else if(image.src){
image=util.loadImg(image.src);
}else if(!(HTMLImageElement的图像实例)){
抛出新错误('非法输入:输入图像既不是URL字符串也不是图像');
}
选项=选项| |{};
var config=this.config;
var t=options.t | | config.t,
threshold=options.threshold | | config.threshold,
codeUnitSize=options.codeUnitSize | | config.codeUnitSize,
prime=util.findNextPrime(Math.pow(2,t)),
args=options.args | | config.args,
messageDelimiter=options.messageDelimiter | | config.messageDelimiter;
如果(!t | | t<1 | | t>7)抛出新错误(“非法选项:参数t=“+t+”无效:0>0,
重叠=代码单位大小%t,
modMessage=[],
decM,oldDec,oldMask,左,右,
dec,curOverlapping,mask;
varⅠ,j;
对于(i=0;i 0&&oldDec){
//新字符的掩码,随着重叠位的计数而移位
掩码=数学功率(2,t-cur)-1;
//旧字符的掩码,即右侧的t-位重叠
//那种性格
oldMask=Math.pow(2,codeUnitSize)*(1-Math.pow(2,-curOverlapping));
左=(dec和mask)>(codeUnitSize-curOverlapping);
modMessage.push(左+右);
如果(i)((j-1)*t)+(t-t));
掩模(codeUnitSize-t);
}
如果((((重叠*(i+1))%t)+(t-curOverlapping))>((bundlesPerChar-1)*t)+(t-curOverlapping));
}
}
}
如果(i(j*t));
蒙面谢谢,但是有没有其他方法不用这个库,而是自己写代码?当然,但这不是我的建议。请检查,你复制粘贴代码或只是用它来激发灵感链接只有答案是不鼓励的,因为他们不教任何东西,更不用说链接可能会在以后的某个时候变得死气沉沉e post无用。如果你想建议这样的库,那么在答案中也包括库的演示/解释。你想做的是所谓的隐写术,这不是一个简单的任务,解释如何做对于SO的Q&a格式来说太长了。在大多数情况下,SO在这里帮助修复你已经没有的代码虽然你需要学习如何在画布上绘制/操作图像,然后学习隐写术方法,然后尝试以某种js方式应用这些方法。你可能需要使用一些第三方库来帮助完成这些任务