65.9K
CodeProject is changing. Read more.
Home

Cexif

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (32 votes)

Mar 16, 2003

1 min read

viewsIcon

198730

downloadIcon

7889

A small class to read EXIF data from JPEG images.

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

  • Cexif::EncodeExif : to append the EXIF data into a standard JPEG image.