Java-如何将黑白图像加载到二进制中?

发布于 2024-11-05 21:03:19 字数 116 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-11-12 21:03:19

只需将其读入 BufferedImage使用ImageIO#read() 并通过 BufferedImage#getRGB()0xFFFFFFFF 的值是白色,余数是颜色。假设您想要将白色表示为字节 0,将颜色(黑色)表示为字节 1,下面是一个启动示例:

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);
    }
}

另请参阅:

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);
    }
}

See also:

原谅我要高飞 2024-11-12 21:03:19

如果您从 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 the FileOutputStream, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文