java - 线程 “main” java.lang.ArrayIndexOutOfBoundsException : Coordinate out of bounds 中的异常

标签 java bufferedimage javax.imageio

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)

Documentation

(您的 col 值运行到 width - 这是图像的 x 最大值,所以使用 col 用于 xrow 用于 y)

关于java - 线程 “main” java.lang.ArrayIndexOutOfBoundsException : Coordinate out of bounds 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27264363/

相关文章:

java - 计算机在二进制搜索中必须进行多少次计算?

java - SLF4J Logger 有时会清除控制台输出(不需要)

java - 如何告诉父进程等待子进程?

java - 为什么我在 Mule 中使用 JMS 队列后没有得到更改的有效负载?

java - 如何使用 TYPE_BYTE_GRAY 使用 AWT 高效创建灰度缓冲图像

java - 检查文件是否可以作为 BufferedImage 读取

Java BufferedImage JPG压缩而不写入文件

java - 旋转 BufferedImage 实例

java - 无法使用 ImageIO.read(文件文件)读取 JPEG 图像

java - 从 imageReader 写回修改后的图像