我有这种方法:
public void SaveImageOntoObject(String filepath) throws IOException {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(filepath));
this.width = image.getWidth();
this.height = image.getHeight();
this.ResetPointInformation();
for (int row = 0; row < width; row++) {
for (int col = 0; col < height; col++) {
this.PointInformation[row][col] = new Color(image.getRGB(col, row));
}
}
}
它将图像的文件路径作为输入,将每个像素的 RPG 值转换为颜色对象,然后将其存储到调用方法的对象的二维数组 PointInformation 中。
现在我的问题:
虽然有些图片像这样:
像魅力一样工作,其他人像这样:
让我以错误结束:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at Drawing.Object2D.SaveImageOntoObject(Object2D.java:75)** (that's the class whose object's my method works on)
为什么会这样?似乎 Java 无法将某些 RGB 值转换为颜色?
你能告诉我如何让它工作吗?