Java的我怎么会加载一个黑白图像转换成二进制? [英] Java- How would I load a black and white image into binary?
问题描述
我使用Java在FSE模式展开。我想加载一个全黑与白的图像转换成二进制格式(二维数组preferably),并用它基于掩模的每个像素的碰撞检测。我甚至不知道从哪里这里开始,我已经研究了几个小时,也没有找到任何有关。
I am using Java with swing in FSE mode. I want to load a completely black-and-white image into binary format (a 2d array preferably) and use it for mask-based per-pixel collision detection. I don't even know where to start here, I've been researching for the past hour and haven't found anything relevant.
推荐答案
刚刚读入的 的BufferedImage
使用<一个href=\"http://download.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html#read%28java.io.File%29\"相对=nofollow> 的ImageIO#阅读()
和获取单个像素<一个href=\"http://download.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html#getRGB%28int,%20int%29\"相对=nofollow> 的BufferedImage#的getRGB()
。 0xFFFFFFFF的
的值是白色,残存的颜色。假设你要重新present白色作为字节 0
和颜色(黑色)作为字节 1
,这里的开球例如:
Just read it into a BufferedImage
using ImageIO#read()
and get the individual pixels by BufferedImage#getRGB()
. A value of 0xFFFFFFFF
is white and the remnant is color. Assuming that you want to represent white as byte 0
and color (black) as byte 1
, here's a kickoff example:
BufferedImage image = ImageIO.read(new File("/some.jpg"));
byte[][] pixels = new byte[image.getWidth()][];
for (int x = 0; x < image.getWidth(); x++) {
pixels[x] = new byte[image.getHeight()];
for (int y = 0; y < image.getHeight(); y++) {
pixels[x][y] = (byte) (image.getRGB(x, y) == 0xFFFFFFFF ? 0 : 1);
}
}
参见:
- Java教程 - 2D图形 - 与图像工作
- The Java Tutorials - 2D Graphics - Working with images
See also:
这篇关于Java的我怎么会加载一个黑白图像转换成二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!