Einzelnen Beitrag anzeigen

Willie1

Registriert seit: 28. Mai 2008
618 Beiträge
 
Delphi 10.1 Berlin Starter
 
#63

AW: JPG-Datei drehen und speichern -> Verlust der Exif-Daten

  Alt 18. Jul 2020, 18:01
Hallo Leute,
ich habe mir zum Drehen (im Speicher) von Bildern nach dem Orientation-Tag das Folgende ausgedacht.
Delphi-Quellcode:
uses GDIPAPI, GDIPOBJ;
procedure TForm1.Button1Click(Sender: TObject);
var
  GPImage: TGPImage;
  GPGraphics: TGPGraphics;
  pPropItem: PPropertyItem;
  BufferSize: Cardinal;
  Orientation: Byte;
  RotateType: TRotateFlipType;
  W,H: Integer;
  Ratio: Double;
  Exif: Boolean;

begin
  if opd.Execute then begin
    GPImage := TGPImage.Create(opd.FileName);
    try
      Exif:=false;
      BufferSize := GPImage.GetPropertyItemSize(PropertyTagOrientation);
      try
        if BufferSize > 0 then // 0 = kein/e Metadaten oder Orientation-Tag
        begin
          Exif:=true;
          GetMem(pPropItem, BufferSize);
          GPImage.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);
        end;
        Ratio:=GPImage.GetWidth / img.Width;
        if Ratio < GPImage.GetHeight / img.Height then Ratio:=GPImage.GetHeight / img.Height;
        W:=Round(GPImage.GetWidth / Ratio);
        H:=Round(GPImage.GetHeight / Ratio);
        ClearImage(img);
        try
          GPGraphics:=TGPGraphics.Create(img.Canvas.Handle);
          GPGraphics.DrawImage(GPImage, (img.Width - W) shr 1, (img.Height - H) shr 1, W, H)
        finally
          GPGraphics.Free;
        end;
      finally
        if Exif then FreeMem(pPropItem);
      end;
    finally
      GPImage.Free
    end;
  end;
end;
Dabei war mir ein Beitrag in StackOverflow sehr hilfreich. Ich denke, dass ist eine gute Lösung. Was meint ihr?
Man kann das Bild natürlich auch speichern, dann müsst ihr aber noch das Or.-Tag anpassen und das Thumbnail drehen.
Willie.
Gut hören kann ich schlecht, schlecht sehen kann ich gut - Ersteres stimmt nicht, das zweite schon.

Geändert von Willie1 (18. Jul 2020 um 18:22 Uhr) Grund: Rechtschreibung
  Mit Zitat antworten Zitat