October 16th 2014, Exiv2
← October 15th 2014 Android Storage | ● | October 18th 2014 GPS Altitude Correction →
Currently, the base Qt package does not support EXIF tags. The “Qt Extended” project used to provide a QExifImageHeader class, but the actual status is that it has been discontinued by Qt in 2009. Although it is still developed as a fork under “Qt Extended Improved” I doubt that it will be 100% compatible with the upcoming Qt 5.4 release, so I am shying away from backporting it.
Instead let’s have a look at the Exiv2 library: It is a GPLed C++ library with CMake support, which adds support for EXIF tags in all major image formats.
Installation
To compile, first let’s install the development packages libjpeg-dev, libpng-dev, libtiff-dev and libexpat1-dev. Then we check out Exiv2 via subversion and compile it CMake-style:
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libexpat1-dev svn checkout svn://dev.exiv2.org/svn/trunk libexiv2 cd libexiv2 cmake . make sudo make install
Usage Example
We simply would like to add a GPSAltitude tag to a JPEG file. According to the tag reference documentation, the tag’s value is a rational value (a divided by b) interpreted as meters (above sea level):
#include <exiv2/image.hpp>
int main()
{
std::string file("MtEverest.jpg");
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
if (image.get())
{
// load EXIF data from image file
image->readMetadata();
Exiv2::ExifData exifData = image->exifData();
// add a rational value (a divided by b)
exifData["Exif.GPSInfo.GPSAltitude"] = Exiv2::Rational(884443, 100); // alt = 8844.43 meter
exifData["Exif.GPSInfo.GPSAltitudeRef"] = int8_t(0); // m.a.s.l.
// write EXIF data to image file
image->setExifData(exifData);
image->writeMetadata();
return(0);
}
return(1);
}
To print and check the EXIF tags of an image file, we use the exiv2 tool:
exiv2 -PE MtEverest.jpg
Output:
Exif.Image.GPSTag Long 1 26 Exif.GPSInfo.GPSAltitudeRef SLong 1 Above sea level Exif.GPSInfo.GPSAltitude SRational 1 8844.4 m
Here is the geo-tagged image:
← October 15th 2014 Android Storage | ● | October 18th 2014 GPS Altitude Correction →