unit ExifImport;
(*********************************************************************
Copyright (C) 2000 Lars B. Dybdahl, lars@dybdahl.dk
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*********************************************************************)
interface
uses
classes;
// Specify the .jpg OR the .txt files
type
TExifImporter=
class
public
ImportTimeStampFromExifTags:boolean;
ImportTimeStampFromFiles:boolean;
Verbose:boolean;
MachineReadable:boolean;
Files:TStringList;
constructor Create;
destructor Destroy; override;
procedure Execute;
end;
implementation
uses
SysUtils,
{$ifdef win32}
Windows,
{$endif}
cptext, ExifReader;
{$ifdef win32}
function GetFileTimestamp (filename:string):TDateTime;
var
f:THandle;
FindData:TWin32FindData;
localtime:TFileTime;
localsystemtime:TSystemTime;
begin
Result:=0;
f:=FindFirstFile (PChar(filename),FindData);
if f=INVALID_HANDLE_VALUE then
raise Exception.Create ('Kunne ikke finde filens dato og tid.');
try
if not FileTimeToLocalFileTime(FindData.ftLastWriteTime,localtime) then
raise Exception.Create ('Cannot convert to local time');
if not FileTimeToSystemTime (localtime,localsystemtime) then
raise exception.create ('Cannot convert to system time');
Result:=EncodeDate (localsystemtime.wYear,localsystemtime.wMonth,localsystemtime.wDay)+
(localsystemtime.wHour+(localsystemtime.wMinute+localsystemtime.wSecond/60)/60)/24;
finally
FindClose (f);
end;
end;
{$else}
function GetFileTimestamp (filename:string):TDateTime;
begin
{ TODO 1 -oLars Dybdahl : Find out, whether this returns local time or UTC }
Result:=FileDateToDateTime(FileAge(filename));
end;
{$endif}
function Float2StrUS (d:double):string;
var
i:integer;
begin
Result:=FloatToStr(d);
i:=pos(',',Result);
if i<>0 then
Result[i]:='.';
end;
constructor TExifImporter.Create;
begin
inherited Create;
files:=TStringList.Create;
ImportTimeStampFromExifTags:=false;
ImportTimeStampFromFiles:=false;
Verbose:=false;
MachineReadable:=false;
end;
destructor TExifImporter.Destroy;
begin
FreeAndNil (files);
inherited;
end;
procedure TExifImporter.Execute;
var
i:integer;
ini:TcpIniFile;
FileTimestamp:TDateTime;
exif:TExifReader;
begin
for i:=0 to files.count-1 do begin
if verbose then begin
if MachineReadable then
Writeln ('#File: '+files.strings[i])
else
Writeln ('Processing file: '+files.strings[i]);
Flush (output);
end;
exif:=TExifReader.Create (files.strings[i]);
try
ini:=TcpIniFile.Create (changefileext(files.Strings[i],'.txt'));
try
if ImportTimeStampFromExifTags and (exif.DateTimeOriginal<>'') then begin
if ini.ReadString('Global','Date','')='' then
ini.WriteString ('Global','Date',copy(exif.DateTimeOriginal,1,10));
if ini.ReadString('Global','Time','')='' then
ini.WriteString ('Global','Time',copy(exif.DateTimeOriginal,12,8));
end else
if ImportTimeStampFromFiles then begin
FileTimestamp:=GetFileTimestamp (files.Strings[i]);
ini.WriteString ('Global','Date',FormatDateTime('yyyy"-"mm"-"dd',FileTimestamp));
ini.WriteString ('Global','Time',FormatDateTime('hh":"nn":"ss',FileTimestamp));
end;
if exif.ImageInputEquipmentModel<>'' then begin
ini.WriteString ('Global','Camera',exif.ImageInputEquipmentModel);
ini.WriteString ('FromJPEG','InputEquipmentModel',exif.ImageInputEquipmentModel);
end;
if exif.ImageInputEquipmentManufacturer<>'' then begin
ini.WriteString ('Global','CameraManufacturer',exif.ImageInputEquipmentManufacturer);
ini.WriteString ('FromJPEG','InputEquipmentManufacturer',exif.ImageInputEquipmentManufacturer);
end;
// Exif data
if exif.ExifVersion<>'' then
ini.WriteString ('FromJPEG','ExifVersion',exif.ExifVersion);
if exif.ImageWidth>1 then
ini.WriteInteger ('FromJPEG','ImageWidth',exif.ImageWidth);
if exif.ImageHeight>1 then
ini.WriteInteger ('FromJPEG','ImageHeight',exif.ImageHeight);
if exif.Software<>'' then
ini.WriteString ('FromJPEG','Software',exif.Software);
if exif.SubjectDistance.ValueHasBeenSet then
ini.WriteString ('FromJPEG','SubjectDistance',RationalToFloatStr(exif.SubjectDistance));
if exif.FNumber.ValueHasBeenSet then
ini.WriteString ('FromJPEG','FNumber',RationalToFloatStr(exif.FNumber));
if exif.ExposureTime.ValueHasBeenSet then
ini.WriteString ('FromJPEG','ExposureTime',RationalToFloatStr(exif.ExposureTime));
if exif.ApertureValue.ValueHasBeenSet then
ini.WriteString ('FromJPEG','ApertureValue',RationalToFloatStr(exif.ApertureValue));
if exif.SpectralSensitivity<>'' then
ini.WriteString ('FromJPEG','SpectralSensitivity',exif.SpectralSensitivity);
if exif.RelatedAudioFile<>'' then
ini.WriteString ('FromJPEG','RelatedAudioFile',exif.RelatedAudioFile);
if exif.DateTimeOriginal<>'' then
ini.WriteString ('FromJPEG','DateTimeOriginal',exif.DateTimeOriginal);
if exif.DateTimeDigitized<>'' then
ini.WriteString ('FromJPEG','DateTimeDigitized',exif.DateTimeDigitized);
if exif.ExposureProgram.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','ExposureProgram',exif.ExposureProgram.Value);
ini.WriteString ('FromJPEG','ExposureProgramEnglish',ExplainExposureProgram(exif.ExposureProgram.Value));
end;
if exif.ISOSpeedRatings.ValueHasBeenSet then
ini.WriteInteger ('FromJPEG','ISOSpeedRatings',exif.ISOSpeedRatings.Value);
if exif.ShutterSpeedValue.ValueHasBeenSet then
ini.WriteString ('FromJPEG','ShutterSpeedValue',RationalToFloatStr(exif.ShutterSpeedValue));
if exif.BrightnessValue.ValueHasBeenSet then
ini.WriteString ('FromJPEG','BrightnessValue',RationalToFloatStr(exif.BrightnessValue));
if exif.ExposureBiasValue.ValueHasBeenSet then
ini.WriteString ('FromJPEG','ExposureBiasValue',RationalToFloatStr(exif.ExposureBiasValue));
if exif.MaxApertureValue.ValueHasBeenSet then
ini.WriteString ('FromJPEG','MaxApertureValue',RationalToFloatStr(exif.MaxApertureValue));
if exif.MeteringMode.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','MeteringMode',exif.MeteringMode.Value);
ini.WriteString ('FromJPEG','MeteringModeEnglish',ExplainMeteringMode(exif.MeteringMode.Value));
end;
if exif.LightSource.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','LightSource',exif.LightSource.Value);
ini.WriteString ('FromJPEG','LightSourceEnglish',ExplainLightSource(exif.LightSource.Value));
end;
if exif.Flash.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','Flash',exif.Flash.Value);
ini.WriteString ('FromJPEG','FlashEnglish',ExplainFlash(exif.Flash.Value));
end;
if exif.FocalLength.ValueHasBeenSet then
ini.WriteString ('FromJPEG','FocalLength',RationalToFloatStr(exif.FocalLength));
if exif.FlashEnergy.ValueHasBeenSet then
ini.WriteString ('FromJPEG','FlashEnergy',RationalToFloatStr(exif.FlashEnergy));
if exif.FocalPlaneXResolution.ValueHasBeenSet then
ini.WriteString ('FromJPEG','FocalPlaneXResolution',RationalToFloatStr(exif.FocalPlaneXResolution));
if exif.FocalPlaneYResolution.ValueHasBeenSet then
ini.WriteString ('FromJPEG','FocalPlaneYResolution',RationalToFloatStr(exif.FocalPlaneYResolution));
if exif.FocalPlaneResolutionUnit.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','FocalPlaneResolutionUnit',exif.FocalPlaneResolutionUnit.Value);
ini.WriteString ('FromJPEG','FocalPlaneResolutionUnitEnglish',ExplainResolutionUnit(exif.FocalPlaneResolutionUnit.Value));
end;
if exif.ExposureIndex.ValueHasBeenSet then
ini.WriteString ('FromJPEG','ExposureIndex',RationalToFloatStr(exif.ExposureIndex));
if exif.SensingMethod.ValueHasBeenSet then begin
ini.WriteInteger ('FromJPEG','SensingMethod',exif.SensingMethod.Value);
ini.WriteString ('FromJPEG','SensingMethodEnglish',ExplainResolutionUnit(exif.SensingMethod.Value));
end;
// Exif GPS info
if exif.GPSLatitudeRef<>'' then
ini.WriteString ('FromJPEG','GPSLatitudeRef',exif.GPSLatitudeRef);
if exif.GPSLongitudeRef<>'' then
ini.WriteString ('FromJPEG','GPSLongitudeRef',exif.GPSLongitudeRef);
finally
FreeAndNil (ini);
end;
finally
FreeAndNil (exif);
end;
end;
end;
end.