Итак, я делаю простой инструмент стеганографии (шифрование сообщений внутри изображений) и выставляю его как веб-службу через Node.js. Я очень новичок в Javascript и Node.js, в частности. Приложение сначала преобразует текстовую строку в двоичную строку, меняя каждый символ на 8-разрядную кодировку ASCII, в результате чего получается одна большая двоичная строка. Затем я шифрую сообщение в пикселях. Даже значения пикселей представляют 0s из двоичного, а нечетные значения представляют 1s. Конец строки помечен как 3 пикселя значения 100 в строке (это временно, пока я не выясню лучший способ отметить конец). Я использую библиотеку node.js под названием «pngjs», которая дает мне доступ к изображениям png на уровне пикселей.Функция, не возвращающая значение
У меня проблема с функцией decodeMessage. Он создает строковое сообщение, а затем предназначен для его возврата, однако обратный вызов в конце приводит к неопределенному.
Как это исправить?
Заранее благодарим за помощь!
function encodeMessage(image, mes) {
var message = mes;
var fs = require('fs'),
PNG = require('pngjs').PNG;
fs.createReadStream(image)
.pipe(new PNG({
filterType: 4
}))
.on('parsed', function() {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x);// << 2;
//console.log(idx);
if (idx < message.length) {
var item = message.charAt(idx);
/* if the character in the encoded string is 0 */
if (item == 0) {
/* if the pixel is odd, we want it to be even */
if (this.data[idx] % 2 == 1) {
/* if the pixel is 0, add 1 to it */
if (this.data[idx] == 0) {
this.data[idx] = this.data[idx] + 1;
} else {
/* else substract 1 */
this.data[idx] = this.data[idx] - 1;
}
}
} else {
/* if the character in the encoded string is 1 */
if (this.data[idx] % 2 == 0) {
if (this.data[idx] == 0) {
this.data[idx] = this.data[idx] + 1;
} else {
this.data[idx] = this.data[idx] - 1;
}
}
}
//console.log(this.data[idx]);
} else if (idx === message.length) {
/* do something to the first pixel following the end of the string */
this.data[idx] = 100;
this.data[idx+1] = 100;
this.data[idx+2] = 100;
//console.log(this.data[idx]);
} else {
/* do something to the remaining pixels */
}
}
}
this.pack().pipe(fs.createWriteStream('encoded_' + image));
});
}
function decodeMessage(image) {
var message = "";
var fs = require('fs'),
PNG = require('pngjs').PNG;
fs.createReadStream(image)
.pipe(new PNG({
filterType: 4
}))
.on('parsed', function() {
dance:
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x);// << 2;
if (this.data[idx] == 100 && this.data[idx+1] == 100 && this.data[idx+2] == 100) {
break dance;
} else {
if (this.data[idx] % 2 == 0) {
message += "0";
} else {
message += "1";
}
}
}
}
/* the message outputs correctly over here */
console.log(message);
//return message;
});
/* but the return of the variable here doesn't work */
return message;
}
exports.encodeMessage = encodeMessage;
exports.decodeMessage = decodeMessage;
Добро пожаловать в чудесный мир ** async **! Вы не можете этого сделать. – SLaks