我试图用c语言找到jpg / jpeg图像的高度和宽度。
我看过一些代码行
iPos = iPos + 5;
*ipHeight = ucpImageBuffer[iPos]<<8|ucpImageBuffer[iPos+1];
*ipWidth = ucpImageBuffer[iPos+2]<<8|ucpImageBuffer[iPos+3];
printf("\nW x H = %d x %d\n\n",*ipWidth,*ipHeight);
我发现上面显示了一些代码行,但我不知道ucpImageBuffer
应该包含哪些内容?
我也不知道从哪里开始?
答案 0 :(得分:1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int iHeight=0, iWidth=0, iPos, i;
char *cpFileName = "/images/image1.jpg";
FILE *fp = fopen(cpFileName,"rb");
fseek(fp,0,SEEK_END);
long len = ftell(fp);
fseek(fp,0,SEEK_SET);
unsigned char *ucpImageBuffer = (unsigned char*) malloc (len+1);
fread(ucpImageBuffer,1,len,fp);
fclose(fp);
printf("\n\nBuffer size %ld", len);
/*Extract start of frame marker(FFCO) of width and hight and get the position*/
for(i=0;i<len;i++)
{
if((ucpImageBuffer[i]==0xFF) && (ucpImageBuffer[i+1]==0xC0) )
{
iPos=i;
break;
}
}
/*Moving to the particular byte position and assign byte value to pointer variable*/
iPos = iPos + 5;
iHeight = ucpImageBuffer[iPos]<<8|ucpImageBuffer[iPos+1];
iWidth = ucpImageBuffer[iPos+2]<<8|ucpImageBuffer[iPos+3];
printf("\nWxH = %dx%d\n\n", iWidth, iHeight);
free(ucpImageBuffer);
}