8

编写移动应用程序 - 它从安全网站拉取图片,如下所示(第一张图片)拉取不正确(注意网页版与移动版),第二张图片在网站上正确显示,但 Delphi TImage 出于某种原因正在旋转它我不知道为什么。Rotate 设置为 0,并且在 TImage 组件上设置了“Fit”。

想法?

在此处输入图像描述

为什么会这样旋转

4

3 回答 3

12

Jpeg 和 Tiff 具有指定图像方向(以及其他数据)的Exif (可交换图像文件格式)元数据。

这不是“TImage 旋转我的图像”。TImage 未处理 Exif 方向元数据。理想情况下,TImage应该根据方向元数据自动旋转图像,但事实并非如此。您需要阅读 Exif 方向属性并相应地旋转图像。

Exif 标记“方向”(0x0112) 规范是:

1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW

您可以使用一些免费的 Exif组件 ,例如 TExif/NativeJpg/CCR Exif,并在需要时根据方向标签旋转图像。

这是使用 GDI+ (VCL/Windows) 的示例,例如:

uses GDIPAPI, GDIPOBJ;

procedure TForm1.Button1Click(Sender: TObject);
var
  GPImage: TGPImage;
  GPGraphics: TGPGraphics;
  pPropItem: PPropertyItem;
  BufferSize: Cardinal;
  Orientation: Byte;
  RotateType: TRotateFlipType;
  Bitmap: TBitmap;
begin
  GPImage := TGPImage.Create('D:\Test\image.jpg');
  try
    BufferSize := GPImage.GetPropertyItemSize(PropertyTagOrientation);
    if BufferSize > 0 then
    begin
      GetMem(pPropItem, BufferSize);
      try
        GDPImage.GetPropertyItem(PropertyTagOrientation, BufferSize, pPropItem);
        Orientation := PByte(pPropItem.value)^;
        case Orientation of
          1: RotateType := RotateNoneFlipNone; // Horizontal - No rotation required
          2: RotateType := RotateNoneFlipX;
          3: RotateType := Rotate180FlipNone;
          4: RotateType := Rotate180FlipX;
          5: RotateType := Rotate90FlipX;
          6: RotateType := Rotate90FlipNone;
          7: RotateType := Rotate270FlipX;
          8: RotateType := Rotate270FlipNone;
        else
          RotateType := RotateNoneFlipNone; // Unknown rotation?
        end;
        if RotateType <> RotateNoneFlipNone then
          GPImage.RotateFlip(RotateType);
        Bitmap := TBitmap.Create;
        try
          Bitmap.Width := GPImage.GetWidth;
          Bitmap.Height := GPImage.GetHeight;
          Bitmap.Canvas.Lock;
          try
            GPGraphics := TGPGraphics.Create(Bitmap.Canvas.Handle);
            try
              GPGraphics.DrawImage(GPImage, 0, 0, GPImage.GetWidth, GPImage.GetHeight);
              Image1.Picture.Assign(Bitmap);              
            finally
              GPGraphics.Free;
            end;
          finally
            Bitmap.Canvas.Unlock;
          end;    
        finally
          Bitmap.Free;
        end;
      finally
        FreeMem(pPropItem);
      end;
    end;
  finally
    GPImage.Free
  end;
end;
于 2017-01-13T19:44:38.683 回答
7

The Exif specification defines an Orientation Tag to indicate the orientation of the camera relative to the captured scene. Some apps can therefore auto-rotate the image corresponding to this EXIF-Flag. I would guess that your web-version does this auto-rotate. TImage does it not.

于 2017-01-12T18:58:28.580 回答
2

网站当然会读取图片的exif数据,其中包含照片的方向,然后相应地旋转图像。德尔福没有。您必须为此阅读图片元数据(在谷歌上搜索“exif”)

于 2017-01-12T18:56:11.417 回答