如果相機真的發送BMP格式,您可以將數據寫入磁盤。然而,更可能的(看起來是這樣,從你的鏈接讀取規格),卡片發送一個原始位圖,這是不一樣的。
使用從卡規範PDF此信息:
RAW圖像轉儲通過串行或閃存卡
- (640:320:160:80)×(480:240:120 :60)的圖像分辨率
- RGB565/YUV655顏色空間
的RGB565像素佈局mentione d以上應該與BufferedImage.TYPE_USHORT_565_RGB
完美匹配,所以應該是最容易使用的。
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:上面的代碼對我的作品,使用byte
陣列長度640 * 480 * 2的,用隨機數據初始化(如我顯然不具備這樣的卡)。
謝謝,它的工作原理。 – NYoussef