From 4e8fb09e98021d61cc4c66c99c824595e2d729b7 Mon Sep 17 00:00:00 2001 From: Konrad Zdunczyk Date: Wed, 14 Jan 2015 14:46:51 +0100 Subject: [PATCH] [Exif] getThumbnail implementation Change-Id: Ie1ba606a5b35854b987bb2efddeb08fc7aaef57d Signed-off-by: Konrad Zdunczyk --- src/exif/exif_api.js | 12 ++++++- src/exif/exif_information.cc | 4 +++ src/exif/exif_instance.cc | 68 +++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/exif/exif_api.js b/src/exif/exif_api.js index e36058be..e890c49b 100644 --- a/src/exif/exif_api.js +++ b/src/exif/exif_api.js @@ -180,7 +180,17 @@ ExifManager.prototype.getThumbnail = function() { } ]); - throw 'Not implemented'; + var callback = function(result) { + if (native_.isFailure(result)) { + native_.callIfPossible(args.errorCallback, + native_.getErrorObject(result)); + } else { + var thumb = native_.getResultObject(result); + args.successCallback(thumb.src); + } + }; + + native_.call('Exif_getThumbnail', {'uri': args.uri}, callback); }; // this function passes ExifInformation_exposureProgram_attribute test: diff --git a/src/exif/exif_information.cc b/src/exif/exif_information.cc index 1badf3f6..b1aba683 100644 --- a/src/exif/exif_information.cc +++ b/src/exif/exif_information.cc @@ -764,6 +764,8 @@ void ExifInformation::processEntry(ExifEntry* entry, ExifData* exif_data) { } } else { LoggerE("iso speed ratings: format or components count is invalid!"); + throw common::TypeMismatchException("iso speed ratings: format or" + " components count is invalid!"); } break; } @@ -787,6 +789,8 @@ void ExifInformation::processEntry(ExifEntry* entry, ExifData* exif_data) { } } else { LoggerE("exposure time: format or components count is invalid!"); + throw common::TypeMismatchException("exposure time: format or" + " components count is invalid!"); } break; } diff --git a/src/exif/exif_instance.cc b/src/exif/exif_instance.cc index 534061d9..9d5720ca 100644 --- a/src/exif/exif_instance.cc +++ b/src/exif/exif_instance.cc @@ -12,6 +12,7 @@ #include #include +#include "common/platform_exception.h" #include "common/task-queue.h" #include "common/logger.h" @@ -140,6 +141,7 @@ void ExifInstance::getExifInfo(const picojson::value& args, void ExifInstance::saveExifInfo(const picojson::value& args, picojson::object& out) { + LoggerD("Entered"); const std::string& uri = args.get("uri").get(); const double callback_id = args.get("callbackId").get(); @@ -171,7 +173,71 @@ void ExifInstance::saveExifInfo(const picojson::value& args, void ExifInstance::getThumbnail(const picojson::value& args, picojson::object& out) { - LoggerE("getThumbnail is not implemented (c++)"); + LoggerD("Entered"); + const std::string& uri = args.get("uri").get(); + + const double callback_id = args.get("callbackId").get(); + auto get = [=](const std::shared_ptr& response) -> void { + try { + const std::string file_path = ExifUtil::convertUriToPath(uri); + ExifData* exif_data = nullptr; + JsonValue result = JsonValue(JsonObject());; + JsonObject& result_obj = result.get(); + + std::string ext = file_path.substr(file_path.find_last_of(".") + 1); + std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); + + if ("jpg" == ext) { + ext = "jpeg"; + } + + if ("jpeg" == ext || "png" == ext || "gif" == ext) { + LoggerD("Get thumbnail from Exif in file: [%s]", file_path.c_str()); + + exif_data = exif_data_new_from_file(file_path.c_str()); + if (!exif_data) { + throw common::UnknownException("File can not be loaded"); + } + + if (exif_data->data && exif_data->size) { + gchar* ch_uri = g_base64_encode(exif_data->data, exif_data->size); + std::string base64 = "data:image/"+ext+";base64," + ch_uri; + + exif_data_unref(exif_data); + + std::pair pair; + pair = std::make_pair("src", base64); + result_obj.insert(pair); + } else { + exif_data_unref(exif_data); + LoggerE("File [%s] doesn't contain thumbnail", file_path.c_str()); + throw common::InvalidValuesException("File doesn't contain" + " thumbnail"); + } + } else { + LoggerE("extension: %s is not valid (jpeg/jpg/png/gif is supported)", + ext.c_str()); + throw common::NotSupportedException("getThumbnail support only" + " jpeg/jpg/png/gif"); + } + + ReportSuccess(result, response->get()); + } catch (const common::PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = + [callback_id, this](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + LoggerD("callback is %s", response->serialize().c_str()); + PostMessage(response->serialize().c_str()); + }; + + common::TaskQueue::GetInstance().Queue( + get, get_response, + std::shared_ptr(new JsonValue(JsonObject()))); } } // namespace exif -- 2.34.1