linux 存bmp图片大小,将rgb图像数据保存为BMP格式图片的方法

extern "C"

{

#include

#include

#include

}

typedef long LONG;

typedef unsigned long DWORD;

typedef unsigned short WORD;

typedef struct {

WORD bfType;

DWORD bfSize;

WORD bfReserved1;

WORD bfReserved2;

DWORD bfOffBits;

} BMPFILEHEADER_T;

typedef struct{

DWORD biSize;

LONG biWidth;

LONG biHeight;

WORD biPlanes;

WORD biBitCount;

DWORD biCompression;

DWORD biSizeImage;

LONG biXPelsPerMeter;

LONG biYPelsPerMeter;

DWORD biClrUsed;

DWORD biClrImportant;

} BMPINFOHEADER_T;

void savebmp(uchar * pdata, char * bmp_file, int width, int height )

{ //分别为rgb数据,要保存的bmp文件名,图片长宽

int size = width*height*3*sizeof(char); // 每个像素点3个字节

// 位图第一部分,文件信息

BMPFILEHEADER_T bfh;

bfh.bfType = (WORD)0x4d42; //bm

bfh.bfSize = size // data size

+ sizeof( BMPFILEHEADER_T ) // first section size

+ sizeof( BMPINFOHEADER_T ) // second section size

;

bfh.bfReserved1 = 0; // reserved

bfh.bfReserved2 = 0; // reserved

bfh.bfOffBits = sizeof( BMPFILEHEADER_T )+ sizeof( BMPINFOHEADER_T );//真正的数据的位置

// 位图第二部分,数据信息

BMPINFOHEADER_T bih;

bih.biSize = sizeof(BMPINFOHEADER_T);

bih.biWidth = width;

bih.biHeight = -height;//BMP图片从最后一个点开始扫描,显示时图片是倒着的,所以用-height,这样图片就正了

bih.biPlanes = 1;//为1,不用改

bih.biBitCount = 24;

bih.biCompression = 0;//不压缩

bih.biSizeImage = size;

bih.biXPelsPerMeter = 2835 ;//像素每米

bih.biYPelsPerMeter = 2835 ;

bih.biClrUsed = 0;//已用过的颜色,24位的为0

bih.biClrImportant = 0;//每个像素都重要

FILE * fp = fopen( bmp_file,"wb" );

if( !fp ) return;

fwrite( &bfh, 8, 1, fp );//由于linux上4字节对齐,而信息头大小为54字节,第一部分14字节,第二部分40字节,所以会将第一部分补齐为16自己,直接用sizeof,打开图片时就会遇到premature end-of-file encountered错误

fwrite(&bfh.bfReserved2, sizeof(bfh.bfReserved2), 1, fp);

fwrite(&bfh.bfOffBits, sizeof(bfh.bfOffBits), 1, fp);

fwrite( &bih, sizeof(BMPINFOHEADER_T),1,fp );

fwrite(pdata,size,1,fp);

fclose( fp );

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值