2013-03-19 121 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