我已成功从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);
}
}
输出:
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 :(得分:0)
由于这些位置的像素相同,因此获得大量空值。在check()
方法中,当像素相同时,您不会在该位置向数组添加任何内容 - 因此它保持为空。在这些点添加一个持有角色的地方,例如-
或其他。
您的打印方法也会在一行上打印每个元素,如果您像打印矩阵一样打印2D数组,它将更具可读性。您可以通过将方法修改为此
来完成此操作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];
}
}