From a25d09cca650930279c4265d44af93dbc752cd32 Mon Sep 17 00:00:00 2001 From: gudehe Date: Wed, 10 May 2023 11:47:15 +0800 Subject: [PATCH] add micro support Signed-off-by: gudehe --- .../src/medialibrary_utils_test.cpp | 8 +-- frameworks/js/src/file_asset_napi.cpp | 36 ++++++---- frameworks/services/media_thumbnail/BUILD.gn | 1 + .../include/default_thumbnail_helper.h | 2 +- .../include/ithumbnail_helper.h | 2 + .../media_thumbnail/include/micro_helper.h | 33 +++++++++ .../media_thumbnail/include/thumbnail_const.h | 8 +++ .../include/thumbnail_helper_factory.h | 2 +- .../media_thumbnail/include/thumbnail_utils.h | 9 ++- .../media_thumbnail/src/ithumbnail_helper.cpp | 50 +++++++++---- .../media_thumbnail/src/micro_helper.cpp | 70 +++++++++++++++++++ .../src/thumbnail_aging_helper.cpp | 2 +- .../src/thumbnail_helper_factory.cpp | 21 +++--- .../media_thumbnail/src/thumbnail_service.cpp | 2 +- .../media_thumbnail/src/thumbnail_utils.cpp | 62 +++++++++++++--- .../include/media_thumbnail_helper.h | 2 +- 16 files changed, 253 insertions(+), 57 deletions(-) create mode 100644 frameworks/services/media_thumbnail/include/micro_helper.h create mode 100644 frameworks/services/media_thumbnail/src/micro_helper.cpp diff --git a/frameworks/innerkitsimpl/test/unittest/medialibrary_utils_test/src/medialibrary_utils_test.cpp b/frameworks/innerkitsimpl/test/unittest/medialibrary_utils_test/src/medialibrary_utils_test.cpp index ec166e9c1..46efde466 100644 --- a/frameworks/innerkitsimpl/test/unittest/medialibrary_utils_test/src/medialibrary_utils_test.cpp +++ b/frameworks/innerkitsimpl/test/unittest/medialibrary_utils_test/src/medialibrary_utils_test.cpp @@ -526,17 +526,17 @@ HWTEST_F(MediaLibraryExtUnitTest, medialib_LoadSourceImage_test_001, TestSize.Le ThumbnailData data; data.mediaType = MEDIA_TYPE_VIDEO; bool isThumbnail = true; - bool ret = ThumbnailUtils::LoadSourceImage(data, isThumbnail, desiredSize); + bool ret = ThumbnailUtils::LoadSourceImage(data, desiredSize, isThumbnail); EXPECT_EQ(ret, false); data.mediaType = MEDIA_TYPE_AUDIO; - ret = ThumbnailUtils::LoadSourceImage(data, isThumbnail, desiredSize); + ret = ThumbnailUtils::LoadSourceImage(data, desiredSize, isThumbnail); EXPECT_EQ(ret, false); data.mediaType = MEDIA_TYPE_MEDIA; - ret = ThumbnailUtils::LoadSourceImage(data, isThumbnail, desiredSize); + ret = ThumbnailUtils::LoadSourceImage(data, desiredSize, isThumbnail); EXPECT_EQ(ret, false); shared_ptr avMetadataHelper = AVMetadataHelperFactory::CreateAVMetadataHelper(); data.source = make_shared(); - ret = ThumbnailUtils::LoadSourceImage(data, isThumbnail, desiredSize); + ret = ThumbnailUtils::LoadSourceImage(data, desiredSize, isThumbnail); EXPECT_EQ(ret, true); } diff --git a/frameworks/js/src/file_asset_napi.cpp b/frameworks/js/src/file_asset_napi.cpp index ebdac0d6a..44f4c05fc 100644 --- a/frameworks/js/src/file_asset_napi.cpp +++ b/frameworks/js/src/file_asset_napi.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "abs_shared_result_set.h" #include "hitrace_meter.h" @@ -1442,19 +1443,30 @@ static unique_ptr QueryThumbnail(const std::string &uri, Size &size, c } tracer.Finish(); - tracer.Start("ImageSource::CreateImageSource"); - SourceOptions opts; - uint32_t err = 0; - unique_ptr imageSource = ImageSource::CreateImageSource(uniqueFd.Get(), opts, err); - if (imageSource == nullptr) { - NAPI_ERR_LOG("CreateImageSource err %{public}d", err); - return nullptr; - } + if ((size.width == DEFAULT_MICRO_SIZE) && (size.height == DEFAULT_MICRO_SIZE)) { + struct stat statInfo; + fstat(uniqueFd.Get(), &statInfo); + uint32_t *buffer = static_cast(malloc(statInfo.st_size)); + read(uniqueFd.Get(), buffer, statInfo.st_size); + InitializationOptions option; + auto pixel = PixelMap::Create(buffer, statInfo.st_size, option); + free(buffer); + return pixel; + } else { + tracer.Start("ImageSource::CreateImageSource"); + SourceOptions opts; + uint32_t err = 0; + unique_ptr imageSource = ImageSource::CreateImageSource(uniqueFd.Get(), opts, err); + if (imageSource == nullptr) { + NAPI_ERR_LOG("CreateImageSource err %{public}d", err); + return nullptr; + } - DecodeOptions decodeOpts; - decodeOpts.desiredSize = size; - decodeOpts.allocatorType = AllocatorType::SHARE_MEM_ALLOC; - return imageSource->CreatePixelMap(decodeOpts, err); + DecodeOptions decodeOpts; + decodeOpts.desiredSize = size; + decodeOpts.allocatorType = AllocatorType::SHARE_MEM_ALLOC; + return imageSource->CreatePixelMap(decodeOpts, err); + } } static void JSGetThumbnailExecute(FileAssetAsyncContext* context) diff --git a/frameworks/services/media_thumbnail/BUILD.gn b/frameworks/services/media_thumbnail/BUILD.gn index c0fc8ca41..84b0727e4 100644 --- a/frameworks/services/media_thumbnail/BUILD.gn +++ b/frameworks/services/media_thumbnail/BUILD.gn @@ -23,6 +23,7 @@ ohos_shared_library("medialibrary_thumbnail") { "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/default_thumbnail_helper.cpp", "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/ithumbnail_helper.cpp", "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/lcd_thumbnail_helper.cpp", + "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/micro_helper.cpp", "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/thumbnail_aging_helper.cpp", "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/thumbnail_datashare_bridge.cpp", "${MEDIALIB_SERVICES_PATH}/media_thumbnail/src/thumbnail_generate_helper.cpp", diff --git a/frameworks/services/media_thumbnail/include/default_thumbnail_helper.h b/frameworks/services/media_thumbnail/include/default_thumbnail_helper.h index 29d511c9d..16324352a 100644 --- a/frameworks/services/media_thumbnail/include/default_thumbnail_helper.h +++ b/frameworks/services/media_thumbnail/include/default_thumbnail_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/services/media_thumbnail/include/ithumbnail_helper.h b/frameworks/services/media_thumbnail/include/ithumbnail_helper.h index df26bbac7..098f6e467 100644 --- a/frameworks/services/media_thumbnail/include/ithumbnail_helper.h +++ b/frameworks/services/media_thumbnail/include/ithumbnail_helper.h @@ -78,6 +78,8 @@ protected: static bool DoCreateLcd(ThumbRdbOpt &opts, ThumbnailData &data); static bool DoCreateThumbnail(ThumbRdbOpt &opts, ThumbnailData &data); static bool DoThumbnailSync(ThumbRdbOpt &opts, ThumbnailData &outData); +private: + static bool LoadThumbnail(ThumbRdbOpt &opts, ThumbnailData &data, const ThumbnailType type); }; // copy from foundation/distributeddatamgr/data_share/frameworks/native/common/include/idatashare.h diff --git a/frameworks/services/media_thumbnail/include/micro_helper.h b/frameworks/services/media_thumbnail/include/micro_helper.h new file mode 100644 index 000000000..2ca93d0d3 --- /dev/null +++ b/frameworks/services/media_thumbnail/include/micro_helper.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_MICRO_THUMBNAIL_HELPER_H_ +#define FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_MICRO_THUMBNAIL_HELPER_H_ + +#include "ithumbnail_helper.h" + +namespace OHOS { +namespace Media { +class MicroHelper : public IThumbnailHelper { +public: + MicroHelper() = default; + virtual ~MicroHelper() override = default; + int32_t CreateThumbnail(ThumbRdbOpt &opts, bool isSync = false) override; + int32_t GetThumbnailPixelMap(ThumbRdbOpt &opts) override; +}; +} // namespace Media +} // namespace OHOS + +#endif // FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_MICRO_THUMBNAIL_HELPER_H_ \ No newline at end of file diff --git a/frameworks/services/media_thumbnail/include/thumbnail_const.h b/frameworks/services/media_thumbnail/include/thumbnail_const.h index d2d9fb11b..839ac216a 100644 --- a/frameworks/services/media_thumbnail/include/thumbnail_const.h +++ b/frameworks/services/media_thumbnail/include/thumbnail_const.h @@ -20,9 +20,16 @@ namespace OHOS { namespace Media { +constexpr int32_t DEFAULT_MICRO_SIZE = 64; constexpr int32_t DEFAULT_THUMBNAIL_SIZE = 256; constexpr int32_t DEFAULT_LCD_SIZE = 1080; +enum class ThumbnailType : int32_t { + MICRO, + THUMB, + LCD +}; + constexpr uint32_t DEVICE_UDID_LENGTH = 65; constexpr int32_t THUMBNAIL_LCD_GENERATE_THRESHOLD = 5000; @@ -30,6 +37,7 @@ constexpr int32_t THUMBNAIL_LCD_AGING_THRESHOLD = 10000; constexpr int32_t WAIT_FOR_MS = 1000; constexpr int32_t WAIT_FOR_SECOND = 3; +const std::string THUMBNAIL_MICRO_SUFFIX = "MICRO"; const std::string THUMBNAIL_THUMB_SUFFIX = "THMB"; const std::string THUMBNAIL_LCD_SUFFIX = "LCD"; const std::string FILE_URI_PREX = "file://"; diff --git a/frameworks/services/media_thumbnail/include/thumbnail_helper_factory.h b/frameworks/services/media_thumbnail/include/thumbnail_helper_factory.h index 327628e65..572cd3a01 100644 --- a/frameworks/services/media_thumbnail/include/thumbnail_helper_factory.h +++ b/frameworks/services/media_thumbnail/include/thumbnail_helper_factory.h @@ -26,7 +26,7 @@ public: virtual ~ThumbnailHelperFactory() = delete; static std::shared_ptr GetThumbnailHelper(const Size &size); - static bool IsThumbnailFromLcd(const Size &size); + static bool IsThumbnail(const Size &size); }; } // namespace Media } // namespace OHOS diff --git a/frameworks/services/media_thumbnail/include/thumbnail_utils.h b/frameworks/services/media_thumbnail/include/thumbnail_utils.h index d8a7cdd7a..07f321235 100644 --- a/frameworks/services/media_thumbnail/include/thumbnail_utils.h +++ b/frameworks/services/media_thumbnail/include/thumbnail_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -90,7 +90,7 @@ public: const std::shared_ptr &kvStore); static bool DeleteLcdData(ThumbRdbOpt &opts, ThumbnailData &data); static bool DeleteDistributeLcdData(ThumbRdbOpt &opts, ThumbnailData &thumbnailData); - static bool DeleteThumbFile(ThumbnailData &data, bool isLcd); + static bool DeleteThumbFile(ThumbnailData &data, ThumbnailType type); static bool DeleteDistributeThumbnailInfo(ThumbRdbOpt &opts); static bool GetKvResultSet(const std::shared_ptr &kvStore, const std::string &key, @@ -98,14 +98,13 @@ public: static bool DeleteOriginImage(ThumbRdbOpt &opts, ThumbnailData &thumbnailData); static std::string GetThumbPath(const std::string &path, const std::string &key); // Steps - static bool LoadSourceImage(ThumbnailData &data, const bool isThumbnail = true, - const Size &desiredSize = { DEFAULT_THUMBNAIL_SIZE, DEFAULT_THUMBNAIL_SIZE }); + static bool LoadSourceImage(ThumbnailData &data, const Size &desiredSize, const bool isThumbnail = true); static DistributedKv::Status SaveThumbnailData(ThumbnailData &data, const std::string &networkId, const std::shared_ptr &kvStore); static DistributedKv::Status SaveLcdData(ThumbnailData &data, const std::string &networkId, const std::shared_ptr &kvStore); - static int SaveFile(ThumbnailData &Data, bool isLcd); + static int SaveFile(ThumbnailData &Data, ThumbnailType type); static bool UpdateLcdInfo(ThumbRdbOpt &opts, ThumbnailData &data, int &err); static bool UpdateVisitTime(ThumbRdbOpt &opts, ThumbnailData &data, int &err); static bool DoUpdateRemoteThumbnail(ThumbRdbOpt &opts, ThumbnailData &data, int &err); diff --git a/frameworks/services/media_thumbnail/src/ithumbnail_helper.cpp b/frameworks/services/media_thumbnail/src/ithumbnail_helper.cpp index 911ae5651..8134a6c91 100644 --- a/frameworks/services/media_thumbnail/src/ithumbnail_helper.cpp +++ b/frameworks/services/media_thumbnail/src/ithumbnail_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -119,6 +119,8 @@ WaitStatus ThumbnailWait::InsertAndWait(const string &id, bool isLcd) if (isLcd) { id_ += THUMBNAIL_LCD_SUFFIX; + } else { + id_ += THUMBNAIL_THUMB_SUFFIX; } unique_lock writeLck(mutex_); auto iter = thumbnailMap_.find(id_); @@ -185,14 +187,14 @@ bool IThumbnailHelper::DoCreateLcd(ThumbRdbOpt &opts, ThumbnailData &data) return false; } - if (!ThumbnailUtils::LoadSourceImage(data, false, opts.screenSize)) { + if (!ThumbnailUtils::LoadSourceImage(data, opts.screenSize, false)) { if (opts.path.empty()) { MEDIA_ERR_LOG("LoadSourceImage faild, %{private}s", data.path.c_str()); return false; } else { opts.path = ""; GetThumbnailInfo(opts, data); - if (!ThumbnailUtils::LoadSourceImage(data, false, opts.screenSize)) { + if (!ThumbnailUtils::LoadSourceImage(data, opts.screenSize, false)) { return false; } } @@ -203,7 +205,7 @@ bool IThumbnailHelper::DoCreateLcd(ThumbRdbOpt &opts, ThumbnailData &data) return false; } - int err = ThumbnailUtils::SaveFile(data, true); + int err = ThumbnailUtils::SaveFile(data, ThumbnailType::LCD); if (err < 0) { MEDIA_ERR_LOG("SaveLcd faild %{public}d", err); return false; @@ -218,32 +220,39 @@ bool IThumbnailHelper::DoCreateLcd(ThumbRdbOpt &opts, ThumbnailData &data) return true; } -bool IThumbnailHelper::DoCreateThumbnail(ThumbRdbOpt &opts, ThumbnailData &data) +bool IThumbnailHelper::LoadThumbnail(ThumbRdbOpt &opts, ThumbnailData &data, const ThumbnailType type) { - ThumbnailWait thumbnailWait(true); - auto ret = thumbnailWait.InsertAndWait(data.id, false); - if (ret == WaitStatus::WAIT_SUCCESS) { - return true; + Size size; + switch (type) { + case ThumbnailType::MICRO: + size = { DEFAULT_MICRO_SIZE, DEFAULT_MICRO_SIZE }; + break; + case ThumbnailType::THUMB: + size = { DEFAULT_THUMBNAIL_SIZE, DEFAULT_THUMBNAIL_SIZE }; + break; + default: + MEDIA_ERR_LOG("invalid thumbnail type: %{public}d", type); + return false; } - if (!ThumbnailUtils::LoadSourceImage(data)) { + if (!ThumbnailUtils::LoadSourceImage(data, size)) { if (opts.path.empty()) { MEDIA_ERR_LOG("LoadSourceImage faild, %{private}s", data.path.c_str()); return false; } else { opts.path = ""; GetThumbnailInfo(opts, data); - if (!ThumbnailUtils::LoadSourceImage(data)) { + if (!ThumbnailUtils::LoadSourceImage(data, size)) { return false; } } } - if (!ThumbnailUtils::CompressImage(data.source, data.thumbnail)) { + if ((type != ThumbnailType::MICRO) && !ThumbnailUtils::CompressImage(data.source, data.thumbnail)) { MEDIA_ERR_LOG("CompressImage faild"); return false; } - int err = ThumbnailUtils::SaveFile(data, false); + int err = ThumbnailUtils::SaveFile(data, type); if (err < 0) { MEDIA_ERR_LOG("SaveThumbnailData faild %{public}d", err); return false; @@ -252,6 +261,21 @@ bool IThumbnailHelper::DoCreateThumbnail(ThumbRdbOpt &opts, ThumbnailData &data) return true; } +bool IThumbnailHelper::DoCreateThumbnail(ThumbRdbOpt &opts, ThumbnailData &data) +{ + ThumbnailWait thumbnailWait(true); + auto ret = thumbnailWait.InsertAndWait(data.id, false); + if (ret == WaitStatus::WAIT_SUCCESS) { + return true; + } + + LoadThumbnail(opts, data, ThumbnailType::MICRO); + if (!LoadThumbnail(opts, data, ThumbnailType::THUMB)) { + return false; + } + return true; +} + bool IThumbnailHelper::DoThumbnailSync(ThumbRdbOpt &opts, ThumbnailData &outData) { ThumbnailConnection *conn = new (nothrow) ThumbnailConnection; diff --git a/frameworks/services/media_thumbnail/src/micro_helper.cpp b/frameworks/services/media_thumbnail/src/micro_helper.cpp new file mode 100644 index 000000000..8ca48d871 --- /dev/null +++ b/frameworks/services/media_thumbnail/src/micro_helper.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define MLOG_TAG "Thumbnail" + +#include + +#include "micro_helper.h" +#include "medialibrary_errno.h" +#include "media_log.h" +#include "thumbnail_const.h" +#include "thumbnail_utils.h" + +using namespace std; +using namespace OHOS::DistributedKv; +using namespace OHOS::NativeRdb; + +namespace OHOS { +namespace Media { +int32_t MicroHelper::CreateThumbnail(ThumbRdbOpt &opts, bool isSync) +{ + ThumbnailData thumbnailData; + GetThumbnailInfo(opts, thumbnailData); + + string fileName = ThumbnailUtils::GetThumbPath(thumbnailData.path, THUMBNAIL_MICRO_SUFFIX); + if (access(fileName.c_str(), F_OK) == 0) { + MEDIA_DEBUG_LOG("CreateThumbnail key is same, no need generate"); + return E_OK; + } + + if (isSync) { + DoCreateThumbnail(opts, thumbnailData); + } else { + IThumbnailHelper::AddAsyncTask(IThumbnailHelper::CreateThumbnail, opts, thumbnailData, true); + } + return E_OK; +} + +int32_t MicroHelper::GetThumbnailPixelMap(ThumbRdbOpt &opts) +{ + ThumbnailWait thumbnailWait(false); + thumbnailWait.CheckAndWait(opts.row, false); + ThumbnailData thumbnailData; + GetThumbnailInfo(opts, thumbnailData); + + string fileName = ThumbnailUtils::GetThumbPath(thumbnailData.path, THUMBNAIL_MICRO_SUFFIX); + if (access(fileName.c_str(), F_OK) != 0) { + if (!DoCreateThumbnail(opts, thumbnailData)) { + return E_THUMBNAIL_LOCAL_CREATE_FAIL; + } + } + auto fd = open(fileName.c_str(), O_RDONLY); + if (fd >= 0) { + return fd; + } + return -errno; +} +} // namespace Media +} // namespace OHOS diff --git a/frameworks/services/media_thumbnail/src/thumbnail_aging_helper.cpp b/frameworks/services/media_thumbnail/src/thumbnail_aging_helper.cpp index aa862ae50..57e1cf80e 100644 --- a/frameworks/services/media_thumbnail/src/thumbnail_aging_helper.cpp +++ b/frameworks/services/media_thumbnail/src/thumbnail_aging_helper.cpp @@ -111,7 +111,7 @@ int32_t ThumbnailAgingHelper::ClearLcdFromFileTable(ThumbRdbOpt &opts) } for (uint32_t i = 0; i < infos.size(); i++) { opts.row = infos[i].id; - if (ThumbnailUtils::DeleteThumbFile(infos[i], true)) { + if (ThumbnailUtils::DeleteThumbFile(infos[i], ThumbnailType::LCD)) { ThumbnailUtils::CleanThumbnailInfo(opts, false, true); } } diff --git a/frameworks/services/media_thumbnail/src/thumbnail_helper_factory.cpp b/frameworks/services/media_thumbnail/src/thumbnail_helper_factory.cpp index 0a53ea04d..8d2301295 100644 --- a/frameworks/services/media_thumbnail/src/thumbnail_helper_factory.cpp +++ b/frameworks/services/media_thumbnail/src/thumbnail_helper_factory.cpp @@ -17,6 +17,7 @@ #include "default_thumbnail_helper.h" #include "lcd_thumbnail_helper.h" +#include "micro_helper.h" #include "thumbnail_const.h" using namespace std; @@ -25,20 +26,24 @@ namespace OHOS { namespace Media { shared_ptr ThumbnailHelperFactory::GetThumbnailHelper(const Size &size) { - bool isFromLcd = IsThumbnailFromLcd(size); - if (isFromLcd) { - shared_ptr resultHelper = make_shared(); - return resultHelper; + if (IsThumbnail(size)) { + if ((size.width == DEFAULT_MICRO_SIZE) && (size.height == DEFAULT_MICRO_SIZE)) { + shared_ptr resultHelper = make_shared(); + return resultHelper; + } else { + shared_ptr resultHelper = make_shared(); + return resultHelper; + } } else { - shared_ptr resultHelper = make_shared(); + shared_ptr resultHelper = make_shared(); return resultHelper; } } -bool ThumbnailHelperFactory::IsThumbnailFromLcd(const Size &size) +bool ThumbnailHelperFactory::IsThumbnail(const Size &size) { - return !((size.width <= DEFAULT_THUMBNAIL_SIZE) && - (size.height <= DEFAULT_THUMBNAIL_SIZE)); + return (size.width <= DEFAULT_THUMBNAIL_SIZE) && + (size.height <= DEFAULT_THUMBNAIL_SIZE); } } // namespace Media } // namespace OHOS \ No newline at end of file diff --git a/frameworks/services/media_thumbnail/src/thumbnail_service.cpp b/frameworks/services/media_thumbnail/src/thumbnail_service.cpp index 7cd9c789e..93c20e42f 100644 --- a/frameworks/services/media_thumbnail/src/thumbnail_service.cpp +++ b/frameworks/services/media_thumbnail/src/thumbnail_service.cpp @@ -108,7 +108,7 @@ int ThumbnailService::GetThumbnailFd(const string &uri) if (thumbnailHelper == nullptr) { return E_NO_MEMORY; } - if (ThumbnailHelperFactory::IsThumbnailFromLcd(size)) { + if (!ThumbnailHelperFactory::IsThumbnail(size)) { opts.screenSize = screenSize_; } int fd = thumbnailHelper->GetThumbnailPixelMap(opts); diff --git a/frameworks/services/media_thumbnail/src/thumbnail_utils.cpp b/frameworks/services/media_thumbnail/src/thumbnail_utils.cpp index 67d9c53e7..568f132f2 100644 --- a/frameworks/services/media_thumbnail/src/thumbnail_utils.cpp +++ b/frameworks/services/media_thumbnail/src/thumbnail_utils.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -106,9 +106,28 @@ bool ThumbnailUtils::DeleteDistributeLcdData(ThumbRdbOpt &opts, ThumbnailData &t return true; } -bool ThumbnailUtils::DeleteThumbFile(ThumbnailData &data, bool isLcd) +static string GetThumbnailSuffix(ThumbnailType type) { - string fileName = ThumbnailUtils::GetThumbPath(data.path, isLcd ? THUMBNAIL_LCD_SUFFIX : THUMBNAIL_THUMB_SUFFIX); + string suffix; + switch (type) { + case ThumbnailType::MICRO: + suffix = THUMBNAIL_MICRO_SUFFIX; + break; + case ThumbnailType::THUMB: + suffix = THUMBNAIL_THUMB_SUFFIX; + break; + case ThumbnailType::LCD: + suffix = THUMBNAIL_LCD_SUFFIX; + break; + default: + return ""; + } + return suffix; +} + +bool ThumbnailUtils::DeleteThumbFile(ThumbnailData &data, ThumbnailType type) +{ + string fileName = ThumbnailUtils::GetThumbPath(data.path, GetThumbnailSuffix(type)); if (!MediaFileUtils::DeleteFile(fileName)) { MEDIA_ERR_LOG("delete file faild %{public}d", errno); return false; @@ -967,7 +986,7 @@ Size ThumbnailUtils::ConvertDecodeSize(const Size &sourceSize, const Size &desir return decodeSize; } -bool ThumbnailUtils::LoadSourceImage(ThumbnailData &data, const bool isThumbnail, const Size &desiredSize) +bool ThumbnailUtils::LoadSourceImage(ThumbnailData &data, const Size &desiredSize, const bool isThumbnail) { if (data.source != nullptr) { return true; @@ -1007,10 +1026,34 @@ bool ThumbnailUtils::LoadSourceImage(ThumbnailData &data, const bool isThumbnail return true; } -int ThumbnailUtils::SaveFile(ThumbnailData &data, bool isLcd) +int ThumbnailUtils::SaveFile(ThumbnailData &data, ThumbnailType type) { const mode_t fileMode = 0664; - string fileName = ThumbnailUtils::GetThumbPath(data.path, isLcd ? THUMBNAIL_LCD_SUFFIX : THUMBNAIL_THUMB_SUFFIX); + + string suffix; + uint8_t *output; + int writeSize; + switch (type) { + case ThumbnailType::MICRO: + suffix = THUMBNAIL_MICRO_SUFFIX; + output = const_cast(data.source->GetPixels()); + writeSize = data.source->GetByteCount(); + break; + case ThumbnailType::THUMB: + suffix = THUMBNAIL_THUMB_SUFFIX; + output = data.lcd.data(); + writeSize = data.lcd.size(); + break; + case ThumbnailType::LCD: + suffix = THUMBNAIL_LCD_SUFFIX; + output = data.thumbnail.data(); + writeSize = data.thumbnail.size(); + break; + default: + MEDIA_ERR_LOG("Invalid thumbnail type: %{public}d", type); + return E_INVALID_ARGUMENTS; + } + string fileName = ThumbnailUtils::GetThumbPath(data.path, suffix); string dir = MediaFileUtils::GetParentPath(fileName); if (!MediaFileUtils::CreateDirectory(dir)) { return -errno; @@ -1028,8 +1071,7 @@ int ThumbnailUtils::SaveFile(ThumbnailData &data, bool isLcd) } } - int writeSize = isLcd ? data.lcd.size() : data.thumbnail.size(); - int size = write(fd.Get(), isLcd ? data.lcd.data() : data.thumbnail.data(), writeSize); + int size = write(fd.Get(), output, writeSize); if (size != writeSize) { return E_NO_SPACE; } @@ -1216,10 +1258,10 @@ bool ThumbnailUtils::DeleteOriginImage(ThumbRdbOpt &opts, ThumbnailData &thumbna return isDelete; } } - if (DeleteThumbFile(tmpData, false)) { + if (DeleteThumbFile(tmpData, ThumbnailType::THUMB)) { isDelete = true; } - if (DeleteThumbFile(tmpData, true)) { + if (DeleteThumbFile(tmpData, ThumbnailType::LCD)) { isDelete = true; } return isDelete; diff --git a/interfaces/inner_api/media_library_helper/include/media_thumbnail_helper.h b/interfaces/inner_api/media_library_helper/include/media_thumbnail_helper.h index d03888ee3..350708665 100644 --- a/interfaces/inner_api/media_library_helper/include/media_thumbnail_helper.h +++ b/interfaces/inner_api/media_library_helper/include/media_thumbnail_helper.h @@ -26,7 +26,7 @@ class MediaThumbnailHelper { public: MediaThumbnailHelper(); ~MediaThumbnailHelper() = default; - static bool IsThumbnailFromLcd(const Size &size); + static bool IsThumbnail(const Size &size); static bool ResizeImage(const std::vector &data, const Size &size, std::unique_ptr &pixelMap); }; } // namespace Media -- Gitee