如何在 Java 中从原始 byte[] 创建 BMP 文件

发布于 2024-07-30 06:54:48 字数 409 浏览 10 评论 0原文

我有一个 C++ 应用程序,它与相机通信并获取原始图像数据。 然后我有一个 C++ 中的 Byte[],我想用 JNI 将其发送到 Java。

但是,我需要将原始 Byte[] 转换为真实的文件格式(.bmp 是我的第一选择)。 如果我使用 BITMAPFILEINFO 和 BITMAPHEADERINFO 将它从 C++ 写入硬盘上的文件,我可以轻松地做到这一点,但我不知道如何将整个格式发送到 Java。

然后我考虑使用 JNI 只发送原始 byte[] 数据,然后将其转换为 .bmp,但我似乎找不到任何好的库可以在 Java 中执行此操作。

我最好的选择是什么? 在 C++ 中转换图像,然后使用 JNI 发送它,或者将 RAW 数据发送到 Java,然后将其转换为 .bmp? 我如何最容易实现这一目标?

I have a C++ application which communicates with a camera and fetches raw image-data.
I then have a Byte[] in C++, which i want to send to Java with JNI.

However, i need to convert the raw Byte[] to an real file format(.bmp was my first choice).
I can easily do this if i write it from C++ to an file on the hard-drive, using BITMAPFILEINFO and BITMAPHEADERINFO, but i do not know how one would go about sending the entire-format to Java.

Then i thought about sending only the raw byte[] data using JNI and then converting it to
.bmp, but i can't seem to find any good library for doing this in Java.

What would be my best choice? Converting the image in C++ and then sending it using JNI or send the RAW data to Java and then convert it to .bmp? How would i easiest achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

奶茶白久 2024-08-06 06:54:48

没有必要这样做。 将字节数组转换为 InputStream 并将其提供给 ImageIO.read();

public Image getImageFromByteArray(byte[] byteArray){
    InputStream is = new ByteArrayInputStream(byteArray);
    return ImageIO.read(is);
} 

这会从你的字节数组创建一个 Image 对象,然后在 gui 组件中显示它确实非常简单。 如果您想保存它,也可以使用 ImageIO 类。

public void saveImage(Image img, String fileFormat, File f){
    ImageIO.write(img, fileFormat, f);
}

There's no need to do any of that. Turn the byte array into an InputStream and feed that to ImageIO.read();

public Image getImageFromByteArray(byte[] byteArray){
    InputStream is = new ByteArrayInputStream(byteArray);
    return ImageIO.read(is);
} 

This creates an Image object from your byte array, which is then very trivial indeed to display inside a gui component. Should you want to save it, you can use the ImageIO class for that as well.

public void saveImage(Image img, String fileFormat, File f){
    ImageIO.write(img, fileFormat, f);
}
人疚 2024-08-06 06:54:48

Java 1.5 中只有两行:

BufferedImage image = ImageIO.read( new ByteArrayInputStream( byteArray ) );
ImageIO.write(image, "BMP", new File("filename.bmp"));

据我所知,Java(在 Windows 上)知道如何导出 jpg、png 和 bmp。

It's just two lines in Java 1.5:

BufferedImage image = ImageIO.read( new ByteArrayInputStream( byteArray ) );
ImageIO.write(image, "BMP", new File("filename.bmp"));

Java (on Windows) knows how to export jpg, png and bmp as far as i know.

指尖凝香 2024-08-06 06:54:48

如果您知道如何将 .bmp 写入文件,那么您可以使用(几乎)相同的代码写入内存缓冲区。 您可以将该内存缓冲区传送到 Java,并让它解码 Stroboskop 或 Markus Koivisto 提到的格式。 如果您编辑问题以包括将数据写入 .bmp 文件的方式,我可以建议如何将其转换为内存中操作。

If you know how to write as .bmp to a file, then you can use (almost) the same code for writing into a memory buffer instead. That memory buffer you can ship over to Java, and have it decode the format like Stroboskop or Markus Koivisto mentioned. If you edited your question to include the way you write the data to a .bmp file, I could suggest how to convert that into an in-memory operation.

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