【问题标题】:Function failing to return a value函数无法返回值
【发布时间】:2013-03-19 03:08:55
【问题描述】:

所以我正在制作一个简单的隐写工具(加密图像中的消息)并通过 Node.js 将其作为 Web 服务公开。我对 Javascript 和 Node.js 非常陌生,尤其是。该应用程序首先将文本字符串转换为二进制字符串,方法是将每个字符更改为 8 位 ASCII 编码,从而生成一个大的二进制字符串。然后我在像素内加密消息。像素的偶数值代表二进制中的 0,奇数值代表 1。字符串的结尾连续标记为 3 个像素值 100(这是暂时的,直到我找到更好的方法来标记结尾)。我正在使用一个名为“pngjs”的 node.js 库,它可以让我对 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 的精彩世界!你不能那样做。

标签: node.js function return steganography


【解决方案1】:

parsed 事件是异步触发的,因此您无法从decodeMessage 返回值。

function decodeMessage(image, cb) {

  // Code
  .on('parsed', function() {
    // Code

    console.log(message);
    cb(message);
  });
}

然后您必须将回调传递给您的decodeMessage 函数。

decodeMessage(image, function(decoded){
  // Here is the decoded data.
});

encodeMessage 函数也是如此。该函数将在编码完成之前返回。如果你想知道什么时候完成,你需要以同样的方式传递一个回调。

【讨论】:

  • 谢谢!我现在了解了异步函数背后的概念。我仍在努力让函数返回“消息”字符串。这个 .js 模块中的函数被导出到另一个模块并在那里调用。我需要将“消息”字符串返回给外部变量。我怎样才能做到这一点?
  • @DinislamTebuev 没有问题!问题是因为它是异步的,所以你不能返回任何有用的东西。相反,您需要传入一个回调,并使任何调用 decodeMessage 的代码也异步。异步是病毒式的,如果你想调用任何异步的东西,那么要正确使用它,你也需要使你自己的所有代码异步。如果您正在使用模块功能,我可以扩展更多示例。
  • 那太好了!我将代码拆分为模块以遵循良好的编程风格。我的 server.js 文件将调用这些函数,它需要一次处理多个请求,所以据我所知,一切都需要异步
  • 基本上你想调用函数并传递回调,就像我的第二个 sn-p 一样。
猜你喜欢
  • 2013-04-22
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2018-06-30
  • 2011-01-30
  • 1970-01-01
相关资源
最近更新 更多