RGB数据保存为BMP图片

一、BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。

1、BMP文件头(14字节)

typedef struct                       /**** BMP file header structure ****/
{
    unsigned int   bfSize;           /* Size of file */
    unsigned short bfReserved1;      /* Reserved */
    unsigned short bfReserved2;      /* ... */
    unsigned int   bfOffBits;        /* Offset to bitmap data */
} MyBITMAPFILEHEADER;

2、位图信息头(40字节)

typedef struct                       /**** BMP file info structure ****/
{
    unsigned int   biSize;           /* Size of info header */
    int            biWidth;          /* Width of image */
    int            biHeight;         /* Height of image */
    unsigned short biPlanes;         /* Number of color planes */
   
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个将RGB565数据保存BMP文件的Java方法: ```java public static void saveRGB565ToBMP(int width, int height, byte[] data, File file) throws IOException { int rowLength = width * 2; byte[] rowPadding = new byte[(4 - rowLength % 4) % 4]; int pixelArraySize = rowLength * height; int fileSize = pixelArraySize + 54; byte[] bmpData = new byte[fileSize]; // BMP Header bmpData[0] = 'B'; // Magic number bmpData[1] = 'M'; // Magic number ByteBuffer.wrap(bmpData, 2, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(fileSize); // File size ByteBuffer.wrap(bmpData, 10, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(54); // Offset to pixel array ByteBuffer.wrap(bmpData, 14, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(40); // DIB Header size ByteBuffer.wrap(bmpData, 18, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(width); // Image width ByteBuffer.wrap(bmpData, 22, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(height); // Image height ByteBuffer.wrap(bmpData, 26, 2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) 1); // Color planes ByteBuffer.wrap(bmpData, 28, 2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) 16); // Bits per pixel ByteBuffer.wrap(bmpData, 34, 4).order(ByteOrder.LITTLE_ENDIAN).putInt(pixelArraySize); // Pixel array size // Pixel array int bmpOffset = 54; int dataOffset = 0; for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < width; x++) { byte b1 = data[dataOffset++]; byte b2 = data[dataOffset++]; int r = ((b2 & 0xF8) >> 3) << 16; int g = (((b2 & 0x07) << 3) | ((b1 & 0xE0) >> 5)) << 8; int b = (b1 & 0x1F) << 3; int rgb = r | g | b; ByteBuffer.wrap(bmpData, bmpOffset, 3).order(ByteOrder.LITTLE_ENDIAN).putInt(rgb); bmpOffset += 3; } bmpOffset += rowPadding.length; } // Write BMP data to file try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(bmpData); } } ``` 该方法使用Java的ByteBuffer类来处理字节序和字节对齐,实现了将RGB565数据保存BMP文件的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lmr廖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值