如何获得JPEG文件的宽度/高度不使用库? [英] How to get the width/height of jpeg file without using library?

查看:345
本文介绍了如何获得JPEG文件的宽度/高度不使用库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说我试过很多次使用谷歌搜索找到答案,而且我发现很多的结果,但我不明白,因为我不知道读一个二进制文件的想法,并转换价值即获得可读价值。

Firstly I want to say I tried many times to find the answer by using google search, and I found many results but I did not understand, because I don't know the idea of reading a binary file, and convert the value that Obtained to readable value.

我试着做了。

unsigned char fbuff[16];
FILE *file;
file = fopen("C:\\loser.jpg", "rb");
if(file != NULL){
   fseek(file, 0, SEEK_SET);
   fread(fbuff, 1, 16, file);
   printf("%d\n", fbuff[1]);
   fclose(file);
}else{
   printf("File does not exists.");
}

我想例如一个简单的解释说明,如何从它的头获取的JPEG文件的宽度/高度,然后将该值转换为可读值。

推荐答案

不幸的是,它似乎并没有简单的JPEG。你应该看看源到 jhead的 命令行工具。它提供了这一信息。当通过源去,你会看到函数 ReadJpegSections 。这个函数扫描通过JPEG文件内包含以提取所需的信息的所有段。处理该有一个 SOFn 标记帧时,获得的图像的宽度和高度。

Unfortunately, it doesn't seem to be simple for JPEG. You should look at the source to the jhead command line tool. It provides this information. When going through the source, you will see the function ReadJpegSections. This function scans through all the segments contained within the JPEG file to extract the desired information. The image width and height is obtained when processing the frames that have an SOFn marker.

我看到的来源是在公共领域,所以我会表明,获取的图像信息的片段:

I see the source is in the public domain, so I'll show the snippet that gets the image info:

static int Get16m(const void * Short)
{
    return (((uchar *)Short)[0] << 8) | ((uchar *)Short)[1];
}

static void process_SOFn (const uchar * Data, int marker)
{
    int data_precision, num_components;

    data_precision = Data[2];
    ImageInfo.Height = Get16m(Data+3);
    ImageInfo.Width = Get16m(Data+5);

从源头code,但我很清楚,没有单一的头这个信息。您可以通过JPEG文件进行扫描,分析每个段,直到你找到你想要它的信息的段。这是在维基百科的文章描述:

一个JPEG图像是由段的序列,每个开头的标记,其每一个与一个0xFF的字节后面是表示它是什么样的标记的一个字节开始的。一些标记只包含这两个字节;其他人随后由两个字节表示如下标记特异性的有效载荷数据的长度。

A JPEG image consists of a sequence of segments, each beginning with a marker, each of which begins with a 0xFF byte followed by a byte indicating what kind of marker it is. Some markers consist of just those two bytes; others are followed by two bytes indicating the length of marker-specific payload data that follows.

一个JPEG文件由段序列的:


A JPEG file consists of a sequence of segments:

SEGMENT_0
SEGMENT_1
SEGMENT_2
...

每个段开头的2字节的标记。第一个字节是 0xFF的,第二个字节决定了段的类型。这之后是段的长度的编码。在该段的数据特定于该段的类型。

Each segment begins with a 2-byte marker. The first byte is 0xFF, the second byte determines the type of the segment. This is followed by an encoding of the length of the segment. Within the segment is data specific to that segment type.

图片宽度和高度类型的段找到 SOFn ,或者开始帧[N],其中n是一些数字,这意味着什么特别为JPEG去$ C $铬。它应该是不够好,只寻找一个 SOF0 ,其字节指定为将0xC0 。一旦你找到了这个框架,你可以去code将其找到图像的高度和宽度。

The image width and height is found in a segment of type SOFn, or "Start of frame [n]", where "n" is some number that means something special to a JPEG decoder. It should be good enough to look only for a SOF0, and its byte designation is 0xC0. Once you find this frame, you can decode it to find the image height and width.

因此​​,一个程序的结构做你想要看起来像什么:

So the structure of a program to do what you want would look like:

file_data = the data in the file
data = &file_data[0]
while (data not at end of file_data)
    segment_type = decoded JPEG segment type at data
    if (type != SOF0)
        data += byte length for segment_type
        continue
    else
        get image height and width from segment
        return

这是本质上迈克尔·彼得罗夫的 get_jpeg_size发现结构()实施

This is essentially the structure found in Michael Petrov's get_jpeg_size() implementation.

这篇关于如何获得JPEG文件的宽度/高度不使用库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆