void print(const std::string& file) { Exiv2::ExifData ed; int rc = ed.read(file); if (rc) { std::string error = Exiv2::ExifData::strError(rc, file); throw Exiv2::Error(error); } Exiv2::ExifData::const_iterator end = ed.end(); for (Exiv2::ExifData::const_iterator i = ed.begin(); i != end; ++i) { std::cout << std::setw(45) << std::setfill(' ') << std::left << i->key() << " " << "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << i->tag() << " " << std::setw(12) << std::setfill(' ') << std::left << i->ifdName() << " " << std::setw(9) << std::setfill(' ') << std::left << i->typeName() << " " << std::dec << std::setw(3) << std::setfill(' ') << std::right << i->count() << " " << std::dec << i->value() << "\n"; } }
// ***************************************************************************** // Main int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::ExifData exifData; int rc = exifData.read(argv[1]); if (rc) { std::string error = Exiv2::ExifData::strError(rc, argv[1]); throw Exiv2::Error(error); } /* There are two pitfalls that we need to consider when setting the Exif user comment (Exif.Photo.UserComment) of an image: First, the type of the Exif user comment tag is "undefined" (and not ASCII) according to the Exif standard. This means that in Exiv2, we have to deal with a DataValue (and not an AsciiValue). DataValue has the usual two read methods, however, the one taking a const std::string& buf expects the string to contain a series of integers (e.g., "0 1 2") and not a text string. Thus, we need to use the read function that reads the value from a character buffer of a given length. Second, the Exif comment field starts with an eight character area that identifies the used character set. For ASCII, these eight characters are "ASCII\0\0\0". The actual comment follows after this code. Note: There is a more simple Exif tag for the title of an image. It is a 20 byte string (type ASCII) and does not store two-byte characters. (Image.OtherTags.ImageDescription) */ // Initialise a data value with the character set and comment std::string charset("ASCII\0\0\0", 8); std::string comment("A comment added to the Exif metadata through Exiv2"); Exiv2::DataValue value; value.read(reinterpret_cast<const Exiv2::byte*>((charset + comment).data()), 8 + static_cast<long>(comment.size())); // Set the Exif comment Exiv2::ExifKey key("Exif.Photo.UserComment"); Exiv2::ExifData::iterator pos = exifData.findKey(key); if (pos != exifData.end()) { // Use the existing Exif UserComment metadatum if there is one pos->setValue(&value); } else { // Otherwise add a new UserComment metadatum exifData.add(key, &value); pos = exifData.findKey(key); } // Now we should have a valid iterator in any case. We use the metadatum // output operator to print the formatted value std::cout << "Writing user comment '" << *pos << "' back to the image\n"; rc = exifData.write(argv[1]); if (rc) { std::string error = Exiv2::ExifData::strError(rc, argv[1]); throw Exiv2::Error(error); } return rc; } catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
long GalleryUtil::GetNaturalRotation(const QString &filePathString) { long rotateAngle = 0; #ifdef EXIF_SUPPORT QByteArray filePathBA = filePathString.toLocal8Bit(); const char *filePath = filePathBA.constData(); try { char *exifvalue = new char[1024]; ExifData *data = exif_data_new_from_file (filePath); if (data) { for (int i = 0; i < EXIF_IFD_COUNT; i++) { ExifEntry *entry = exif_content_get_entry (data->ifd[i], EXIF_TAG_ORIENTATION); ExifByteOrder byteorder = exif_data_get_byte_order (data); if (entry) { ExifShort v_short = exif_get_short (entry->data, byteorder); VERBOSE(VB_GENERAL|VB_EXTRA, QString("Exif entry=%1").arg(v_short)); /* See http://sylvana.net/jpegcrop/exif_orientation.html*/ if (v_short == 8) { rotateAngle = -90; } else if (v_short == 6) { rotateAngle = 90; } break; } } exif_data_free(data); } else { VERBOSE(VB_FILE, LOC_ERR + QString("Could not load exif data from '%1'") .arg(filePath)); } delete [] exifvalue; #if 0 Exiv2::ExifData exifData; int rc = exifData.read(filePath); if (!rc) { Exiv2::ExifKey key = Exiv2::ExifKey("Exif.Image.Orientation"); Exiv2::ExifData::iterator pos = exifData.findKey(key); if (pos != exifData.end()) { long orientation = pos->toLong(); switch (orientation) { case 6: rotateAngle = 90; break; case 8: rotateAngle = -90; break; default: rotateAngle = 0; break; } } } #endif } catch (...) { VERBOSE(VB_IMPORTANT, LOC_ERR + QString("Failed to extract EXIF headers from '%1'") .arg(filePathString)); } #else // Shut the compiler up about the unused argument (void)filePathString; #endif // EXIF_SUPPORT return rotateAngle; }