为什么 TImage 旋转我的图像?

问题描述 投票:0回答:4

编写移动应用程序 - 它从安全网站提取图像,如下所示(第一个图像)提取不正确(注意网络版本与移动版本),第二个图像在网站上正确显示,但 Delphi TImage 正在将其旋转一些原因,我不明白为什么。旋转设置为 0,并且在 TImage 组件上设置“Fit”。

想法?

delphi firemonkey timage
4个回答
15
投票

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;
    

7
投票
Exif 规范定义了方向标签来指示相机相对于拍摄场景的方向。因此,某些应用程序可以自动旋转与此 EXIF 标志相对应的图像。我猜你的网络版本会自动旋转。 TImage 没有。


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


0
投票
原始海报使用 FMX 从库中获取图像。 最有可能使用 FMX.MediaLibrary.Actions.TTakePhotoFromLibraryAction。

而且似乎没有办法获取方向信息。

FMX 似乎仍然忽略方向信息,或者至少我找不到任何可用于获取它的字段或属性或方法。

如果有这样的属性/方法/等,FMX 可能会考虑它。

现在我的状态和7年前问这个问题的人是一样的。一种方法是使用 ccr-exif 库,它支持 FMX,但要分析文件,我们需要文件的路径,但我认为我们现在不能使用 FMX 来做到这一点。

所以是的,如果有人知道如何使用 FMX 从库加载图像时获取方向,并在这里分享,那就太好了。

© www.soinside.com 2019 - 2024. All rights reserved.