jpg文件的定义很复杂 我上面提供的函数可以不用创建图片对象 将文件数据读到pHead中,解析就可以了 IPicture接口也不错,但是有许多Jpg文件解析不出来,速度也很慢 用jpeg官方提供的库函数,是正解//---------- BYTE *pHead;CFile f; f.Open("c:\\aaa.jpg",CFile::modeRead); int iWidth=f.GetLength(); pHead=new BYTE[iWidth]; f.Read(pHead,iWidth); f.Close(); int iHeight; ParsePic(pHead,iWidth,iHeight); //---------------- 此时,iWidth,iHeight就是图片的size
To ok1234567(ok1234567)朋友: 直接读取jpg文件中关于图片信息的方法很不错,上述代码在测试工程中也可得到正确图片大小的信息。想知道你所说的“这个解析还有极少数图片的高宽解析不正确(非常少)”,在什么场合会不正确?我开了个帖子: http://community.csdn.net/Expert/topic/4769/4769390.xml?temp=.1704065 敬请光临!
pHead为jpg数据指针
这个解析还有极少数图片的高宽解析不正确(非常少)
当然,如果利用jpeg的标准类库,那是没有问题的,我在使用中,重要的是性能,不想创建图片对象int ParsePic(BYTE *pHead, int &iWidth, int &iHeight)
{
BYTE n[4];
n[0]=pHead[0];
n[1]=pHead[1];
n[2]=pHead[2];
n[3]=pHead[3];
int i=1;
if(n[0] == 0xFF && n[1] == 0xD8) //jpeg
{
i=iWidth<40000?iWidth-1000:30000;
while(i>20)
{
i--;
if(pHead[i] < 0xC0) continue;
if(pHead[i] > 0xC3) continue;
i--;
if(pHead[i] != 0xFF) continue;
i+=5;
break;
}
if(i<=20) return -1;
/*
if(n[3] == 0xE1)
{
while(i<iWidth)//length
{
i++;
if(pHead[i] != 0xFF) continue;
i++;
if(pHead[i] != 0xC0) continue;
i+=4;
if(i > iWidth-10) return -2;
break;
}
if(i>=iWidth) return -1;
}
else
{
i=iWidth-100;
while(i>10)
{
i--;
if(pHead[i] != 0xC0 && pHead[i] != 0xC2) continue;
i--;
if(pHead[i] != 0xFF) continue;
i+=5;
break;
}
if(i<=10) return -1;
}
*/ iHeight=pHead[i]*256+pHead[i+1];
i+=2;
iWidth=pHead[i]*256+pHead[i+1];
return 0;
}
return 0;
}
IPicture::get_Width
IPicture::get_Height
ok1234567(ok1234567) 的代码缺少东西我说明个例,文件c:\aaa.jpg,得到此文件的宽高(就是此文件摘要中的那个值)
我上面提供的函数可以不用创建图片对象
将文件数据读到pHead中,解析就可以了
IPicture接口也不错,但是有许多Jpg文件解析不出来,速度也很慢
用jpeg官方提供的库函数,是正解//----------
BYTE *pHead;CFile f;
f.Open("c:\\aaa.jpg",CFile::modeRead);
int iWidth=f.GetLength();
pHead=new BYTE[iWidth];
f.Read(pHead,iWidth);
f.Close();
int iHeight;
ParsePic(pHead,iWidth,iHeight);
//----------------
此时,iWidth,iHeight就是图片的size
直接读取jpg文件中关于图片信息的方法很不错,上述代码在测试工程中也可得到正确图片大小的信息。想知道你所说的“这个解析还有极少数图片的高宽解析不正确(非常少)”,在什么场合会不正确?我开了个帖子:
http://community.csdn.net/Expert/topic/4769/4769390.xml?temp=.1704065
敬请光临!