简体   繁体   English

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

[英]Find JPG/JPEG image height and width using C

I am trying to find the height and width of jpg/jpeg image in c language.我试图用c语言找到jpg/jpeg图像的高度和宽度。

I have seen some lines of code我看到了一些代码行

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); 

I find some lines of code as shown above but I don't know what should be in ucpImageBuffer ?我找到了一些如上所示的代码行,但我不知道ucpImageBuffer应该有什么?

And i also don't know from where I have to start?我也不知道从哪里开始?

#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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM