这是我用Java编写的代码。它接收图像并一次更改一个像素,直到它到达图像的最后一个像素,然后将图像写出。我在第67行得到一个ArrayIndexOutOfBounds异常,我声明了一个新的Color对象:
Color pixel = new Color(image.getRGB(j,i));
我正在使用UNIX为类编写代码,如果该信息是相关的。知道发生了什么吗?
public class SecretDecoder
{
public static void main(String[] args)
{
try
{
BufferedImage image = ImageIO.read(new File(args[0]));
int height = image.getHeight();
int width = image.getWidth();
int[] rgbOriginal = new int[3];
if(args[1].equalsIgnoreCase("red"))
{
rgbOriginal[0] = 255;
rgbOriginal[1] = 0;
rgbOriginal[2] = 0;
}
else if(args[1].equalsIgnoreCase("green"))
{
rgbOriginal[0] = 0;
rgbOriginal[1] = 255;
rgbOriginal[2] = 0;
}
else if(args[1].equalsIgnoreCase("blue"))
{
rgbOriginal[0] = 0;
rgbOriginal[1] = 0;
rgbOriginal[2] = 255;
}
else if(args[1].equalsIgnoreCase("white"))
{
rgbOriginal[0] = 255;
rgbOriginal[1] = 255;
rgbOriginal[2] = 255;
}
else if(args[1].equalsIgnoreCase("gray"))
{
rgbOriginal[0] = 128;
rgbOriginal[1] = 128;
rgbOriginal[2] = 128;
}
else if(args[1].equalsIgnoreCase("cyan"))
{
rgbOriginal[0] = 0;
rgbOriginal[1] = 255;
rgbOriginal[2] = 255;
}
else
{
rgbOriginal[0] = 0;
rgbOriginal[1] = 255;
rgbOriginal[2] = 0;
}
System.out.println(width);
System.out.println(height);
for(int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Color pixel = new Color(image.getRGB(j,i));
if(pixel.getRed() == rgbOriginal[0] && pixel.getGreen() == rgbOriginal[1] && pixel.getBlue() == rgbOriginal[2])
{
if(args[2].equalsIgnoreCase("red"))
image.setRGB(j,i,Color.RED.getRGB());
else if(args[2].equalsIgnoreCase("green"))
image.setRGB(j,i,Color.GREEN.getRGB());
else if(args[2].equalsIgnoreCase("blue"))
image.setRGB(j,i,Color.BLUE.getRGB());
else if(args[2].equalsIgnoreCase("white"))
image.setRGB(j,i,Color.WHITE.getRGB());
else if(args[2].equalsIgnoreCase("gray"))
image.setRGB(j,i,Color.GRAY.getRGB());
else if(args[2].equalsIgnoreCase("cyan"))
image.setRGB(j,i,Color.CYAN.getRGB());
else
image.setRGB(j,i,Color.BLACK.getRGB());
}
}
}
File resultFile = new File("decoded_file.png");
ImageIO.write(image, "PNG", resultFile);
}
catch(Exception e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
也许图像不是从(0,0)开始。
我认为你必须像完成here一样调用image.getMinX()和image.getMinY()。
System.out.println(image.getMinX());
System.out.println(image.getMinY());
for(int i = image.getMinY(); i < height; i++) {
for (int j = image.getMinX(); j < width; j++) {