java - 某些 RGB 值似乎无法转换为颜色对象

标签 java image graphics colors rgb

我有这种方法:

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 中。

现在我的问题:

虽然有些图片像这样:

enter image description here

像魅力一样工作,其他人像这样:

enter image description here

让我以错误结束:

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 值转换为颜色?

你能告诉我如何让它发挥作用吗?

最佳答案

错误消息实际上是这样说的:“索引越界”。看来,你混淆了你的坐标和它们的界限。 getRGB 首先接受参数 x(范围 0 .. width),然后 y(范围 0 .. 高度) 作为第二个。

this.width = image.getWidth();
this.height = image.getHeight();

for (int row = 0; row < height; row++) {        // swapped the ...
    for (int col = 0; col < width; col++) {     // ... bounds
        this.PointInformation[row][col] = new Color(image.getRGB(col, row));
    }
}

你的第一个例子有 width = height,所以问题没有显示出来。

关于java - 某些 RGB 值似乎无法转换为颜色对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13592148/

相关文章:

Java - 使用多正则表达式从字符串中提取 JSON 值

java - Spring Bean 根据条件创建一个接口(interface)

haskell - 绝望 : How to install Haskell School of Expression graphics pkg, Windows XP 和 7

graphics - 有没有办法从 Applesoft Basic 访问 Apple 2 上的 DHR

java - 如何指定流程持续时间 - flowable

java - Java String.split 是否有可能返回 null String[]

ios - 导航栏上的类别覆盖不起作用

python - TensorFlow Serving 将数据发送为 b64 而不是 Numpy Array

javascript - 为什么按钮必须点击两次才能起作用?

c++ - opengl中渲染对象的移动