Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - cexif.png

Introduction

Cexif is a small class to read the EXIF data stored in JPEG images, normally generated by digital cameras.

The code is based on Jhead, written by Matthias Wandel. Jhead offers a lot of switches to parse an image with EXIF tags, but it's plain C. I simply rearranged the functions and the global variables into a simple class.

Another useful article on this topic is here [^], with a bit of details on the EXIF structure. The official document for the "Digital Still Camera Image File Format Standard (Exchangeable image file format for Digital Still Cameras: Exif) Version 2.1" can be downloaded from http://www.bbs-informatique.fr/pdf/photo/Exif_2-1.pdf [^].

You can find some EXIF images at http://www.exif.org/samples.html

Using the code

You can use this class in 2 ways. The simplest is:

Cexif exif;
exif.DecodeExif(hFile);

where hFile is a valid file handle. If the file contains EXIF data, exif.m_exifinfo->IsExif is true, and you can read the fields in exif.m_exifinfo to create a report. The second way is:

EXIFINFO m_exifinfo;
memset(&m_exifinfo,0,sizeof(EXIFINFO));
Cexif exif(&m_exifinfo);
exif.DecodeExif(hFile);

In this case the result is stored in the variable m_exifinfo, and you can delete the exif object without losing the EXIF data.

More EXIF tags

typedef struct tag_ExifInfo {
    char  Version      [5];
    char  CameraMake   [32];
    char  CameraModel  [40];
    char  DateTime     [20];
    int   Height, Width;
    int   Orientation;
    int   IsColor;
    int   Process;
    int   FlashUsed;
    float FocalLength;
    float ExposureTime;
    float ApertureFNumber;
    float Distance;
    float CCDWidth;
    float ExposureBias;
    int   Whitebalance;
    int   MeteringMode;
    int   ExposureProgram;
    int   ISOequivalent;
    int   CompressionLevel;
    float FocalplaneXRes;
    float FocalplaneYRes;
    float FocalplaneUnits;
    float Xresolution;
    float Yresolution;
    float ResolutionUnit;
    float Brightness;
    char  Comments[MAX_COMMENT];
    unsigned char * ThumbnailPointer;
    unsigned ThumbnailSize;
    bool  IsExif;
} EXIFINFO;

If needed, the EXIFINFO structure can be extended to read more tags.

  1. Declare a new field in the EXIFINFO structure (example: float ExposureTime;)
  2. Define a new tag in exif.cpp (example: #define TAG_EXPOSURETIME 0x829A).
  3. Add a new case in the ProcessExifDir function.

Example:

case TAG_EXPOSURETIME:
m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
break;

ConvertAnyFormat converts automatically the value stored in the tag.

To Do

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralError fix
yury_i
5:49 21 Jan '10  
Thank you for great job, but several points have to be corrected:

//Exif.cpp
235:
int FirstOffset = Get32u(CharBuf+10); //it's OK
//but
247:
if (!ProcessExifDir(CharBuf+14/*!!*/, CharBuf+6, length-6,m_exifinfo,&LastExifRefd)) //error

You should use calculated offset here. Also I have jpegs taken with Cannon EOS 450D camera which has offset more than 9000 bytes. So I think that better way is:

235:
if (FirstOffset < 8 || FirstOffset > 16){
strcpy(m_szLastError,"Suspicious offset of first IFD value");
//return 0; comment this!
}

247:
if (!ProcessExifDir(CharBuf+6+FirstOffset, CharBuf+6, length-6, m_exifinfo, &LastExifRefd))

__________
Y.

GeneralMy vote of 1
arkilisx
5:48 19 Oct '09  
i do not how to say, but it is boring for its hard description in the code. No logical!
GeneralMaking the demo work on Visual Studio 2008 under Vista
CodeGibbon
12:28 30 Nov '08  
Change the calls on exif.cpp where it says log(2) to log(2.0f). That'll get rid of ambiguous command.

And change the line s+=c; on ExifTestDlg.cpp line 307 to s.AppendChar(c);
GeneralDemo program doesn't read EXIF data from Pentax K100D
Nico Cuppen
6:33 14 Oct '08  
The class somehow doesn't know how to read the EXIF data from photos made with my Pentax K100D. I can mail you a photo, if you tell me how.
GeneralRe: Demo program doesn't read EXIF data from Pentax K100D
Davide Pizzolato
13:46 14 Oct '08  
I should update the article... the version implemented in CxImage (http://www.codeproject.com/KB/graphics/cximage.aspx[^]) should be ok, but the source is a bit different.
GeneralEXIF GPS tags
andycarr1
1:32 9 Jan '08  
Very useful library, will you be extending it to support GPS tags - part of the EXIF standard, which are stored in a separate IFD within the EXIF information?
Generalcomments in JPEG files
Martin0815
3:26 31 May '07  
Hello,

using Cexif I realized it is only possible to read comments from JPEG files if they contain EXIF information.

But a JPEG file containing only comments fails.

I just wanted to test, if Cexif is able to read more than one comment chunk from a JPEG file, so that it is possible to extract all probably existing comment chunks.

Ok - Perhabs, 4 years after the last article update, somebody can help a bit!

Best regards,

Martin
GeneralIMC INFO
AndE00
5:42 26 Feb '05  
Does anyone know how to properly embed the ICM tag in the JPG's EXIF info? I don't have .Net and find only .Net solutions to decoding the Tag. The tag PropertyTagICCProfile, I presume is the one to fill but I cant figure out in c++ how to decode this byte array on my visual studio 6. I noticed custom tags try to fill this value but Adobe1998.icc wont. It seems to be the only logical tag to embed the color profile name. Any suggestions are appreciated.

Thanks

zBz
GeneralOrientation
TeaShirt
16:43 26 Aug '04  
With all due respect to God (David) In the example in ShowInfo change the following line

t.Format("Orientation : %s\n", m_exifinfo.Orientation);
to

t.Format("Orientation : %d\n", m_exifinfo.Orientation);

Probably you did not notice that because cameras with orientation detection are rather newerer

Peace In and Out
yalcin
GeneralRe: Orientation Text Descriptions
jmeaux
9:23 14 Aug '05  
I came across the same bug and have found text descriptions of the Orientation value, here:
http://sylvana.net/jpegcrop/exif_orientation.html

Here's the jist of it:
2) transform="-flip horizontal";
3) transform="-rotate 180";
4) transform="-flip vertical";
5) transform="-transpose";
6) transform="-rotate 90";
7) transform="-transverse";
8) transform="-rotate 270";

Hope this helps others,
Jared

GeneralHow to make a non-Exif jpg an Exif jpg?
cjjj
22:22 11 Nov '03  

What i want to do is to convert a "non-Exif" jpg file to an "Exif" one and

have it's thumbnail inside it's Exif data.

Can any body know how to do it? Cry

Thanks for any help. Big Grin
GeneralRe: How to make a non-Exif jpg an Exif jpg?
cjjj
1:29 20 Nov '03  
I've done it myself using GDI+ API. Laugh
GeneralCan't Read Fujifilm EXIF
Richard Collins
4:41 21 Oct '03  
Hi. The Cexif class (16/Mar/2003) works fine on Olympus .jpg files but not on those from a Fuji FinePix camera. The format in the Fuji file is Exif II - I guess it should work. The smallest Fuji file I have is 500 KB - I can send if you want it. Thanks.

RC
GeneralRe: Can't Read Fujifilm EXIF
Davide Pizzolato
7:03 25 Oct '03  
I have a Fuji Finepix, see the image in the artice, with exif version 2.1 Confused
If it's a nice photo, send it
GeneralCexif::EncodeExif
David Horner
19:37 18 Aug '03  
Have you produced any code to store Exif tags on an existing jpeg?
I would like to use this function and will develop it if I HAVE to..
But I wanted to make sure I wasn't going to reinvent the wheel.

Thanks for you great work!
GeneralRe: Cexif::EncodeExif
Davide Pizzolato
22:08 18 Aug '03  
in the original Jhead source code there are the seeds for this function.
GeneralExport Exif data
BengCésar
3:01 9 Jul '03  
I want to export Exif data from a file A to a file B

I read my files until I find M_SOS (0xDA Start Of Scan : begins compressed data)
and I replace the head's file B by the head's file A

But it doesn't work : the file B is Dead

Is it a wrong way ?

--------
Excuse my english, I'm antartician
GeneralRe: Export Exif data
Davide Pizzolato
8:58 14 Jul '03  
BengCésar wrote: Is it a wrong way ?
yes, because you replace also the header of the image, with its information and pointers. If I'm not wrong, JHead has an option to write the Exif data into an existing Jpeg.
GeneralI can't read the exif format of my photo file
Rousset de Pina
0:39 23 Mar '03  
:( I try on three photo file and no one can be read the exif part of the file, the error is on length on two char who is read on begining. As i don't know the right way I can't do anything else. My camera is Canon.
Best regards
GeneralRe: I can't read the exif format of my photo file
Davide Pizzolato
0:45 23 Mar '03  
can you share some photo?
GeneralRe: I can't read the exif format of my photo file
Rousset de Pina
1:30 23 Mar '03  
Smile Sorry it is a bug of my part, it is because I don't open the file in binary mode, think you very much for your answer.Blush
Best regards


Last Updated 16 Mar 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010