所以,我试着用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));
}
}
}
}
它由主类构造函数调用:
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像素= image.getRGB (xx,yy)“,第41行是构造函数中调用它的地方,第206行是主要方法。
提前谢谢!^_^
发布于 2016-12-25 14:27:20
问题在于:
int pixel = image.getRGB (xx, yy);
它应该是:
int pixel = image.getRGB (yy, xx);
发布于 2015-01-25 20:31:51
您的xx
从0到高度,而不是从0到宽度。您的yy
从0到宽度,而不是从0到高度。
发布于 2015-11-16 18:42:34
level = imageLoader.loadImage ("/levels/level_test.png");
您正在使用的图像应该小于主窗口的总宽度和高度。在这种情况下,RGB值被拍摄到大小为2^X的照片,其中X=1,2,3,4,5,6,7,8,9.。。
试一试:将level_test.png的大小调整为512个像素。
上面是这方面的解决方案,因为Array包含边界。
java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
https://stackoverflow.com/questions/28141099
复制相似问题