2013-03-19 116 views
0

因此,我正在制作一个简单的隐写工具(加密图像中的邮件),并通过Node.js将其公开为Web服务。我对Javascript和Node.js非常陌生。应用程序首先通过将每个字符更改为8位ASCII编码将文本字符串转换为二进制字符串,从而生成一个较大的二进制字符串。然后我在像素内加密消息。偶数的像素值表示二进制的0,奇数值表示1。字符串的结尾被标记为连续值为100的3个像素(这是暂时的,直到我找出一个更好的方式来标记结尾)。我使用了一个叫做'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; 
+3

欢迎来到** async **的美妙世界!你不能那样做。 – SLaks 2013-03-19 03:09:54

回答

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函数也是如此。该功能将在编码完成之前返回。如果您想知道何时完成,您需要以相同的方式传递回调。

+0

谢谢!我现在理解异步函数背后的概念。我仍然在努力让函数返回'message'字符串。这个.js模块中的函数被导出到另一个模块并在那里被调用。我需要将'message'字符串返回到外部变量。我怎样才能做到这一点? – 2013-03-20 03:16:48

+0

@DinislamTebuev没问题!问题是因为它是异步的,你不能返回任何有用的东西。相反,您需要传递回调,并使任何调用decodeMessage的代码也是异步的。异步是病毒性的,如果你想调用任何异步的东西然后正确地使用它,你需要使你自己的所有代码异步。如果你正在使用模块功能,我可以扩展更多的例子。 – loganfsmyth 2013-03-20 03:21:01

+0

那太棒了!我将我的代码分解成模块以遵循良好的编程风格。我的server.js文件将调用这些函数,它需要一次处理多个请求,所以我明白一切都需要异步 – 2013-03-20 03:30:34