sharptooth
sharptooth

Reputation: 170499

Is there some easy way to detect if a file has EXIF data?

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.

Upvotes: 2

Views: 1111

Answers (4)

Alex Cohn
Alex Cohn

Reputation: 57173

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;
}

Upvotes: 1

goorj
goorj

Reputation: 443

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_and_structure

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.

Upvotes: 0

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...

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300855

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.

Upvotes: 0

Related Questions