画像のExifタグ「Orientation」を変更する
Dec 14 2020
特定の画像のExifタグ「Orientation」(0x0112)をコードで変更しようとしています。
ここで、読み取りに関する実用的な例を見つけましたが、同じタグを書き込むことができません。
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;
実行時に、次EAccessViolation
のGPImage.SetPropertyItem(PI);
行が発生します。
モジュール 'msvcrt.dll'のアドレス757A8E30でのアクセス違反。アドレス00000006の読み取り。
これは私のtest_up.jpgです:

回答
1 fpiette Dec 14 2020 at 16:09
私はこのコードを正常に使用しています:
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;
これは、アプリケーションで作成した関数です。GitHubの完全なソースコード。