From 889c58413b613d234e880007966fc38b262d77e2 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 13 Mar 2023 16:30:13 -0400 Subject: partial-fix: images not corrupted (but still not displaying with correct orientation) --- lib/exif_wrapper.cpp | 30 +++++++++++++++++++++++++++++- lib/exif_wrapper.h | 3 ++- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/exif_wrapper.cpp b/lib/exif_wrapper.cpp index eab5598..21ee6c6 100644 --- a/lib/exif_wrapper.cpp +++ b/lib/exif_wrapper.cpp @@ -35,10 +35,38 @@ extern "C" unsigned int getExifOrientation(uint8_t* imgBytes, unsigned int size) } } + +extern "C" void setExifOrientation(char* fileName, unsigned int orientation) { + try { + // Exiv init + Exiv2::XmpParser::initialize(); + ::atexit(Exiv2::XmpParser::terminate); + Exiv2::enableBMFF(); + + // using blob works, except that exiv2 won't write back to the blob :( + // std::unique_ptr image = Exiv2::ImageFactory::open(imgBytes, size); + // So instead we load from the temp file + std::unique_ptr image = Exiv2::ImageFactory::open(fileName); + assert(image.get() != 0); + image->readMetadata(); + Exiv2::ExifData &exifData = image->exifData(); + + exifData[ORIENTATION_KEY] = orientation; + + // Save exif to image object & save image back to file + image->setExifData(exifData); + image->writeMetadata(); + } + catch (Exiv2::Error& e) { + std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; + } +} +/* extern "C" void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation) { try { // Hack to work around Exiv2 seeming to refuse to write back to blob, but will write // back to a file. Create temp file -> operate on it -> copy back to blob, unlink file. + // tmpFileName gets overwritten with the actual filename by mkstemp char tmpFileName[] = "/tmp/image-temp.XXXXXX"; int fd = mkstemp(tmpFileName); assert(fd != -1); @@ -74,4 +102,4 @@ extern "C" void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigne catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; } -} +}*/ diff --git a/lib/exif_wrapper.h b/lib/exif_wrapper.h index ec51406..ab649c8 100644 --- a/lib/exif_wrapper.h +++ b/lib/exif_wrapper.h @@ -1,4 +1,5 @@ #include unsigned int getExifOrientation(uint8_t* imgBytes, unsigned int size); -void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation); +//void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation); +void setExifOrientation(char* fileName, unsigned int orientation); -- cgit v1.2.3