#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

const char filePath[]={"路径"};

typedef struct tagBmpFileHeader //文件头  
{  
    //unsigned short bfType;      //标识该文件为bmp文件,判断文件是否为bmp文件,即用该值与"0x4d42"比较是否相等即可,0x4d42 = 19778  
    unsigned long  bfSize;      //文件大小  
    unsigned short bfReserved1; //预保留位  
    unsigned short bfReserved2; //预保留位  
    unsigned long  bfOffBits;   //图像数据区的起始位置  
}BmpFileHeader;//14字节  
typedef struct tagBmpInfoHeader //信息头  
{  
    unsigned long  biSize;  //图像数据块大小  
    long     biWidth;   //宽度  
    long     biHeight;  //高度  
    unsigned short biPlanes;//为1  
    unsigned short biBitCount; //像素位数,8-灰度图;24-真彩色  
    unsigned long biCompression;//压缩方式  
    unsigned long biSizeImage;  //图像区数据大小  
    long     biXPelsPerMeter;  //水平分辨率,像素每米  
    long     biYPelsPerMeter;  
    unsigned long biClrUsed;   //位图实际用到的颜色数  
    unsigned short biClrImportant;//位图显示过程,重要的颜色数;0--所有都重要  
}BmpInfoHeader;//40字节 

void main()
{
	int fp=-1;
	unsigned short bfType;
	BmpFileHeader file_head;
	BmpInfoHeader info_head;
	fp=open(filePath,O_RDWR,0777);
	if(-1 == fp)
	{
		printf("%s open  failure!!\n",filePath);
	}
	printf("%s open success!\n",filePath);
	
	read(fp,&bfType,sizeof(bfType));
	read(fp,&file_head,sizeof(file_head));
	read(fp,&info_head,sizeof(info_head));
	printf("file head:\n");
	printf("bfType=%x bfSize=%lu ",bfType,file_head.bfSize);
	printf("bfReserved1=%u bfReserved2=%u ",file_head.bfReserved1,file_head.bfReserved2);
	printf("bfOffBits=%lu\n",file_head.bfOffBits);
	
	printf("info head:\n");
	printf("biSize=%lu biWidth=%ld biHeight=%ld ",info_head.biSize,info_head.biWidth,info_head.biHeight);
	printf("biPlanes=%u biBitCount=%u ",info_head.biPlanes,info_head.biBitCount);
	printf("biCompression=%lu biSizeImage=%lu ",info_head.biCompression,info_head.biSizeImage);
	printf("biXPelsPerMeter=%ld biYPelsPerMeter=%ld ",info_head.biXPelsPerMeter,info_head.biYPelsPerMeter);
	printf("biClrUsed=%lu biClrImportant=%u\n",info_head.biClrUsed,info_head.biClrImportant);
	close(fp);
}

Logo

更多推荐