开发者

How to get Date and Time the picture was taken at?

开发者 https://www.devze.com 2023-01-31 06:33 出处:网络
I am writing a program in Delphi which should get the date and time of the picture which was taken with photo cam and then it would rename the file to include the date+time it found.

I am writing a program in Delphi which should get the date and time of the picture which was taken with photo cam and then it would rename the file to include the date+time it found.

So far i have achieved that by opening file as binary and searching for a special order of bytes. These bytes were then followed by the date and then time. So i've come to a problem. Actually few problems.

  1. Because it reads the file 1 byte after another, reading a file is a slow process. If the date was found, it is usually at the start of file, it doesn't take long, however if 'special byte order' was not found, it will read the whole file. So my method is way too slow.

  2. The special byte order may change in some pictures (i have no idea why) even if it was taken with the same camera. So my program sometimes fails to find the date in the file even though it is there.

Windows explorer has no problems finding date in all of the pictures, so i was thinking maybe there are some kind of special functions which could get me what i need?

How do i get the information i need from the picture so it wor开发者_开发知识库ks with all the formats?

Thanks


I think you only need to look at the EXIF information. http://en.wikipedia.org/wiki/Exif

There are some open source tools which accomplish that, but I don't know of anything Delphi specific. If you're not scared of Java, you can have a look at the source code of this open source project: http://sourceforge.net/projects/jexifviewer/ to see how they evaluate the date field.
You can then optimise your reader, to only look at the relevant area. You might want to keep in mind that the Endianness in Java is different to Delphi.


Jules is right about Exif data; you might want to try this Delphi library:

http://delphihaven.wordpress.com/ccr-exif/


This is an amazing site to view Exif data.

http://regex.info/exif.cgi


If it is a graphic file (as in your case), to open it with a dialog box, place a TOpenPictureDialog component on the form. Also place the component TLabel, for displaying the date of creation of the file. In the button's place the following code:

if OpenPictureDialog1.Execute then Label1.Caption := DateTimeToStr(FileDateToDateTime(FileAge(OpenPictureDialog1.FileName));

In order to open jpeg and png files in the code, in the line uses you need to add the name of the two libraries, JPEG, PNGImage.

If you have the full address of your file, you can write the code above instead of the code above:

Label1.Caption := DateTimeToStr(FileDateToDateTime(FileAge('full address to file')));

If you only need the date, without the file creation time, then instead of the DateTimeToStr command, use the DateToStr command.

0

精彩评论

暂无评论...
验证码 换一张
取 消