如何在 Java 中从原始 byte[] 创建 BMP 文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有必要这样做。 将字节数组转换为 InputStream 并将其提供给 ImageIO.read();
这会从你的字节数组创建一个 Image 对象,然后在 gui 组件中显示它确实非常简单。 如果您想保存它,也可以使用 ImageIO 类。
There's no need to do any of that. Turn the byte array into an InputStream and feed that to ImageIO.read();
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.
Java 1.5 中只有两行:
据我所知,Java(在 Windows 上)知道如何导出 jpg、png 和 bmp。
It's just two lines in Java 1.5:
Java (on Windows) knows how to export jpg, png and bmp as far as i know.
如果您知道如何将
.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.