void ExifTransfer::copyMetadata() { try { dst = Exiv2::ImageFactory::open(BasicIo::AutoPtr(new MemIo(data, dataSize))); dst->readMetadata(); } catch (Exiv2::Error & e) { std::cerr << "Exiv2 error: " << e.what() << std::endl; return; } try { src = Exiv2::ImageFactory::open(srcFile.toLocal8Bit().constData()); src->readMetadata(); copyXMP(); copyIPTC(); copyEXIF(); } catch (Exiv2::Error & e) { std::cerr << "Exiv2 error: " << e.what() << std::endl; // At least we have to set the SubImage1 file type to Primary Image dst->exifData()["Exif.SubImage1.NewSubfileType"] = 0; } try { dst->writeMetadata(); FileIo fileIo(dstFile.toLocal8Bit().constData()); fileIo.open("wb"); fileIo.write(dst->io()); fileIo.close(); } catch (Exiv2::Error & e) { std::cerr << "Exiv2 error: " << e.what() << std::endl; } }
// ***************************************************************************** // Main int main(int argc, char* const argv[]) { try { // Handle command line arguments Params params; if (params.getopt(argc, argv)) { params.usage(); return 1; } if (params.help_) { params.help(); return 2; } // Use MemIo to increase test coverage. Exiv2::BasicIo::AutoPtr fileIo(new Exiv2::FileIo(params.read_)); Exiv2::BasicIo::AutoPtr memIo(new Exiv2::MemIo); memIo->transfer(*fileIo); Exiv2::Image::AutoPtr readImg = Exiv2::ImageFactory::open(memIo); assert(readImg.get() != 0); readImg->readMetadata(); Exiv2::Image::AutoPtr writeImg = Exiv2::ImageFactory::open(params.write_); assert(writeImg.get() != 0); if (params.preserve_) writeImg->readMetadata(); if (params.iptc_) { writeImg->setIptcData(readImg->iptcData()); } if (params.exif_) { writeImg->setExifData(readImg->exifData()); } if (params.comment_) { writeImg->setComment(readImg->comment()); } if (params.xmp_) { writeImg->setXmpData(readImg->xmpData()); } try { writeImg->writeMetadata(); } catch (const Exiv2::AnyError&) { std::cerr << params.progname() << ": Could not write metadata to (" << params.write_ << ")\n"; return 8; } return 0; } catch (Exiv2::AnyError& e) { std::cerr << "Caught Exiv2 exception '" << e << "'\n"; return 10; } }
int populateMetaExif(std::string file, std::string plate) { try { Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file); std::string comment = plate + ' '+ "LOT50"; //Add code to get information about lot if(!image.get()) { throw Exiv2::Error(1, "Failed to open image"); } image->readMetadata(); Exiv2::ExifData &exifData = image->exifData(); exifData["Exif.Photo.UserComment"]= comment; //std::cout<< "Writing Exiv User Comment "<< exifData["Exif.Image.ImageDescription"] <<"\n"; image->writeMetadata(); } catch (Exiv2::AnyError& e) { std::cout << "Problem Writing Metadata '" << e << "'\n"; return -1; } return 0; }
Exiv2::ExifData getExifFromPath(char* filename) { qDebug() << "Trying to read EXIF for" << filename; try { Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filename); assert(image.get() != 0); image->readMetadata(); Exiv2::ExifData &exifData = image->exifData(); if (exifData.empty()) { std::string error(filename); error += ": No Exif data found in the file"; //TODO Translate //throw Exiv2::Error(1, error); } return exifData; } catch (Exiv2::Error& e) { Exiv2::ExifData exifData; qCritical() << "Caught Exiv2 exception '" << e.what(); //TODO Translate return exifData; } }
void print(const std::string& file) { Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file); assert(image.get() != 0); image->readMetadata(); Exiv2::ExifData &ed = image->exifData(); 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"; } }
int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]); assert(image.get() != 0); image->readMetadata(); const std::string& xmpPacket = image->xmpPacket(); if (xmpPacket.empty()) { std::string error(argv[1]); error += ": No XMP packet found in the file"; throw Exiv2::Error(1, error); } std::cout << xmpPacket << "\n"; return 0; } catch (Exiv2::AnyError& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
bool ptImageHelper::TransferExif(const QString ASourceFile, const QString ATargetFile) { if (ASourceFile == ATargetFile || ASourceFile.trimmed().isEmpty() || ATargetFile.trimmed().isEmpty()) return false; try { if (Exiv2::ImageFactory::getType(ASourceFile.toLocal8Bit().data()) == Exiv2::ImageType::none) return false; Exiv2::Image::AutoPtr hSourceImage = Exiv2::ImageFactory::open(ASourceFile.toLocal8Bit().data()); if (!hSourceImage.get()) return false; hSourceImage->readMetadata(); Exiv2::Image::AutoPtr hTargetImage = Exiv2::ImageFactory::open(ATargetFile.toLocal8Bit().data()); hTargetImage->clearMetadata(); hTargetImage->setMetadata(*hSourceImage); hTargetImage->writeMetadata(); return true; } catch (Exiv2::AnyError& Error) { if (Settings->GetInt("JobMode") == 0) { ptMessageBox::warning(0 ,"Exiv2 Error","No exif data written!\nCaught Exiv2 exception '" + QString(Error.what()) + "'\n"); } else { std::cout << "Caught Exiv2 exception '" << Error << "'\n"; } } return false; }
int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]); assert(image.get() != 0); image->readMetadata(); Exiv2::XmpData xmpData; Exiv2::copyExifToXmp(image->exifData(), xmpData); Exiv2::ExifData exifData; Exiv2::copyXmpToExif(xmpData, exifData); image->setXmpData(xmpData); image->setExifData(exifData); image->writeMetadata(); return 0; } catch (Exiv2::AnyError& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
/** \fn ImageUtils::GetExifValue(const QString &, const QString &, bool *) * \brief Reads and returns the value of an exif tag in a file * \param fileName The filename that holds the exif data * \param exifTag The key that shall be updated * \param ok Will be set to true if the reading was ok, otherwise false * \return The value of the exif tag or an empty string */ QString ImageUtils::GetExifValue(const QString &fileName, const QString &exifTag, bool *ok) { // Assume the exif reading fails *ok = false; // default value QString value(""); try { Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(fileName.toLocal8Bit().constData()); if (image.get()) { image->readMetadata(); Exiv2::ExifData &exifData = image->exifData(); if (!exifData.empty()) { Exiv2::Exifdatum &datum = exifData[exifTag.toLocal8Bit().constData()]; value = QString::fromStdString(datum.toString()); if (!value.isEmpty()) { *ok = true; } else { LOG(VB_GENERAL, LOG_ERR, QString("Exiv2 error: No tag found, file %1, tag %2") .arg(fileName).arg(exifTag)); } } else { LOG(VB_GENERAL, LOG_ERR, QString("Exiv2 error: No header, file %1, tag %2") .arg(fileName).arg(exifTag)); } } else { LOG(VB_GENERAL, LOG_ERR, QString("Exiv2 error: Could not open file, file %1, tag %2") .arg(fileName).arg(exifTag)); } } catch (Exiv2::Error& e) { LOG(VB_GENERAL, LOG_ERR, QString("Exiv2 exception %1, file %2, tag %3") .arg(e.what()).arg(fileName).arg(exifTag)); } return value; }
int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } const char* path=argv[1]; Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path); assert(image.get() != 0); image->readMetadata(); Jzon::Object root; const char* FS="FS"; Jzon::Object fs ; root.Add (FS,fs) ; fileSystemPush(path,root.Get(FS)); Exiv2::ExifData &exifData = image->exifData(); for ( ExifData::const_iterator i = exifData.begin(); i != exifData.end() ; ++i ) { std::string key ; push(objectForKey(i->key(),key,root),key,i); } Exiv2::IptcData &iptcData = image->iptcData(); for (Exiv2::IptcData::const_iterator i = iptcData.begin(); i != iptcData.end(); ++i) { std::string key ; push(objectForKey(i->key(),key,root),key,i); } Exiv2::XmpData &xmpData = image->xmpData(); for (Exiv2::XmpData::const_iterator i = xmpData.begin(); i != xmpData.end(); ++i) { std::string key ; push(objectForKey(i->key(),key,root),key,i); } /* This is only for testing long paths { ExifData::const_iterator i = exifData.begin(); std::string key; push(objectForKey("This.Is.A.Rather.Long.Path.Key",key,root),key,i); } */ Jzon::Writer writer(root,Jzon::StandardFormat); writer.Write(); std::cout << writer.GetResult() << std::endl; return 0; } //catch (std::exception& e) { //catch (Exiv2::AnyError& e) { catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; return -1; }
int RemoveGPSExif(const char* File, int NoChangeMtime) { struct stat statbuf; struct stat statbuf2; struct utimbuf utb; if (NoChangeMtime) stat(File, &statbuf); // Open the file and start reading. Exiv2::Image::AutoPtr Image; try { Image = Exiv2::ImageFactory::open(File); } catch (Exiv2::Error e) { DEBUGLOG("Failed to open file %s.\n", File); return 0; } Image->readMetadata(); if (Image.get() == NULL) { // It failed if we got here. DEBUGLOG("Failed to read file %s %s.\n", File, Exiv2::strError().c_str()); return 0; } Exiv2::ExifData &ExifInfo = Image->exifData(); if (ExifInfo.count() == 0) { DEBUGLOG("No EXIF tags in file %s.\n", File); return 0; } EraseGpsTags(ExifInfo); try { Image->writeMetadata(); } catch (Exiv2::Error e) { DEBUGLOG("Failed to write to file %s.\n", File); return 0; } if (NoChangeMtime) { stat(File, &statbuf2); utb.actime = statbuf2.st_atime; utb.modtime = statbuf.st_mtime; utime(File, &utb); } return 1; }
void RawParameters::loadCamXyzFromDng() { // Try to load it from the DNG metadata try { const float d65_white[3] = { 0.950456f, 1.0f, 1.088754f }; double cc[4][4], xyz[] = { 1,1,1 }; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { cc[j][i] = i == j ? 1.0 : 0.0; } } Exiv2::Image::AutoPtr src = Exiv2::ImageFactory::open(fileName.toLocal8Bit().constData()); src->readMetadata(); const Exiv2::ExifData & srcExif = src->exifData(); auto ccData = srcExif.findKey(Exiv2::ExifKey("Exif.Image.CameraCalibration1")); if (ccData == srcExif.end()) { ccData = srcExif.findKey(Exiv2::ExifKey("Exif.Image.CameraCalibration2")); } if (ccData != srcExif.end()) { for (int i = 0; i < colors; ++i) { for (int j = 0; j < colors; ++j) { cc[i][j] = ccData->toFloat(i*colors + j); } } } auto xyzData = srcExif.findKey(Exiv2::ExifKey("Exif.Image.AsShotWhiteXY")); if (xyzData != srcExif.end()) { xyz[0] = xyzData->toFloat(0); xyz[1] = xyzData->toFloat(1); xyz[2] = 1.0 - xyz[0] - xyz[1]; for (int i = 0; i < 3; ++i) { xyz[i] /= d65_white[i]; } } auto cmData = srcExif.findKey(Exiv2::ExifKey("Exif.Image.ColorMatrix1")); if (cmData == srcExif.end()) { cmData = srcExif.findKey(Exiv2::ExifKey("Exif.Image.ColorMatrix2")); } if (cmData != srcExif.end() && cmData->count() == 3*colors) { for (int c = 0; c < colors; ++c) { for (int i = 0; i < 3; ++i) { camXyz[c][i] = 0.0; for (int j = 0; j < colors; ++j) { camXyz[c][i] += cc[c][j] * cmData->toFloat(j*3 + i) * xyz[i]; } } } } } catch (Exiv2::Error & e) { Log::debug("Could not load camXyz values from metadata: ", e.what()); } }
void Frame::save(std::string dir) { std::string path = dir + "/" + id; imwrite(path, *img); Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path); image->readMetadata(); Exiv2::ExifData &exifData = image->exifData(); std::stringstream ss; ss << std::setprecision(12) << data.lat << " " << data.lon << " " << data.altitude << " " << data.heading << " " << data.time; exifData["Exif.Photo.UserComment"] = ss.str(); image->writeMetadata(); }
char* ReadExifDate(const char* File, int* IncludesGPS) { // Open and read the file. Exiv2::Image::AutoPtr Image; try { Image = Exiv2::ImageFactory::open(File); } catch (Exiv2::Error e) { DEBUGLOG("Failed to open file %s.\n", File); return NULL; } Image->readMetadata(); if (Image.get() == NULL) { DEBUGLOG("Failed to read file %s %s.\n", File, Exiv2::strError().c_str()); return NULL; } Exiv2::ExifData &ExifRead = Image->exifData(); // Read the tag out. Exiv2::Exifdatum& Tag = ExifRead["Exif.Photo.DateTimeOriginal"]; // Check that the tag is not blank. std::string Value = Tag.toString(); if (Value.length() == 0) { // No date/time stamp. // Not good. // Just return - above us will handle it. return NULL; } // Copy the tag and return that. char* Copy = strdup(Value.c_str()); // Check if we have GPS tags. Exiv2::Exifdatum& GPSData = ExifRead["Exif.GPSInfo.GPSLatitude"]; if (GPSData.count() < 3) { // No valid GPS data. *IncludesGPS = 0; } else { // Seems to include GPS data... *IncludesGPS = 1; } // Now return, passing a pointer to the date string. return Copy; // It's up to the caller to free this. }
// Reads metadata from given file Exiv2::ExifData *FlickrDownload::readExifData(QString file){ try{ Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open( file.toStdString().c_str()); image->readMetadata(); Exiv2::ExifData *data = new Exiv2::ExifData(); *data = image->exifData(); return data; } catch (Exiv2::Error& e) { qCritical("[Filechooser] %s", e.what()); return 0; } }
bool KExiv2::loadFromData(const QByteArray& imgData) const { if (imgData.isEmpty()) return false; try { Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size()); d->filePath.clear(); image->readMetadata(); // Size and mimetype --------------------------------- d->pixelSize = QSize(image->pixelWidth(), image->pixelHeight()); d->mimeType = QString::fromLatin1(image->mimeType().c_str()); // Image comments --------------------------------- d->imageComments() = image->comment(); // Exif metadata ---------------------------------- d->exifMetadata() = image->exifData(); // Iptc metadata ---------------------------------- d->iptcMetadata() = image->iptcData(); #ifdef _XMP_SUPPORT_ // Xmp metadata ----------------------------------- d->xmpMetadata() = image->xmpData(); #endif // _XMP_SUPPORT_ return true; } catch( Exiv2::Error& e ) { d->printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e); } catch(...) { qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2"; } return false; }
NegativeProcessor* NegativeProcessor::createProcessor(AutoPtr<dng_host> &host, const char *filename) { // ----------------------------------------------------------------------------------------- // Open and parse rawfile with libraw... AutoPtr<LibRaw> rawProcessor(new LibRaw()); int ret = rawProcessor->open_file(filename); if (ret != LIBRAW_SUCCESS) { rawProcessor->recycle(); std::stringstream error; error << "LibRaw-error while opening rawFile: " << libraw_strerror(ret); throw std::runtime_error(error.str()); } ret = rawProcessor->unpack(); if (ret != LIBRAW_SUCCESS) { rawProcessor->recycle(); std::stringstream error; error << "LibRaw-error while unpacking rawFile: " << libraw_strerror(ret); throw std::runtime_error(error.str()); } // ----------------------------------------------------------------------------------------- // ...and libexiv2 Exiv2::Image::AutoPtr rawImage; try { rawImage = Exiv2::ImageFactory::open(filename); rawImage->readMetadata(); } catch (Exiv2::Error& e) { std::stringstream error; error << "Exiv2-error while opening/parsing rawFile (code " << e.code() << "): " << e.what(); throw std::runtime_error(error.str()); } // ----------------------------------------------------------------------------------------- // Identify and create correct processor class if (rawProcessor->imgdata.idata.dng_version != 0) { try {return new DNGprocessor(host, rawProcessor.Release(), rawImage);} catch (dng_exception &e) { std::stringstream error; error << "Cannot parse source DNG-file (code " << e.ErrorCode() << ")"; throw std::runtime_error(error.str()); } } else if (!strcmp(rawProcessor->imgdata.idata.model, "ILCE-7")) return new ILCE7processor(host, rawProcessor.Release(), rawImage); else if (!strcmp(rawProcessor->imgdata.idata.make, "FUJIFILM")) return new FujiProcessor(host, rawProcessor.Release(), rawImage); return new VariousVendorProcessor(host, rawProcessor.Release(), rawImage); }
bool ExifHasher::HashExif(const std::string& path, unsigned char* sha1_hash) const { FD fd; sys_call_rv(fd, open, path.c_str(), O_RDONLY); struct stat stat_buf; sys_call(fstat, fd, &stat_buf); void* memblock; sys_call2_rv(NULL, memblock, mmap, NULL, stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); auto bytes = static_cast<const unsigned char*>(memblock); Exiv2::Image::AutoPtr image; try { image = Exiv2::ImageFactory::open(bytes, stat_buf.st_size); } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return false; } if (image.get() != 0) image->readMetadata(); else std::cerr << "Warning: EXIF not found in: " << path << std::endl; sys_call(munmap, memblock, stat_buf.st_size); fd.Close(); if (image.get() == 0) return false; const auto& exifData = image->exifData(); if (!exifData.empty()) { std::ostringstream oss; auto end = exifData.end(); for (auto i = exifData.begin(); i != end; ++i) { // std::cerr << i->key() << " (" << i->count() << ")" << ": " << // i->value() << std::endl; oss << i->key() << i->value(); } const std::string& exif_str = oss.str(); SHA1(reinterpret_cast<const unsigned char*>(exif_str.c_str()), exif_str.size(), sha1_hash); return true; } else { std::cerr << "Warning: EXIF not found in: " << path << std::endl; } return false; }
void ImageView::saveImage() { Exiv2::Image::AutoPtr image; bool exifError = false; if (newImage) { saveImageAs(); return; } setFeedback(tr("Saving...")); try { image = Exiv2::ImageFactory::open(currentImageFullPath.toStdString()); image->readMetadata(); } catch (Exiv2::Error &error) { exifError = true; } QImageReader imgReader(currentImageFullPath); if (!displayPixmap.save(currentImageFullPath, imgReader.format().toUpper(), GData::defaultSaveQuality)) { QMessageBox msgBox; msgBox.critical(this, tr("Error"), tr("Failed to save image.")); return; } if (!exifError) { try { image->writeMetadata(); } catch (Exiv2::Error &error) { QMessageBox msgBox; msgBox.critical(this, tr("Error"), tr("Failed to save Exif metadata.")); } } reload(); setFeedback(tr("Image saved.")); }
// uses the exiv2 class to readout the exifdata from images int ExifScout::showExifData(std::string a){ try { Ui_MainWindow::textEdit->clear(); std::cerr << "ImageFactory::open "; Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(a); std::cerr << "DONE\n"; assert(image.get() != 0); std::cerr << "image->readMetadata() "; image->readMetadata(); std::cerr << "DONE\n"; std::cerr << "Exiv2::ExifData& exifData = image->exifData() "; Exiv2::ExifData exifData = image->exifData(); std::cerr << "DONE\n"; cout << "Number of found exiv2 data: " << exifData.count() << endl; if (exifData.empty()) { Ui_MainWindow::textEdit->setPlainText("No Exif data found in file"); return 1; } Exiv2::ExifData::const_iterator end = exifData.end(); std::cerr << "Generate the exif-output "; QString output; for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) { std::string tagname = i->tagName(); if(tagname != std::string("MakerNote") and tagname.substr(0,2) != "0x"){ // Print out the exif-data in the QtextEdit Field std::ostringstream oss; oss << i->tagName() << ":\n " << i->value() << "\n"; output = output+(QString)oss.str().c_str(); } } std::cerr << "Done\n"; Ui_MainWindow::textEdit->setPlainText(output); return 0; } //catch (std::exception& e) { //catch (Exiv2::AnyError& e) { catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; return -1; } }
int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } std::string file(argv[1]); Exiv2::IptcData iptcData; iptcData["Iptc.Application2.Headline"] = "The headline I am"; iptcData["Iptc.Application2.Keywords"] = "Yet another keyword"; iptcData["Iptc.Application2.DateCreated"] = "2004-8-3"; iptcData["Iptc.Application2.Urgency"] = uint16_t(1); iptcData["Iptc.Envelope.ModelVersion"] = 42; iptcData["Iptc.Envelope.TimeSent"] = "14:41:0-05:00"; iptcData["Iptc.Application2.RasterizedCaption"] = "230 42 34 2 90 84 23 146"; iptcData["Iptc.0x0009.0x0001"] = "Who am I?"; Exiv2::StringValue value; value.read("very!"); iptcData["Iptc.Application2.Urgency"] = value; std::cout << "Time sent: " << iptcData["Iptc.Envelope.TimeSent"] << "\n"; // Open image file Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file); if (image.get() == 0) { std::string error(file); error += " : Could not read file or unknown image type"; throw Exiv2::Error(error); } // Read existing metdata (so that exif will be preserved) int rc = image->readMetadata(); if (rc) { std::string error = Exiv2::Image::strError(rc, file); throw Exiv2::Error(error); } // Replace Iptc data and write it back to the file image->setIptcData(iptcData); return image->writeMetadata(); } catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
void ThumbView::updateExifInfo(QString imageFullPath) { Exiv2::Image::AutoPtr exifImage; QString key; QString val; try { exifImage = Exiv2::ImageFactory::open(imageFullPath.toStdString()); exifImage->readMetadata(); } catch (Exiv2::Error &error) { return; } Exiv2::ExifData &exifData = exifImage->exifData(); if (!exifData.empty()) { Exiv2::ExifData::const_iterator end = exifData.end(); infoView->addTitleEntry("Exif"); for (Exiv2::ExifData::const_iterator md = exifData.begin(); md != end; ++md) { key = QString::fromUtf8(md->tagName().c_str()); val = QString::fromUtf8(md->print().c_str()); infoView->addEntry(key, val); } } Exiv2::IptcData &iptcData = exifImage->iptcData(); if (!iptcData.empty()) { Exiv2::IptcData::iterator end = iptcData.end(); infoView->addTitleEntry("IPTC"); for (Exiv2::IptcData::iterator md = iptcData.begin(); md != end; ++md) { key = QString::fromUtf8(md->tagName().c_str()); val = QString::fromUtf8(md->print().c_str()); infoView->addEntry(key, val); } } Exiv2::XmpData &xmpData = exifImage->xmpData(); if (!xmpData.empty()) { Exiv2::XmpData::iterator end = xmpData.end(); infoView->addTitleEntry("XMP"); for (Exiv2::XmpData::iterator md = xmpData.begin(); md != end; ++md) { key = QString::fromUtf8(md->tagName().c_str()); val = QString::fromUtf8(md->print().c_str()); infoView->addEntry(key, val); } } }
// ***************************************************************************** // Main int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]); assert (image.get() != 0); image->readMetadata(); Exiv2::ExifData &exifData = image->exifData(); /* Exiv2 uses a CommentValue for Exif user comments. The format of the comment string includes an optional charset specification at the beginning: [charset=["]Ascii|Jis|Unicode|Undefined["] ]comment Undefined is used as a default if the comment doesn't start with a charset definition. Following are a few examples of valid comments. The last one is written to the file. */ exifData["Exif.Photo.UserComment"] = "charset=\"Unicode\" An Unicode Exif comment added with Exiv2"; exifData["Exif.Photo.UserComment"] = "charset=\"Undefined\" An undefined Exif comment added with Exiv2"; exifData["Exif.Photo.UserComment"] = "Another undefined Exif comment added with Exiv2"; exifData["Exif.Photo.UserComment"] = "charset=Ascii An ASCII Exif comment added with Exiv2"; std::cout << "Writing user comment '" << exifData["Exif.Photo.UserComment"] << "' back to the image\n"; image->writeMetadata(); return 0; } catch (Exiv2::AnyError& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
void ImageView::saveImageAs() { Exiv2::Image::AutoPtr exifImage; Exiv2::Image::AutoPtr newExifImage; bool exifError = false; setCursorHiding(false); QString fileName = QFileDialog::getSaveFileName(this, tr("Save image as"), currentImageFullPath, tr("Images") + " (*.jpg *.jpeg *.jpe *.png *.bmp *.ppm *.pgm *.pbm *.xbm *.xpm)"); if (!fileName.isEmpty()) { try { exifImage = Exiv2::ImageFactory::open(currentImageFullPath.toStdString()); exifImage->readMetadata(); } catch (Exiv2::Error &error) { exifError = true; } if (!displayPixmap.save(fileName, 0, GData::defaultSaveQuality)) { QMessageBox msgBox; msgBox.critical(this, tr("Error"), tr("Failed to save image.")); } else { if (!exifError) { try { newExifImage = Exiv2::ImageFactory::open(fileName.toStdString()); newExifImage->setMetadata(*exifImage); newExifImage->writeMetadata(); } catch (Exiv2::Error &error) { exifError = true; } } setFeedback(tr("Image saved.")); } } if (mainWindow->isFullScreen()) { setCursorHiding(true); } }
int ImgTagJson::literal() { Exiv2::Image::AutoPtr image; try { image = Exiv2::ImageFactory::open(fname); } catch (Exiv2::Error& e) { cerr << "Invalid file: " << fname << endl; return 6; } if (image.get() == 0) return 1; image->readMetadata(); JSONNODE *l = json_new(JSON_NODE); if (exif) { JSONNODE *e = this->genLitExif(image); if (e != NULL) json_push_back(l, e); } if (iptc) { JSONNODE *i = this->genLitIptc(image); if (i != NULL) json_push_back(l, i); } if (xmp) { JSONNODE *x = this->genLitXmp(image); if (x != NULL) json_push_back(l, x); } JSONNODE *chksum = getChkSum(); if (chksum != NULL) json_push_back(l, chksum); cout << json_write_formatted(l) << endl; json_delete(l); return 0; }
void ExifTools::copyExif(const QString &sourceStr, const QString &destStr) { Exiv2::Image::AutoPtr sourceImageData = Exiv2::ImageFactory::open(QFile::encodeName(sourceStr).data()); sourceImageData->readMetadata(); Exiv2::ExifData exifData = sourceImageData->exifData(); Exiv2::IptcData iptcData = sourceImageData->iptcData(); Exiv2::ExifThumb exifThumb(exifData); exifThumb.erase(); Exiv2::Image::AutoPtr destImageData = Exiv2::ImageFactory::open(QFile::encodeName(destStr).data()); destImageData->setExifData(exifData); destImageData->setIptcData(iptcData); destImageData->writeMetadata(); }
bool ImageTags::writeTagsToImage(QString &imageFileName, QSet<QString> &newTags) { QSet<QString> imageTags; Exiv2::Image::AutoPtr exifImage; try { exifImage = Exiv2::ImageFactory::open(imageFileName.toStdString()); exifImage->readMetadata(); Exiv2::IptcData newIptcData; /* copy existing data */ Exiv2::IptcData &iptcData = exifImage->iptcData(); if (!iptcData.empty()) { QString key; Exiv2::IptcData::iterator end = iptcData.end(); for (Exiv2::IptcData::iterator iptcIt = iptcData.begin(); iptcIt != end; ++iptcIt) { if (iptcIt->tagName() != "Keywords") { newIptcData.add(*iptcIt); } } } /* add new tags */ QSetIterator<QString> newTagsIt(newTags); while (newTagsIt.hasNext()) { QString tag = newTagsIt.next(); Exiv2::Value::AutoPtr value = Exiv2::Value::create(Exiv2::string); value->read(tag.toStdString()); Exiv2::IptcKey key("Iptc.Application2.Keywords"); newIptcData.add(key, value.get()); } exifImage->setIptcData(newIptcData); exifImage->writeMetadata(); } catch (Exiv2::Error &error) { QMessageBox msgBox; msgBox.critical(this, tr("Error"), tr("Failed to save tags to ") + imageFileName); return false; } return true; }
// Returns empty string if the destination subdirectory could not be determined // for the supplied source file. std::string build_dest(const fs::path &source_file) { std::string dest; Exiv2::Image::AutoPtr image; try { image = Exiv2::ImageFactory::open(source_file.string()); image->readMetadata(); } catch(const Exiv2::AnyError&) { // No metadata, let things continue to try file info } std::vector<PathPart>::iterator iter = g_path_parts.begin(); std::vector<PathPart>::iterator end = g_path_parts.end(); for( ; iter != end; ++iter) { dest += iter->pre; std::string result; const Pattern *pat = iter->pat; for(unsigned fx = 0; fx < g_run_order.size(); ++fx) { if(g_run_order[fx] != -1 && pat->funcs[g_run_order[fx]]) { if(g_run_order[fx] == FILE_SLOT) { // Always run file operations result = pat->funcs[g_run_order[fx]](image.get(), source_file); } else if(image.get()) { // No point in running metadata operations without an image result = pat->funcs[g_run_order[fx]](image.get(), source_file); } if(result.length()) break; } } // If we found no data, even for part of pattern, give up and // return no destination if(!result.length()) return result; dest += (result + iter->post); } return dest; }
static gfloat expcombine_get_file_ev (const gchar *path) { /* Open the file and read in the metadata */ Exiv2::Image::AutoPtr image; try { image = Exiv2::ImageFactory::open (path); image->readMetadata (); } catch (Exiv2::Error ex) { g_print ("Error: unable to read metadata from path: '%s'\n", path); exit (EXIT_FAILURE); } Exiv2::ExifData &exifData = image->exifData (); if (exifData.empty ()) return NAN; /* Calculate the APEX brightness / EV */ gfloat time, aperture, gain = 1.0f; time = exifData["Exif.Photo.ExposureTime"].value().toFloat(); aperture = exifData["Exif.Photo.FNumber" ].value().toFloat(); /* iso */ try { gain = exifData["Exif.Photo.ISOSpeedRatings"].value().toLong() / 100.0f; } catch (Exiv2::Error ex) { // Assume ISO is set at 100. It's reasonably likely that the ISO is the // same across all images anyway, and for our purposes the relative // values can be sufficient. gain = 1.0f; } return log2f (aperture * aperture) + log2f (1 / time) + log2f (gain); }
// tries to find a specified exif tag in a specified file QString ExifScout::getExifData(QString fname, QString etag){ std::string filename = fname.toAscii().data(); std::string exiftag = etag.toAscii().data(); Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filename); assert(image.get() != 0); image->readMetadata(); Exiv2::ExifData exifData = image->exifData(); if (!exifData.empty()) { Exiv2::ExifData::const_iterator end = exifData.end(); QString output; for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) { if(i->tagName()==exiftag){ std::ostringstream oss; oss << i->value(); return (QString)oss.str().c_str(); } } } return NULL; }