Exif example for Delphi

This example does the following:

1.

Reads all of the comments in an existing Exif file, updating the Comment array.

2.

Loads and modifies the file's image.

3.

Updates some of the comments in the comment array.

4.

Saves the file.

5.

Reads and displays the updated comments.

 

Note:

Comments for GIF, TIFF, and DICOM files are simpler than the ones in this Exif example. With those file types, all of the comments are strings that you can get and set with simple assignments.

 

 

To compile this example, you should add LEADDEF to the uses section of your unit file.

procedure TForm1.Button1Click(Sender: TObject);
var
Empty: Variant; { For clearing comments }
SubjectDistance: Variant; { Array for CMNT_SUBJECTDISTANCE }
NewSubjectDistance: Variant; { Array to be updated by ReadComment }
NewDateTime: string; { String to be updated by ReadComment }
MyComment: Variant; { Array for CMNT_USERCOMMENT }
MyCommentText: string; { String for CMNT_USERCOMMENT }
MyCommentSize: Integer; { Size of the comment array }
NewComment: Variant; { Array to be updated by ReadComment }
NewCommentText: string; { String for CMNT_USERCOMMENT that we read }
TmpString: string; { For parsing the comment string into bytes }
FilePath, DistanceString, ThisDate, ThisTime, MyMsg: string; {Miscellaneous }
ArraySize, LowerBound, UpperBound: Integer; { Array measurements }
i: Integer; { Counter }

begin
{ Create the Subject Distance arrays. }
SubjectDistance := VarArrayCreate([0, 1], varInteger);
NewSubjectDistance := VarArrayCreate([0, 1], varInteger);

{ Specify the file that we will update. }
FilePath := 'd:\lead\images\exif1.tif';

{ Specify the Empty variant for clearing comments. }
VarClear(Empty);

{ Get all of the current comments from an Exif file. }
{ Temporarily disable method errors so that we do not fail when comments are missing. }
Lead1.EnableMethodErrors := False;
for i := 0 to CMNT_LAST do
begin
  Lead1.Comment[i] := Empty;
  Lead1.Comment[i] := Lead1.ReadComment(FilePath, 0, i);
end;
Lead1.EnableMethodErrors := True;

{ Load and modify the image. }
Lead1.Load(FilePath, 0, 0, 1);
Lead1.Reverse();

{ Update the date when the file is generated. }
ThisDate := FormatDateTime('yyyy:mm:dd hh:nn:ss', Now);
Lead1.Comment[CMNT_SZDATETIMEDIGITIZED] := ThisDate;
Lead1.Comment[CMNT_SZSUBSECTIMEDIGITIZED] := Empty;

{ Change the subject distance to 4 1/3 meters. }
SubjectDistance[0] := 13;
SubjectDistance[1] := 3;
Lead1.Comment[CMNT_SUBJECTDISTANCE] := SubjectDistance;

{ Specify the user comment string. }
MyCommentText := 'This is my new comment.';
MyCommentSize := 8 + StrLen(PChar(MyCommentText));

{ Define the array for the user comment. }
MyComment := VarArrayCreate([0, MyCommentSize], varByte);

{ Fill in the ASCII prefix. }
MyComment[0] := Byte ('A');
MyComment[1] := Byte ('S');
MyComment[2] := Byte ('C');
MyComment[3] := Byte ('I');
MyComment[4] := Byte ('I');
MyComment[5] := Byte (0);
MyComment[6] := Byte (0);
MyComment[7] := Byte (0);

{ Fill in the rest of the comment. }
for i := 8 to (MyCommentSize - 1) do
begin
  TmpString := Copy(MyCommentText, i-7, 1);
  MyComment[i] := Byte (TmpString[1]);
end;
MyComment[MyCommentSize] := Byte (0);

{ Update the user comment. }
Lead1.Comment[CMNT_USERCOMMENT] := Empty;
Lead1.Comment[CMNT_USERCOMMENT] := MyComment;

{ Save the file and read the comments that we saved. }
Lead1.Save(FilePath, FILE_EXIF, 24, 0, SAVE_OVERWRITE);

NewDateTime := Lead1.ReadComment(FilePath, 0, CMNT_SZDATETIMEDIGITIZED);
NewSubjectDistance := Lead1.ReadComment(FilePath, 0, CMNT_SUBJECTDISTANCE);
NewComment := Lead1.ReadComment(FilePath, 0, CMNT_USERCOMMENT);

{ Interpret the Subject-Distance RATIONAL. }
DistanceString := FloatToStr(NewSubjectDistance[0] / NewSubjectDistance[1]);

{ Initialize the string for the message box. }
MyMsg := 'Subject Distance = ' + DistanceString + CHR(13) + 'Date/Time = ' + NewDateTime;

{ Add the ASCII comment if it is there. }
if NewComment[0] = Byte ('A') then
begin
  UpperBound := VarArrayHighBound(NewComment, 1);
  LowerBound := VarArrayLowBound(NewComment, 1);
  ArraySize := UpperBound - LowerBound;
  for i := 8 to ArraySize do
  begin
    NewCommentText := NewCommentText + Chr(Integer(NewComment[i]));
  end;
  MyMsg := MyMsg + CHR(13) + CHR(13) + NewCommentText;
end;

{ Display the message. }
Application.MessageBox(PChar(MyMsg), 'File Comments', mb_OK);

{ Clear the comments from memory. }
for i := 0 To CMNT_LAST do Lead1.Comment[i] := Empty;

end;