Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Hi
I am looking for a way to programatically access the DateTaken attribute a .JPG photo has.
Can anyone give me an example in c++?

Thanks
Posted
Updated 6-Jul-14 10:21am
v2
Comments
Michael Haephrati 6-Jul-14 14:21pm    
Yes. I found this:


PropertyTagDateTime
Date and time the image was created.
Tag 0x0132
Type PropertyTagTypeASCII
Count 20

Not sure it is the correct one. but in any case, I think my question can be useful if fully answered with some code sample to save time to others in the future...
Okay. But I am not sure, if there are any codes for this as I can't find any. I guess nobody has tried this before. :)
Sergey Alexandrovich Kryukov 6-Jul-14 14:19pm    
Image file format have different structures for metadata. It depends on what is the format you use.
—SA
Michael Haephrati 6-Jul-14 14:22pm    
Well, The attribute DateTaken doesn't appear always, like "last access date" and "creation date" but only for photos. I am talking about .JPG files but maybe other formats as well.

It wasn't a matter of discussion before you clarified: JPEG. Thank you.

In this case, you need to parse EXIF:
http://en.wikipedia.org/wiki/EXIF[^].

See, for example, this library: http://www.exiv2.org/[^].

—SA
 
Share this answer
 
Comments
Maciej Los 6-Jul-14 15:37pm    
+5
Sergey Alexandrovich Kryukov 6-Jul-14 15:57pm    
Thank you, Maciej.
—SA
Michael Haephrati 6-Jul-14 16:20pm    
Thank you Sergey
I found the solution after reading this [^]excellent article.

So that parts actually give you the "Date Taken" property, which as Sergey rightly stated, is taken from the EXIF that only JPEG based files have.

EXIFINFO exifinfo;
memset(&exifinfo,0,sizeof(EXIFINFO));
Cexif exif(&exifinfo);
bool decode_success = false;
decode_success = exif.DecodeExif(hFile);
fclose(hFile);

if (decode_success && exifinfo.IsExif) {
	CString sDatetime = exifinfo.DateTime;
	if (sDatetime.GetLength() == 19 
		&& _T("    ") != sDatetime.Left(4)
		&& _T("0000") != sDatetime.Left(4)
	) // make sure that full date and time is present "YYYY:MM:DD HH:MM::SS"
	{
		int d_yyyy = StrToInt (sDatetime.Left(4));
		int d_mm   = StrToInt (sDatetime.Mid(4+1,     2));
		int d_dd   = StrToInt (sDatetime.Mid(4+1+2+1, 2));
		int t_hh   = StrToInt (sDatetime.Mid(4+1+2+1+2+1, 2));
		int t_mm   = StrToInt (sDatetime.Mid(4+1+2+1+2+1+2+1, 2));
		int t_ss   = StrToInt (sDatetime.Mid(4+1+2+1+2+1+2+1+2+1, 2));
// here you have the DateTaken value..,
 
Share this answer
 
I wrote an article with the required source code to change the Date Taken attribute.
Date & time batch changer for photos and other files[^]

Sergey Alexandrovich is absolutely right that it will only work on JPEG files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900