Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Exif GPS info using Delphi

I need to get geolocation info from my photos. Lat/Lon and GPSVersion. I've already found some info related to this question, I compared different EXIF headers and found a hexadecimal dump that gives me coordinates - now I need to get it from the file.

The question might seem very simple. How do I open a JPEG-file in Delphi to get necessary hexadecimal dumps?

Already tried to read Chars and Integers, but nothing worked. I would like not to use any external libraries for this task if possible.

This is basically my major question, but I'll be extremely happy if anyone could answer one more.

Is there an easy way to search GPS tags without searching the file for specific dumps? Now I'm looking for a strange combination 12 00 02 00 07 00, which really works. I've read EXIF documentation but I couldn't really understand the thing with GPS Tags.

like image 544
user1131662 Avatar asked Mar 23 '23 13:03

user1131662


1 Answers

If you require no external libraries, you can do this with TFileStream and an array of byte. I've done this in a project to obtain the 'picture taken date', the GPS lat-long coordinates are just another field in the EXIF header. I don't have the code here but the method is straight-forward: once you have a TFileStream to the JPEG file:

  • Read the first 2 bytes, check it is in fact $FF $D8 (just to be sure it's a valid JPEG)
  • Read the next 2 bytes, check if it's $FF $E1
    • if it's not, depending on which segment it is, read two more bytes (or a word) and skip that many bytes (by calling the stream's Seek method), there's a list of segments here: https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure; then repeat
  • If it is, read 4 bytes and see if it's 'Exif' ($45 $78 $69 $66)
  • What follows is $00 $00 and a 8-byte TIFF header which holds general information like endianness, followed by the EXIF tags you need to work through and grab the ones you need, I had a quick search and found a list here: http://www.exiv2.org/tags.html

Since it's safe to assume that the EXIF data is in the first kilobytes of the JPEG file, you could read this much in a byte array (or TMemoryStream) and process the data there, which should perform better than separate small reads from a TFileStream.

like image 122
Stijn Sanders Avatar answered Apr 01 '23 08:04

Stijn Sanders