如何从Java中的原始byte []创建一个BMP文件 [英] How to create a BMP file from raw byte[] in Java

查看:443
本文介绍了如何从Java中的原始byte []创建一个BMP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



然而,我需要转换原始Byte [到一个真正的文件格式(.bmp是我的第一选择)。
我可以很容易地做到这一点,如果我写它从C ++到硬盘驱动器上的文件,使用BITMAPFILEINFO和BITMAPHEADERINFO,但我不知道如何将整个格式发送到Java。



然后我想到使用JNI只发送原始的byte []数据,然后将其转换为
.bmp,但我似乎找不到任何好的库在Java中这样做。



我最好的选择是什么?在C ++中转换图像,然后使用JNI发送它或将RAW数据发送到Java,然后将其转换为.bmp?

解决方案

这只是Java 1.5中的两行:

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

Java(在Windows上)知道如何导出jpg,png和bmp, / p>

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?

解决方案

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.

这篇关于如何从Java中的原始byte []创建一个BMP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆