File newFile = null;
BufferedImage oldImage = null;
BufferedImage newImage = null;
String fileName = new String((args[0]));
String newFileName = new String(fileName.replaceFirst(".png", "-tiled.png"));
try{
oldImage = ImageIO.read(new File(fileName));
System.out.println("Reading Complete");
}
catch(IOException e) {
System.out.println("Error: "+ e);
}
int width = oldImage.getWidth();
int height = oldImage.getHeight();
newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int imagePixels [][] = new int [height][width];
for (int i = 0; i < height -1; ++i) {
for (int j = 0; j < width - 1; ++j) {
imagePixels[i][j] = oldImage.getRGB(i,j); //error here
}
}
它编译得很好,但是当我运行程序时,它可以完美地运行 10x10 图像,但是当我尝试使用 800x600 图像时,它不起作用,并且在注释行上出现错误,指出
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
at java.awt.image.BufferedImage.getRGB(Unknown Source)
at imagery.ImageTiler.main(ImageTiler.java:35)
有人能发现这个令人恼火的错误吗?
一些辅助信息:程序从命令行获取文件名,然后将 RGB 像素加载到 2D 数组中。
感谢任何帮助。干杯。
最佳答案
您在 getRGB
调用中得到的坐标是错误的。图像高度位于 Y 方向,但您使用高度作为 i
的边界 ... X 坐标。
This page解释Java图像坐标系。
关于java - 无法将图像加载到缓冲图像二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027203/