0

I'm working on a project in which I have to get image from camera (cmucam4) which is connected to my computer with Xbee. The probleme is that I can get the image data over the serial port, but when I save it as a file, the file can't be openned as image. I noticed that when I open the file with notepad++, the file does not have a header like other images (the camera send bmp image).

I tried to save the Image using ImageIO, but I dont know how to pass the data recived to the image!!

BufferedImage img = new BufferedImage(640, 480,BufferedImage.TYPE_INT_RGB);               
ImageIO.write(img, "BMP", new File("img/tmp.bmp"));
Harald K
  • 26,314
  • 7
  • 65
  • 111
NYoussef
  • 13
  • 1
  • 5

1 Answers1

1

If the camera truly sends BMP format, you could just write the data to disk. However, more likely (and this seems to be the case, reading the specs from your link), the cards sends a raw bitmap, which is not the same.

Using this info from the card spec PDF:

Raw image dumps over serial or to flash card

  • (640:320:160:80)x(480:240:120:60) image resolution
  • RGB565/YUV655 color space

The RGB565 pixel layout mentioned above should match perfectly with the BufferedImage.TYPE_USHORT_565_RGB, so that should be the easiest to use.

byte[] bytes = ... // read from serial port

ShortBuffer buffer = ByteBuffer.wrap(bytes)
        .order(ByteOrder.BIG_ENDIAN) // Or LITTLE_ENDIAN depending on the spec of the card
        .asShortBuffer();            // Our data will be 16 bit unsigned shorts

// Create an image matching the pixel layout from the card
BufferedImage img = new BufferedImage(640, 480, BufferedImage.TYPE_USHORT_565_RGB);

// Get the pixel data from the image, and copy the data from the card into it
// (the cast here is safe, as we know this will be the case for TYPE_USHORT_565_RGB)
short[] data = ((DataBufferUShort) img.getRaster().getDataBuffer()).getData();
buffer.get(data);

// Finally, write it out as a proper BMP file
ImageIO.write(img, "BMP", new File("temp.bmp"));

PS: The above code works for me, using a byte array of length 640 * 480 * 2, initialised with random data (as I obviously don't have such a card).

Community
  • 1
  • 1
Harald K
  • 26,314
  • 7
  • 65
  • 111