Showing posts with label geotag. Show all posts
Showing posts with label geotag. Show all posts

Thursday, June 19, 2008

Geo Tagging images using iphone-exif

The GeoTagging of photos can be really useful. Flickr and Picasa can both use Exif data embedded into an image to generate the Map locations displayed on the site. The only downside is that it is quite tricky to do this directly on the phone.

There are a number of solutions to do this on the PC or by uploading to the site and then tag each image or image folder with  a location, but it is another step when you really want to do it all at once.

I ended up creating a static C/Objective-C library that allows reading/editing and deleting standard Exif tags directly on images on your iPhone. 

This is a quick guide to getting it set up in your project. The Exif spec is a bit tedious and can be found at www.exif.org/Exif2-2.PDF.

Add Into Xcode
Download the library from code.google.com/p/iphone-exif  and unzip into a directory. The zip file contains a static library which you must add into the project as a dependency. then place the header files into your source directory and you are good to go. This is not a dynamic library as the application must ship with the code embedded, it will not be on the iPhone to link with like the framework files you use.

Getting Started
NOTE: The 0.8 version of the library expects a logging variable to be declared - just add it into the AppDelegate .m class like so:

BOOL gLogging = FALSE;


this controls whether internal log statements are echoed to standard out.


The initial task is to parse the Jpeg file data to extrac the Exif data.
This we achieve in the following lines
NSData* theImageData = UIImageRepresentation(anImage,1.0);
EXFJpeg* jpegScanner = [[EXFJpeg alloc] init];
[jpegScanner scanImageData: theImageData];

This will scan the image and extract any Exif data. 

The ExifJpeg class gives us an interface that provides access to the Exif data and the JFIF tags using the properties:

EXFMetaData* exifData = jpegScanner.exifMetaData;
EXFJFIF* jfif = jpegScanner.jfif;

The EXFMetaData class is where most of the work is done. It allows us to 
  • retrieve a tag definition
  • get a list of child tags
  • get a value for a tag
  • remove a tag value
  • add a remove a custom handler
  • get a list of all the tag values.

Retrieving a tag definition
The tag definition details the id, data type, short string name, parent tag, and the number of components (see the Exif Spec for details) it can occupy.

EXFTag* tagDefinition = [exifData tagDefinition: aTagId];

Getting a list of child tags for a parent
NSMutableArray* tagDefinitions = [exifData tagDefinitionsForParent:aTagId];
Getting a value for a tag
The value is an id as it could be a Numeric value, a String, a binary type, or a type provided by a custom tag handler.
id tagValue = [exifData tagValue: aTagId];
Add a tag value
[exifData addTagValue: aValue forKey: aTagId];

Remove a tag value
[exifData removeTagValue:aTagId];
There is obviously a requirement to know what the tagIds are, and these are available in the spec. The EXFConstants.h file also has a defined set of constants to make it easy to see what these are.

Part II
The second post will deal with adding custom handlers and a discussion of the data types, especially around the GPS tag set.