#include<stdio.h>#include<windows.h>#pragma pack(1)//BMP文件头(14字节)typedefstruct/**** BMP file header structure ****/{
unsignedint bfSize;/* Size of file */unsignedshort bfReserved1;/* Reserved */unsignedshort bfReserved2;/* ... */unsignedint bfOffBits;/* Offset to bitmap data */} MyBITMAPFILEHEADER;//位图信息头(40字节)typedefstruct/**** BMP file info structure ****/{
unsignedint biSize;/* Size of info header */int biWidth;/* Width of image */int biHeight;/* Height of image */unsignedshort biPlanes;/* Number of color planes */unsignedshort biBitCount;/* Number of bits per pixel */unsignedint biCompression;/* Type of compression to use */unsignedint biSizeImage;/* Size of image data */int biXPelsPerMeter;/* X pixels per meter */int biYPelsPerMeter;/* Y pixels per meter */unsignedint biClrUsed;/* Number of colors used */unsignedint biClrImportant;/* Number of important colors */} MyBITMAPINFOHEADER;voidMySaveBmp(constchar*filename,unsignedchar*rgbbuf,int width,int height){
MyBITMAPFILEHEADER bfh;
MyBITMAPINFOHEADER bih;/* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */unsignedshort bfType =0x4d42;
bfh.bfReserved1 =0;
bfh.bfReserved2 =0;
bfh.bfSize =sizeof(MyBITMAPFILEHEADER)+sizeof(MyBITMAPINFOHEADER)+ width * height *3;
bfh.bfOffBits =0x36;
bih.biSize =sizeof(MyBITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight =-height;
bih.biPlanes =1;
bih.biBitCount =24;
bih.biCompression =0;
bih.biSizeImage =0;
bih.biXPelsPerMeter =5000;
bih.biYPelsPerMeter =5000;
bih.biClrUsed =0;
bih.biClrImportant =0;
FILE *file =fopen(filename,"wb");if(!file){
printf("Could not write file\n");return;}/*Write headers*/fwrite(&bfType,sizeof(bfType),1, file);fwrite(&bfh,sizeof(bfh),1, file);fwrite(&bih,sizeof(bih),1, file);fwrite(rgbbuf, width*height *3,1, file);fclose(file);}voidRGBAtoRGB(char*buf,long lenght){
char*prgba = buf;char*prgb = buf;for(long i =0; i < lenght; i++, prgba++){
if((i +1)%4==0&& i !=0)continue;*prgb++=*prgba;}}voidrgbaTobmp(constchar*filename,unsignedchar*rgbbuf,int width,int height){
// 因为传入的是rgba,所以*4RGBAtoRGB((char*)rgbbuf, height*width *4);MySaveBmp(filename, rgbbuf, width, height);}voidmain(int argc,char*argv[]){
int conf =0;int devicenum =0;// image.data存储的是图片的rgba信息
FILE * file =fopen("image.data","rb");fwrite(image_data,1,k4a_image_get_size(color_image), file);fclose(file);char temp[100]={
0};sprintf(temp,"%d.bmp", count);rgbaTobmp(temp,(unsignedchar*)image_data, width, height);}