Java-如何将黑白图像加载到二进制中?
我在 FSE 模式下使用 Java 和 swing。我想将完全黑白图像加载为二进制格式(最好是二维数组),并将其用于基于掩码的每像素碰撞检测。我什至不知道从哪里开始,过去一个小时我一直在研究,但没有找到任何相关的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将其读入
BufferedImage
使用
ImageIO#read()
并通过BufferedImage#getRGB()
。0xFFFFFFFF
的值是白色,余数是颜色。假设您想要将白色表示为字节0
,将颜色(黑色)表示为字节1
,下面是一个启动示例:另请参阅:
Just read it into a
BufferedImage
usingImageIO#read()
and get the individual pixels byBufferedImage#getRGB()
. A value of0xFFFFFFFF
is white and the remnant is color. Assuming that you want to represent white as byte0
and color (black) as byte1
, here's a kickoff example:See also:
如果您从 URL 读取图像,则它已经是二进制格式了。只需下载数据并忽略它是图像的事实。毕竟,下载涉及的代码并不关心。假设你想将其写入文件或类似的东西,只需打开
URLConnection
并打开FileOutputStream
,并重复从网络输入流中读取,写入数据您已读取输出流。如果您不从某些资源下载 ImageIO,也可以使用它。
If you're reading the image from a URL, it will already be in a binary format. Just download the data and ignore the fact that it's an image. The code which is involved in download it won't care, after all. Assuming you want to write it to a file or something similar, just open the
URLConnection
and open theFileOutputStream
, and repeatedly read from the input stream from the web, writing the data you've read to the output stream.You can also use
ImageIO
if you are not downloading it from some resource.