使用C查找JPG/JPEG图像的高度和宽度

使用C查找JPG/JPEG图像的高度和宽度,c,height,width,jpeg,resolution,C,Height,Width,Jpeg,Resolution,我试图用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); iPos=iPos+

我试图用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); 
iPos=iPos+5;
*ipHeight=ucpImageBuffer[iPos]
#包含
#包括
#包括
void main()
{
int-iHeight=0,iWidth=0,iPos,i;
char*cpFileName=“/images/image1.jpg”;
FILE*fp=fopen(cpFileName,“rb”);
fseek(fp,0,SEEK_END);
长长度=英尺(fp);
fseek(fp,0,SEEK_集);
无符号字符*ucpImageBuffer=(无符号字符*)malloc(len+1);
fread(ucpImageBuffer,1,len,fp);
fclose(fp);
printf(“\n\n缓冲大小%ld”,len);
/*提取宽度和高度的帧起始标记(FFCO)并获取位置*/

对于(i=0;这是我已经使用gcc编译器测试过的工作代码。这不是答案。请使用“编辑”按钮并在您的问题中添加更多信息。事实上,我在上传我的答案时得到了我的答案。为了帮助他人,我创建了此问题和答案。谢谢您的建议,但现在我有了问题的解决方案t上面的代码在所有/许多情况下都不起作用。带有缩略图的JPEG会将其丢弃。
#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);
}