2014-01-10 52 views
-1

我已經成功地從compareHexaRGB方法中檢索RGB像素值。由於輸出包含空字符,輸出看起來並不令人信服。我怎樣才能消除它們?檢索到的值有一個空值。如何消除它們?

其次,當我試圖結合String [] [] char 1 + char 2時,我得到一個錯誤,指出它是「+」類型的錯誤操作數。但我想結合2字符串,以便我能夠將其轉換爲ASCII字符。

下面是compareHexaRGB方法的代碼:

public class compareHexaRGB 
{ 
private static int w; 
private static int h; 
private static BufferedImage img; 
private static BufferedImage img2; 
private static String[][] check_hex2; 
private static String[][] check_hex4; 
private static String[][] message; 

public static void compareHexaRGB(BufferedImage image, BufferedImage image2, int width, int height) throws IOException 
{ 
w = width; 
h = height; 
img = image; 
img2 = image2; 

} 

public void check() throws IOException 
{ 
    getPixelRGB1 pixel = new getPixelRGB1(); 
    getPixelData1 newPD = new getPixelData1(); 

    int[] rgb; 
    int count = 0; 

    int[][] pixelData = new int[w * h][3]; 
    check_hex2 = new String[w][h]; 
    check_hex4 = new String[w][h]; 

    for(int i = 0; i < w; i++) 
    { 
     for(int j = 0; j < h; j++) 
     { 
      rgb = newPD.getPixelData(img, i, j); 

      for(int k = 0; k < rgb.length; k++) 
      { 
       pixelData[count][k] = rgb[k]; 
      } 

       if(pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j])) 
       { 
        System.out.println("\nPixel values at position 2 are the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]); 
       } 
       if(pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])) 
       { 
        System.out.println("\nPixel values at position 4 are the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]); 
       } 
       if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j])) 
       { 
        System.out.println("\nPixel values at position 2 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]); 
        check_hex2[i][j] = pixel.display_img2Hex2()[i][j]; 
        System.out.println("\nOutput Hex 2: " + check_hex2[i][j]); 
       } 
       if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])) 
       { 
        System.out.println("\nPixel values at position 4 are not the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]); 
        check_hex4[i][j] = pixel.display_img2Hex4()[i][j]; 
        System.out.println("\nOutput Hex 4: " + check_hex4[i][j]); 
       } 
       if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))) 
       { 
        System.out.println("\nOne of the pixel values at position 2 and 4 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j] + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]); 

        if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))) 
        { 
         check_hex2[i][j] = pixel.display_img2Hex2()[i][j]; 
         System.out.println("\nOutput Hex 2: " + check_hex2[i][j]); 
        } 

        if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]) || (pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))) 
        { 
         check_hex4[i][j] = pixel.display_img2Hex4()[i][j]; 
         System.out.println("\nOutput Hex 4: " + check_hex4[i][j]); 
        } 

       } 
      count++; 
      System.out.println("\nOutput Count: " + count); 
     } 

    } 

} 

public String[][] getCheck_hex2() 
{ 
    return check_hex2; 
} 

public String[][] getCheck_hex4() 
{ 
    return check_hex4; 
} 
} 

代碼extractMessage方法:

public class extractMessage 
{ 
private static String character; 
private static String[][] char1; 
private static String[][] char2; 
private static int w; 
private static int h; 
private static String[][] in; 

public static void extractMessage(int width, int height, String[][] inn) 
{ 
    character = ""; 
    w = width; 
    h = height; 
    in = inn; 
} 

public static void printString2DArray(String[][] inn) 
{ 
    for (int i = 0; i < inn.length; i++) 
    { 
     for(int j = 0; j < inn[i].length; j++) 
     { 
      if (i != 0 && j == 0) 
      { 
       System.out.print(" "); 
      } 
      System.out.println(inn[i][j]); 
     } 
    } 
} 

public static void charExtract() 
{ 
    compareHexaRGB hexRGB = new compareHexaRGB(); 

    char1 = hexRGB.getCheck_hex2(); 
    char2 = hexRGB.getCheck_hex4(); 
    String[][] combine = char1 + char2; 

    System.out.println("Char 1: "); 
    printString2DArray(char1); 
    System.out.println("Char 2: "); 
    printString2DArray(char2); 

} 
} 

OUTPUT:

Char 1: 
null 
null 
null 
null 
null 
null 
null 
null 
6 
6 
null 
null 
6 
null 
null 
null 
null 
null 
Char 2: 
null 
null 
null 
null 
null 
null 
null 
null 
2 
3 
null 
null 
1 
null 
null 
null 
null 
null 

任何建議或糾正我將高度讚賞!

回答

0
  • 由於這些位置上的像素相同,因此會出現很多空值。在你的check()方法中,當像素相同時,你不會在該位置向數組添加任何東西 - 因此它仍然爲空。在-之類的地方添加一個擁有角色的地方。

  • 您的打印方法也是將每個元素打印在一行上,如果您將矩陣打印爲二維數組,它會更具可讀性。您可以通過修改你的方法來此

    for (int i = 0; i < inn.length; i++) 
    { 
        for(int j = 0; j < inn[i].length; j++) 
        { 
         System.out.print(inn[i][j] + " "); 
        } 
        System.out.println(); 
    } 
    

    另一種有益的方法做到這一點,你可能會感興趣的是Arrays.deepToString(Array),但是這創造了一個字符串,將需要操縱多條線路上打印會更容易做到以下幾點加上兩個數組在一起,沒有循環的內容 -

    for(String[] arr : inn) 
        System.out.println(Arrays.toString(arr)); 
    
  • 您想添加爲你根本無法做到這一點的數組時出現錯誤。您將需要一個類似環以上

    char1 = hexRGB.getCheck_hex2(); 
    char2 = hexRGB.getCheck_hex4(); 
    //assuming char1 and char2 are the same size and symmetrical 
    String[][] combine = new String[char1.length][char1[0].length] 
    
    for (int i = 0; i < char1.length; i++) 
    { 
        for(int j = 0; j < char1[i].length; j++) 
        { 
         // This will only concatenate the strings though, 
         // If you want the numerical addition you will have to 
         // convert them to ints first 
         combine[i][j] = char1[i][j] + char2[i][j]; 
        } 
    } 
    
在你第一次的建議
+0

,你說,我要補充的地方來保存字符。我可以做這樣的事情 - > if(pixel.display_imgHex2()[i] [j] .equals(pixel.display_img2Hex2()[i] [j])) { System.out.println(「\ nPixel 「+」\ n「+ pixel.display_imgHex2()[i] [j] +」「+ pixel.display_img2Hex2()[i] [j]); not_stega2 [i] [j] = pixel.display_img2Hex2()[i] [j]; }?還是我做錯了? – user3116040

相關問題