如何从 Java 中的原始字节 [] 创建 BMP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1193748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a BMP file from raw byte[] in Java
提问by Silfverstrom
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.
我有一个 C++ 应用程序,它与相机通信并获取原始图像数据。然后我在 C++ 中有一个 Byte[],我想用 JNI 将它发送到 Java。
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.
但是,我需要将原始 Byte[] 转换为真正的文件格式(.bmp 是我的第一选择)。如果我使用 BITMAPFILEINFO 和 BITMAPHEADERINFO 从 C++ 将它写入硬盘驱动器上的文件,我可以轻松地做到这一点,但我不知道如何将整个格式发送到 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.
然后我考虑使用 JNI 只发送原始字节 [] 数据,然后将其转换为 .bmp,但我似乎找不到任何好的库来在 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?
我最好的选择是什么?在 C++ 中转换图像,然后使用 JNI 发送它或将 RAW 数据发送到 Java,然后将其转换为 .bmp?我将如何最简单地实现这一目标?
回答by Stroboskop
It's just two lines in Java 1.5:
在 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.
据我所知,Java(在 Windows 上)知道如何导出 jpg、png 和 bmp。
回答by Markus Koivisto
There's no need to do any of that. Turn the byte array into an InputStream and feed that to ImageIO.read();
没有必要做任何这些。将字节数组转换为 InputStream 并将其提供给 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.
这将从您的字节数组创建一个 Image 对象,然后在 gui 组件中显示它确实非常简单。如果你想保存它,你也可以使用 ImageIO 类。
public void saveImage(Image img, String fileFormat, File f){
ImageIO.write(img, fileFormat, f);
}
回答by Marc Mutz - mmutz
If you know how to write as .bmpto 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 .bmpfile, I could suggest how to convert that into an in-memory operation.
如果您知道如何写入.bmp文件,那么您可以使用(几乎)相同的代码来写入内存缓冲区。您可以将该内存缓冲区传送到 Java,并让它解码像 Stroboskop 或 Markus Koivisto 提到的格式。如果您编辑了问题以包含将数据写入.bmp文件的方式,我可以建议如何将其转换为内存操作。

