2
這是我的代碼,用Java編寫。它獲取圖像並逐個更改像素,直到達到圖像的最後一個像素,然後將圖像寫出。我上線67得到一個例外的ArrayIndexOutOfBounds,在那裏我宣佈一個新的Color對象:創建新對象時ArrayIndexOutOfBounds?
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();
}
}
}
看起來像... C#或Java ... – 2014-09-10 22:59:13
是的,對不起。在這個論壇的第一篇文章。這確實是Java。生病編輯在我的身體。 – Phalanges 2014-09-10 23:00:27
不是args [0]程序的名稱? – Chrisky 2014-09-10 23:02:22