在不解码图像的情况下获得JPEG分辨率 [英] Getting JPEG resolution without decoding the image

查看:187
本文介绍了在不解码图像的情况下获得JPEG分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不解码文件的情况下获得JPEG图像的分辨率。我从互联网上获得了几个样本,但没有一个能正常工作。之所以这样,是因为许多 JPEG文件不是标准的,尽管任何图形应用程序(Irfan,PSP,Firefox等)都可以打开它们。

I am trying to get the resolution of a JPEG image without decoding the file. I got several samples from internet but none is working properly. It seems to be this way because many JPEG files are not standard, though any graphic application (Irfan, PSP, Firefox etc) can open them.

JPEG的标头应该是:

The header of a JPEG was supposed to be:

typedef struct _JFIFHeader
{
  BYTE SOI[2];          /* 00h  Start of Image Marker     */
  BYTE APP0[2];         /* 02h  Application Use Marker    */
  BYTE Length[2];       /* 04h  Length of APP0 Field      */
  BYTE Identifier[5];   /* 06h  "JFIF" (zero terminated) Id String */
  BYTE Version[2];      /* 07h  JFIF Format Revision      */
  BYTE Units;           /* 09h  Units used for Resolution */
  BYTE Xdensity[2];     /* 0Ah  Horizontal Resolution     */
  BYTE Ydensity[2];     /* 0Ch  Vertical Resolution       */
  BYTE XThumbnail;      /* 0Eh  Horizontal Pixel Count    */
  BYTE YThumbnail;      /* 0Fh  Vertical Pixel Count      */
} JFIFHEAD;

但是,当我查看这些非标准文件之一时,Xdensity和Ydensity字段是错误的。但同样,所有图形应用程序都可以读取此非标准文件。

However, when I looked into one of those non-standard files, the Xdensity and Ydensity fields were wrong. But again, all graphic applications can read this non-standard file.

有人知道一段实际上可以读取所有JPEG文件的Delphi代码吗?

Does anybody knows a piece of Delphi code that can actually read all JPEG files?

Delphi 7,Win 7 32位

Delphi 7, Win 7 32 bit

推荐答案

以下代码可以帮助您获取所需数据:

Here is some code which could help you get the data you want:

function GetJpegSize(jpeg: TMemoryStream; out width, height, BitDepth: integer): boolean;
var n: integer;
    b: byte;
    w: Word;
begin
  result := false;
  n := jpeg.Size-8;
  jpeg.Position := 0;
  if n<=0 then
    exit;
  jpeg.Read(w,2);
  if w<>$D8FF then
    exit; // invalid format
  jpeg.Read(b,1);
  while (jpeg.Position<n) and (b=$FF) do begin
    jpeg.Read(b,1);
    case b of
      $C0..$C3: begin
        jpeg.Seek(3,soFromCurrent);
        jpeg.Read(w,2);
        height := swap(w);
        jpeg.Read(w,2);
        width := swap(w);
        jpeg.Read(b,1);
        BitDepth := b*8;
        Result := true; // JPEG format OK
        exit;
      end;
      $FF:
        jpeg.Read(b,1);
      $D0..$D9, $01: begin
        jpeg.Seek(1,soFromCurrent);
        jpeg.Read(b,1);
      end;
      else begin
        jpeg.Read(w,2);
        jpeg.Seek(swap(w)-2, soFromCurrent);
        jpeg.Read(b,1);
      end;
    end;
  end;
end;

这篇关于在不解码图像的情况下获得JPEG分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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