gpt4 book ai didi

Java BufferedImage.getRGB()——坐标越界

转载 作者:行者123 更新时间:2023-12-01 18:18:52 30 4
gpt4 key购买 nike

所以,我正在尝试 BufferedImage 中特定像素的颜色...

public void LoadImageLevel (BufferedImage image) {

int w = image.getWidth ();
int h = image.getHeight ();

System.out.println (w + " " + h);

for (int xx = 0; xx < h; xx++) {

for (int yy = 0; yy < w; yy++) {

int pixel = image.getRGB (xx, yy);

int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;

if (red == 255 && green == 255 && blue == 255) {

handler.addObject (new Block (xx * 32, yy * 32, ObjectID.Block, 32, 32));
}
}
}
}

它是从 Main 类构造函数调用的:

    ImageLoader imageLoader = new ImageLoader ();

level = imageLoader.loadImage ("/levels/level_test.png");

LoadImageLevel (level);

BufferedImage 是从我的 BufferedImageLoader 类加载的:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader {

private BufferedImage image;

public BufferedImage loadImage (String path) {

try {

image = ImageIO.read (getClass ().getResource (path));

} catch (IOException e) {

e.printStackTrace ();
}

return image;
}
}

当我运行该项目时,出现此错误:

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 com.main.index.Game.LoadImageLevel(Game.java:190)
at com.main.index.Game.<init>(Game.java:41)
at com.main.index.Game.main(Game.java:206)

第190行是“int Pixel = image.getRGB(xx, yy);”,第41行是构造函数中调用它的地方,第206行是main方法。

<小时/>

提前致谢! ^_^

最佳答案

问题出在这里:

int pixel = image.getRGB (xx, yy);

应该是:

int pixel = image.getRGB (yy, xx);

关于Java BufferedImage.getRGB()——坐标越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28141099/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com