2017-06-17 66 views
0

我可以隱藏圖像中的文本。但是當我試圖從我的圖像中提取文本時,我總是隻能設法得到第一個字符。我不知道哪裏出了問題?隱寫術提取問題C#

我的嵌入操作碼:

public static Bitmap embedMessage(string hiddenText, Bitmap oriImage) 
     { 
      Color currentPixel; 
      int[] colorRGB = new int[3]; 
      List<int> messageValue = new List<int>(); 
      messageValue = ConvertMessage(messageValue, hiddenText); 
      int messageIndex = messageValue.Count; 
      int binaryCount = 0; 
      int zeroAdded = 0; 


      for(int row = 0; row < oriImage.Height; row++) 
      { 
       for(int col = 0; col < oriImage.Width; col++) 
       { 
        currentPixel = oriImage.GetPixel(col, row); 

        colorRGB[0] = ConvertEven(currentPixel.R); 
        colorRGB[1] = ConvertEven(currentPixel.G); 
        colorRGB[2] = ConvertEven(currentPixel.B); 

        for(int rgbIndex = 0; rgbIndex < colorRGB.Length; rgbIndex++) 
        { 
         if(messageIndex > 0) 
         { 
          colorRGB[rgbIndex] += messageValue[messageValue.Count - messageIndex] % 2; 
          messageValue[messageValue.Count - messageIndex] /= 2; 
         } 
        } 

        if (messageIndex == 0 && zeroAdded < 8) 
        { 
         oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2])); 
         zeroAdded++; 
         Console.WriteLine("Adding zero number: " + zeroAdded); 
        } 
        else 
        { 
         Console.WriteLine("Set Pixel"); 
         oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2])); 
        } 

        if (zeroAdded == 8) 
        { 
         Console.WriteLine("Final Pixel Add"); 
         oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2])); 
         return oriImage; 
        } 

        binaryCount++; 
        if (binaryCount % 8 == 0) { messageIndex--; Console.WriteLine("Message Index deducted"); } 

       } 
      } 
      return oriImage; 
     } 

我的嵌入實現是相同的這一example至於萃取我使用的確切提取碼從示例。無論我嘗試了什麼,我仍然只能獲得嵌入文本的第一個字符。我試着通過打印每個操作來檢查我的代碼,並且它們全都觸發,沒有任何問題,這意味着嵌入操作應該按預期工作。

+2

拿一個調試器,並在運行時檢查每個變量在任何時刻都有預期值。 – zerkms

+0

@zerkms感謝您的調試建議!剛試過打印R G B變量,我的版本肯定不像我期望的那樣工作。我的嵌入部分絕對有問題。 –

回答

0

剛剛發現我的問題。我的嵌入方法中的所有內容都應該在R G B for循環中。

+0

就目前來看,我可以看到你正在使用'.SetPixel'方法,這個方法已經很慢了:(https://stackoverflow.com/questions/7768711/setpixel-is-too-slow-is-there -a更快的路到平局對位圖);我實際上寫了一個類似的程序,然後將圖像中的壓縮文件存儲在GitHub圖像中(在它們被任意上載之前)。 https://github.com/ckpearson/Pack/blob/1.1/Pak/Packer.cs#L139 < - 這部分展示瞭如何以快速的方式將數據逐像素地寫入圖像。 – Clint