从二进制文件中获取 JPEG 的图像大小 [英] Getting Image size of JPEG from its binary

查看:18
本文介绍了从二进制文件中获取 JPEG 的图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多图像大小不同的 jpeg 文件.例如,这里是大小为 256*384(像素)的图像的 hexdump 给出的前 640 个字节:

0000000: ffd8 ffe0 0010 4a46 4946 0001 0101 0048 ......JFIF.....H0000010: 0048 0000 ffdb 0043 0003 0202 0302 0203 .H.......C.......0000020: 0303 0304 0303 0405 0805 0504 0405 0a07 ......0000030:0706 080c 0a0c 0c0b 0a0b 0b0d 0e12 100d ......

我猜尺寸信息一定在这些行之内.但是我无法看到哪些字节正确地给出了大小.谁能帮我找到包含尺寸信息的字段?

解决方案

根据语法和结构维基百科上的JPEG页面的部分,图像的宽度和高度不't 似乎存储在图像本身中——或者,至少,不是以一种很容易找到的方式.


不过,引用 JPEG 图像压缩常见问题解答,第 1/2 部分:p><块引用>

主题:[22] 我的程序如何从 JPEG 文件中提取图像尺寸文件?

JPEG 文件的标题包括一系列块,称为标记".图片高度和宽度被存储在 SOFn 类型的标记中 (Start Of框架,类型 N).
查找 SOFn你必须跳过前面的标记;你不必知道是什么在其他类型的标记中,只需用他们的长词跳过他们.
所需的最低逻辑是也许是一页 C 代码.
(有些人们建议只是搜索对于表示 SOFn 的字节对,不注意标记块结构.这是不安全的因为先前的标记可能包含SOFn 模式,无论是偶然还是因为它包含 JPEG 压缩缩略图.如果你不跟随您将检索的标记结构缩略图的大小而不是主图像大小.)
大量可以在 C 中找到注释的示例IJG 发行版中的 rdjpgcom.c(见第 2 部分,第 15 项).
Perl 代码可以在 wwwis 中找到,从http://www.tardis.ed.ac.uk/~ark/wwwis/.

(呃,那个链接好像坏了……)


不过,这里有一部分 C 代码可以帮助您:解码 JPEG 的宽度和高度(JFIF) 文件

I have a lot of jpeg files with varying image size. For instance, here is the first 640 bytes as given by hexdump of an image of size 256*384(pixels):

0000000: ffd8 ffe0 0010 4a46 4946 0001 0101 0048  ......JFIF.....H
0000010: 0048 0000 ffdb 0043 0003 0202 0302 0203  .H.....C........
0000020: 0303 0304 0303 0405 0805 0504 0405 0a07  ................
0000030: 0706 080c 0a0c 0c0b 0a0b 0b0d 0e12 100d  ................

I guess the size information mus be within these lines. But am unable to see which bytes give the sizes correctly. Can anyone help me find the fields that contains the size information?

解决方案

According to the Syntax and structure section of the JPEG page on wikipedia, the width and height of the image don't seem to be stored in the image itself -- or, at least, not in a way that's quite easy to find.


Still, quoting from JPEG image compression FAQ, part 1/2 :

Subject: [22] How can my program extract image dimensions from a JPEG file?

The header of a JPEG file consists of a series of blocks, called "markers". The image height and width are stored in a marker of type SOFn (Start Of Frame, type N).
To find the SOFn you must skip over the preceding markers; you don't have to know what's in the other types of markers, just use their length words to skip over them.
The minimum logic needed is perhaps a page of C code.
(Some people have recommended just searching for the byte pair representing SOFn, without paying attention to the marker block structure. This is unsafe because a prior marker might contain the SOFn pattern, either by chance or because it contains a JPEG-compressed thumbnail image. If you don't follow the marker structure you will retrieve the thumbnail's size instead of the main image size.)
A profusely commented example in C can be found in rdjpgcom.c in the IJG distribution (see part 2, item 15).
Perl code can be found in wwwis, from http://www.tardis.ed.ac.uk/~ark/wwwis/.

(Ergh, that link seems broken...)


Here's a portion of C code that could help you, though : Decoding the width and height of a JPEG (JFIF) file

这篇关于从二进制文件中获取 JPEG 的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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