views:

120

answers:

4

Is there some cheap and reliable way to detect if an image file has EXIF data? Something like "read the first 100 bytes and search for EXIF substring" is highly preferable. I don't need to read and parse it - only to know if it is there.

The key is that it must be very fast. C++ is preferable.

A: 

You could read the source of the PHP EXIF extension - by looking at how the exif_read_data is implemented, you might pick up some clues.

Paul Dixon
+1  A: 

You might look at the implementation of file (1).

You could just call it, of course, but you presumably don't need the rest of files functionality so...

dmckee
A: 

If you do not require massive performance, I would use some Exif library and let it try to get the Exif data (if present) for you. (pyexif, perl Image::exif, c# MetaDataExtractor etc)

Otherwise,

take a look at http://en.wikipedia.org/wiki/JPEG#Syntax%5Fand%5Fstructure

You need to create a simple binary parser do dig out the "segment codes", and to find the segment called APP1 (if I understand it correctly). The data should contain the letters "Exif"

e.g. in a random JPEG file on my PC, the bytes 7-10 said "Exif". I do not know if the location is the same in all JPEG files. The segments may be of variable length.

goorj
+1  A: 

You only need to check first 4 bytes of the stream:

bool IsExifStream(const char* pJpegBuffer)
{
    static const char stream_prefix1[] = "\xff\xd8\xff\xe1";
    return memcmp(pJpegBuffer, stream_prefix1, 4) == 0;
}
Alex Cohn
The problem is I don't want to parse the file to find where the stream starts. I don't care of the stream, I work with a file.
sharptooth