6

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?

Silfverstrom
  • 28,292
  • 6
  • 45
  • 57

3 Answers3

7

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.

Stroboskop
  • 4,327
  • 5
  • 35
  • 52
  • 1
    This solution is not working for me. image is always returned as null. So ImageIO.write throws exception. My byte array is a raw Y image data. Can you elaborate more? – Darshan Prajapati Jan 09 '14 at 06:12
  • Are you sure that your byteArray is a valid image? I think the only way to get `image == null` is if there is no ImageReader registered that can decode your stream. So my guess is that it's either a file type that is not supported or corrupt data. – Stroboskop Jan 09 '14 at 11:27
  • Its a YUV image data with ratio 4:0:0. – Darshan Prajapati Jan 16 '14 at 11:01
  • I'm not an expert on image formats, but i think BufferedImage only supports variations of RGB. You will probably have to convert it into RGB. Have a look at this question which includes YUV - RGB conversion code. http://stackoverflow.com/questions/9325861/converting-yuv-rgbimage-processing-yuv-during-onpreviewframe-in-android – Stroboskop Jan 16 '14 at 13:24
4

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);
}
Markus Koivisto
  • 609
  • 3
  • 6
1

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.

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90