首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数返回值失败

函数返回值失败
EN

Stack Overflow用户
提问于 2013-03-19 11:08:56
回答 1查看 151关注 0票数 0

因此,我正在制作一个简单的隐写术工具(加密图像中的消息),并通过Node.js将其公开为web服务。我对Javascript和Node.js是非常陌生的。该应用程序首先将文本字符串转换为二进制字符串,方法是将每个字符转换为8位ASCII编码,从而生成一个大的二进制字符串。然后我在像素内对消息进行加密。像素的偶数值表示二进制中的0,奇数值表示1。字符串的末尾被标记为一行中值为100的3个像素(这是暂时的,直到我找到更好的方法来标记末尾)。我使用的是一个名为'pngjs‘的node.js库,它让我可以像素级访问png图像。

所以我有一个关于decodeMessage函数的问题。它构建字符串message,然后返回它,但是最后的返回调用导致未定义。

我怎么才能修复它?

提前感谢您的帮助!

代码语言:javascript
运行
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 11:10:52

parsed事件是异步激发的,因此不能从decodeMessage返回值。

代码语言:javascript
运行
复制
function decodeMessage(image, cb) {

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

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

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

代码语言:javascript
运行
复制
decodeMessage(image, function(decoded){
  // Here is the decoded data.
});

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15490836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档