Ändern des Exif-Tags eines Bildes "Ausrichtung"

Dec 14 2020

Ich versuche, das Exif-Tag "Orientation" (0x0112) für ein bestimmtes Bild per Code zu ändern .

Hier habe ich ein funktionierendes Beispiel zum Lesen gefunden, aber ich kann nicht dasselbe Tag schreiben.

uses
  GDIPAPI, GDIPOBJ, GDIPUTIL;

var
  GPImage: TGPImage;
  BufferSize: Cardinal;
  Orientation: Byte;
  RotateType: TRotateFlipType;
  EncoderClsid: TGUID;
  PI : PropertyItem;
begin
  GPImage := TGPImage.Create('.\test_up.jpg');
  try
    BufferSize := GPImage.GetPropertyItemSize(PropertyTagOrientation);
    if BufferSize <= 0
    then raise Exception.Create('BufferSize <= 0');

    Orientation := 6; //this should be Rotate90FlipNone

    PI.id := PropertyTagOrientation;
    PI.type_ := 3;
    PI.length := BufferSize;
    PI.value := PByte(Orientation);

    GPImage.SetPropertyItem(PI);

    GetEncoderClsid('image/jpeg', EncoderClsid);
    GPImage.Save('.\test_up_Rotate90FlipNone.jpg', EncoderClsid);
  finally
    GPImage.Free
  end;
end;

Zur Laufzeit wird EAccessViolationin der GPImage.SetPropertyItem(PI);Zeile Folgendes ausgelöst:

Zugriffsverletzung unter Adresse 757A8E30 im Modul 'msvcrt.dll'. Adresse 00000006 lesen.

Dies ist meine test_up.jpg :

Antworten

1 fpiette Dec 14 2020 at 16:09

Ich verwende diesen Code erfolgreich:

procedure TOvbCustomImage.SetImageOrientation(AGPImage: TGPImage; Value: WORD);
var
    PropItem : TPropertyItem;
begin
    if not Assigned(AGPImage) then
        Exit;
    PropItem.Id            := PropertyTagOrientation;
    PropItem.Length        := SizeOf(WORD);
    PropItem.Type_         := PropertyTagTypeShort;
    PropItem.Value         := @Value;
    AGPImage.SetPropertyItem(PropItem);
end;

Dies ist eine Funktion, die ich in meiner Anwendung geschrieben habe. Vollständiger Quellcode bei GitHub .