该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/*
** 这个程序是读取jpg图像的
** 后续加上jpg图像打开和存放
*/ #include
#include
#include
#include
#include #define SOI 0xD8 //文件头
#define EOI 0xD9 //文件尾
#define APP0 0xE0 //定义交换格式和图像识别信息
#define SOF0 0xC0 //帧开始(标准 JPEG)
#define DQT 0xDB //定义量化表
#define DHT 0xC4 //定义 Huffman 表(霍夫曼表)
#define SOS 0xDA // 扫描行开始
#define DRI 0xDD //定义重新开始间隔
#define COM 0xFE //注释 unsigned short jpgWidth; //图像的宽
unsigned short jpgHeight; //图像的高
unsigned char bitDepth; //图像类型,每像素位数
unsigned short jpgXpelsPerMeter; //水平分辨率
unsigned short jpgYpelsPerMeter; //垂直分辨率
unsigned char SOF_find=0; char readPath[100]; //图像路径
unsigned char CurrentByte; //当前字节
unsigned short CurrentWord; //当前word
unsigned int BytePos; //当前字节的位置 bool readjpg(char *jpgName); //读取路径
void showjpg(); //显示
void GetJpgSize(FILE *fp); //获取大小 /****************************分割线**************************************/
bool readjpg(char *jpgName)
{
unsigned char Flag_1,Flag_2; //文件头 FFD8
unsigned short Xdensity,Ydensity;
/*unsigned char SOS_find=0;
unsigned int FileLength;
unsigned char Length;*/
//二进制读方式打开指定的图像文件
FILE *fp;
fp=fopen(jpgName,"rb");
if(fp==0)
{
perror("jpgName"); //提示错误信息
getch();
exit(EXIT_FAILURE); //出现错误,终止程序。
} /*
判断文件类型是否为JPG
检查第1, 2字节
*/
fread(&Flag_1,1,1,fp);
fseek(fp,1L,0);
fread(&Flag_2,1,1,fp);
if(( Flag_1 != 0xff) || (Flag_2 != 0xd8) )
{
printf("Not a jpg file?? (SOI)\n");
exit(EXIT_FAILURE);
}
//开始寻找开始位置
GetJpgSize(fp); fclose(fp);
return 1;
} void showjpg()
{
//窗口大小为图片大小
initgraph(jpgWidth,jpgHeight+40);
setbkcolor(WHITE);
setcolor(BLACK);
cleardevice(); setfont(16, 8, "宋体");
char width[5],height[5];
sprintf(width, "%d",jpgWidth);
sprintf(height, "%d",jpgHeight); outtextxy(0,0,"图像的宽:");
outtextxy(10+textwidth("图像的宽:"),0,width); outtextxy(jpgWidth/2,0,"图像的高:");
outtextxy(jpgWidth/2 + 10 + textwidth("图像的高"),0,height); IMAGE img; // 定义 IMAGE 对象
loadimage(&img, readPath); //读取图片到 img 对象中
putimage(0, 40, &img); // 在坐标 (0, 0) 位置显示 IMAGE 对象 getch();
closegraph();
} //获取照片尺寸大小
void GetJpgSize(FILE *fp)
{ unsigned char HeightH;
unsigned char HeightL;
unsigned char WidthH;
unsigned char WidthL;
unsigned char BitDepth;
while(!SOF_find)
{
CurrentByte=fgetc(fp);
if(CurrentByte!=0xFF)
continue;
//发现标记符
CurrentByte=fgetc(fp);
switch(CurrentByte)
{
case APP0: break;
case SOF0:
fseek(fp,2L,1); BitDepth=fgetc(fp); //图像高的高位和低位
HeightH=fgetc(fp); HeightL=fgetc(fp); //图像宽的高位和低位
WidthH=fgetc(fp); WidthL=fgetc(fp); SOF_find=1;
break;
default:
break;
}
}
jpgHeight=HeightH * 256 + HeightL;
jpgWidth=WidthH * 256 + WidthL; } void main()
{
printf("输入图像的完整路径及文件名:\n");
gets(readPath);
readjpg(readPath);
printf(" bitDepth=%x\n height=%x\n width=%x\n ",bitDepth,jpgHeight,jpgWidth);
printf(" jpgXpelsPerMeter=%x\n jpgYpelsPerMeter=%x\n",jpgXpelsPerMeter,jpgYpelsPerMeter); printf("按任意键显示图像\n");
getch();
showjpg();
}