我还是Java新手。我从我的方法中检索RGB像素值时遇到了麻烦,compareHexaRGB在这种方法中,我可以根据2个相似图像的比较得到不同的像素值。我尝试了printString2DArray方法,但它不起作用。它声明了不兼容的类型:String [] []无法转换为String []或产生错误,如[[Ljava.lang.String; @ 6d09a8df
获取值后,我想将其合并为一个Hex值,然后将其转换为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;
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();
System.out.println("Char 1: ");
printString2DArray(char1);
}
public String convertHexToString()
{
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
extractMessage extract = new extractMessage();
extract.charExtract();
for(int i = 0; i < extract.length(); i+=2) //error stated couldn't find symbol extract
{
String output = extract.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
}
我会很感激的任何帮助!
编辑:我现在更改了代码。输出就是这样产生的。
Char 1:
null
null
null
null
null
null
null
null
6
6
null
null
6
null
null
null
null
null
不应该那样输出吗?我不想要null字符,但我只想要值。那我该怎么办?
答案 0 :(得分:0)
您的printString2DArray
方法应如下所示:
private static void printString2DArray(String[][] in)
{
for (int i = 0; i < in.length; i++) {
for (int j = 0; j < in[i].length; j++) {
if (i != 0 && j == 0) { // This is just formatting (you may want to change it)
System.out.print(", ");
}
System.out.print(in[i][j]);
}
}
}
要迭代数组String[][]
,您可以使用2个索引(i
和j
),因为它是一个二维数组。
修改强>
而不是:
char1 = new String[][]
{
new String[] { hexRGB.getCheck_hex2() } //error stated incompatible types
};
你可以尝试:
char1 = hexRGB.getCheck_hex2();