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).