BufferedImage image = ImageIO.read(new File(img path));
int width = image.getWidth();
int height = image.getHeight();
int[][] result = new int[height][width];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
result[row][col] = image.getRGB(row, col);
}
}
这是我得到的异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:301)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
at PlantExtraction.main(PlantExtraction.java:46)
如何删除这些异常?
最佳答案
代码
image.getRGB(row, col);
应该是
image.getRGB(col, row);
正如文档所说:
getRGB(int x, int y)
。
(您的 col
值运行到 width
- 这是图像的 x
最大值,所以使用 col
用于 x
和 row
用于 y
)
关于java - 线程 “main” java.lang.ArrayIndexOutOfBoundsException : Coordinate out of bounds 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27264363/