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

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

我找到了一些如上所示的代码行,但我不知道 ucpImageBuffer 中应该包含什么?

而且我也不知道从哪里开始?

最佳答案

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

关于c - 使用 C 查找 JPG/JPEG 图像的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632268/

相关文章:

android - 包装内容与设置 dp

C - 制作 API 时进行 NULL 检查?

JavaScript screen.height 和 screen.width 返回不正确的值

html - Safari 浏览器中的 CSS 宽度最大内容

javascript - 如何为不同高度的多个实例设置CKEditor?

android导航栏隐藏和可用屏幕重叠百分比

html - 在 div 的大小中包含填充

c - 汇编和调用栈

c - 调用函数后如何设置全局数组的大小? (C)

c - 当指向它的指针作为参数传递给函数时如何填充结构