From 61e31d8653fb4e40c7641385ecbedf128caf3c65 Mon Sep 17 00:00:00 2001 From: zhaona45 Date: Thu, 24 Jul 2025 16:10:49 +0800 Subject: [PATCH] =?UTF-8?q?Image=20API20=20=E6=94=AF=E6=8C=81ArkTs?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaona45 Change-Id: Ic902c46a6d789ee0ef6e442a06ca88082378b29e --- frameworks/kits/taihe/BUILD.gn | 1 + .../idl/ohos.multimedia.image.image.taihe | 26 ++++++- .../kits/taihe/include/image_source_taihe.h | 5 +- .../kits/taihe/src/image_packer_taihe.cpp | 13 +++- .../kits/taihe/src/image_source_taihe.cpp | 74 ++++++++++++++++++- frameworks/kits/taihe/src/picture_taihe.cpp | 45 +++++++++-- 6 files changed, 150 insertions(+), 14 deletions(-) diff --git a/frameworks/kits/taihe/BUILD.gn b/frameworks/kits/taihe/BUILD.gn index 3912e5cac..90b40a56b 100644 --- a/frameworks/kits/taihe/BUILD.gn +++ b/frameworks/kits/taihe/BUILD.gn @@ -70,6 +70,7 @@ template("image_taihe_shared_library") { sources = get_target_outputs(":run_taihe") sources += [ "${image_subsystem}/frameworks/innerkitsimpl/accessor/src/exif_metadata_formatter.cpp", + "${image_subsystem}/frameworks/kits/js/common/image_error_convert.cpp", "src/auxiliary_picture_taihe.cpp", "src/image_creator_taihe.cpp", "src/image_packer_taihe.cpp", diff --git a/frameworks/kits/taihe/idl/ohos.multimedia.image.image.taihe b/frameworks/kits/taihe/idl/ohos.multimedia.image.image.taihe index 023cd71b5..8432be2ae 100644 --- a/frameworks/kits/taihe/idl/ohos.multimedia.image.image.taihe +++ b/frameworks/kits/taihe/idl/ohos.multimedia.image.image.taihe @@ -426,6 +426,7 @@ enum AuxiliaryPictureType: i32 { enum MetadataType: i32 { EXIF_METADATA = 1, FRAGMENT_METADATA = 2, + GIF_METADATA = 5 } enum FragmentMapPropertyKey: String { @@ -435,6 +436,11 @@ enum FragmentMapPropertyKey: String { HEIGHT = "FragmentImageHeight" } +enum GifPropertyKey: String { + GIF_DELAY_TIME = "GifDelayTime", + GIF_DISPOSAL_TYPE = "GifDisposalType" +} + struct DecodingOptionsForPicture { desiredAuxiliaryPictures: Array; } @@ -712,12 +718,13 @@ interface ImageSource { @gen_promise("getFrameCount") GetFrameCountSync(): i32; - @gen_promise("getImageProperty") - GetImagePropertySync(key: PropertyKey, options: Optional): String; + @promise GetImageProperty(key: PropertyKey, options: Optional): String; @gen_promise("getImageProperties") GetImagePropertiesSync(key: Array): @record Map; + GetImagePropertySync(key: PropertyKey): String; + @gen_promise("modifyImageProperty") ModifyImagePropertySync(key: PropertyKey, value: String): void; @@ -735,6 +742,12 @@ interface ImageSource { @gen_promise("createPicture") CreatePictureSync(options: Optional): Picture; + @gen_promise("createPictureAtIndex") + CreatePictureAtIndexSync(index: i32): Optional; + + @gen_promise("createWideGamutSdrPixelMap") + CreateWideGamutSdrPixelMapSync(): PixelMap; + @get GetSupportedFormats(): Array; } @@ -993,6 +1006,10 @@ function CreateIncrementalSourceByArrayBufferOption(buf: @arraybuffer Array, @overload("createImageSource") function CreateImageSourceByRawFileDescriptorOption(rawfile: @sts_type("resourceManager.RawFileDescriptor") Opaque, options: Optional): ImageSource; +function GetImageSourceSupportedFormats(): Array; + +function GetImagePackerSupportedFormats(): Array; + // For legacy ANI backward compatibility function CreateImageSourceByPtr(ptr: i64): ImageSource; @@ -1010,4 +1027,7 @@ function CreatePictureByPtr(ptr: i64): Picture; function CreatePictureFromParcel(sequence: @sts_type("rpc.MessageSequence") Opaque): Picture; -function CreateAuxiliaryPicture(buffer: @arraybuffer Array, size: Size, type: AuxiliaryPictureType): AuxiliaryPicture; \ No newline at end of file +function CreateAuxiliaryPicture(buffer: @arraybuffer Array, size: Size, type: AuxiliaryPictureType): AuxiliaryPicture; + +@gen_promise("createPictureByHdrAndSdrPixelMap") +function CreatePictureByHdrAndSdrPixelMapSync(hdrPixelMap: PixelMap, sdrPixelMap: PixelMap): Picture; \ No newline at end of file diff --git a/frameworks/kits/taihe/include/image_source_taihe.h b/frameworks/kits/taihe/include/image_source_taihe.h index fd390e743..cdf44edea 100644 --- a/frameworks/kits/taihe/include/image_source_taihe.h +++ b/frameworks/kits/taihe/include/image_source_taihe.h @@ -42,20 +42,23 @@ public: PixelMap CreatePixelMapSync(); PixelMap CreatePixelMapUsingAllocatorSync(optional_view options, optional_view allocatorType); + PixelMap CreateWideGamutSdrPixelMapSync(); array CreatePixelMapListSync(); array CreatePixelMapListSyncWithOptions(DecodingOptions const& options); array CreatePixelMapListSyncWithOptionalOptions(optional_view options); array GetDelayTimeListSync(); array GetDisposalTypeListSync(); int32_t GetFrameCountSync(); - string GetImagePropertySync(PropertyKey key, optional_view options); + string GetImageProperty(PropertyKey key, optional_view options); map GetImagePropertiesSync(array_view key); + string GetImagePropertySync(PropertyKey key); void ModifyImagePropertySync(PropertyKey key, string_view value); void ModifyImagePropertiesSync(map_view records); void UpdateDataSync(array_view buf, bool isFinished, int32_t offset, int32_t length); void ReleaseSync(); #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) Picture CreatePictureSync(optional_view options); + optional CreatePictureAtIndexSync(int32_t index); #endif array GetSupportedFormats(); diff --git a/frameworks/kits/taihe/src/image_packer_taihe.cpp b/frameworks/kits/taihe/src/image_packer_taihe.cpp index d418440b5..4d544447c 100644 --- a/frameworks/kits/taihe/src/image_packer_taihe.cpp +++ b/frameworks/kits/taihe/src/image_packer_taihe.cpp @@ -796,6 +796,17 @@ ImagePacker CreateImagePacker() std::shared_ptr imagePacker = std::make_shared(); return make_holder(imagePacker); } + +array GetImagePackerSupportedFormats() +{ + std::set formats; + uint32_t ret = OHOS::Media::ImagePacker::GetSupportedFormats(formats); + if (ret != OHOS::Media::SUCCESS) { + ImageTaiheUtils::ThrowExceptionError("Fail to get decode supported formats"); + } + return ImageTaiheUtils::ToTaiheArrayString(std::vector(formats.begin(), formats.end())); +} } // namespace ANI::Image -TH_EXPORT_CPP_API_CreateImagePacker(CreateImagePacker); \ No newline at end of file +TH_EXPORT_CPP_API_CreateImagePacker(CreateImagePacker); +TH_EXPORT_CPP_API_GetImagePackerSupportedFormats(GetImagePackerSupportedFormats); \ No newline at end of file diff --git a/frameworks/kits/taihe/src/image_source_taihe.cpp b/frameworks/kits/taihe/src/image_source_taihe.cpp index 43eb28cb7..6d7d04fbf 100644 --- a/frameworks/kits/taihe/src/image_source_taihe.cpp +++ b/frameworks/kits/taihe/src/image_source_taihe.cpp @@ -16,6 +16,7 @@ #include "ani_color_space_object_convertor.h" #include "image_common.h" #include "image_dfx.h" +#include "image_error_convert.h" #include "image_log.h" #include "image_source_taihe.h" #include "image_source_taihe_ani.h" @@ -576,6 +577,16 @@ PixelMap ImageSourceImpl::CreatePixelMapUsingAllocatorSync(optional_view context = std::make_unique(); + context->decodeOpts.isCreateWideGamutSdrPixelMap = true; + context->decodeOpts.desiredDynamicRange = OHOS::Media::DecodeDynamicRange::AUTO; + context->rPixelMap = CreatePixelMapInner(this, nativeImgSrc, context->index, context->decodeOpts, context->status); + return CreatePixelMapComplete(context); +} + static bool CheckAsyncContext(std::unique_ptr &taiheContext, bool check) { if (taiheContext == nullptr) { @@ -806,9 +817,9 @@ static void GenerateErrMsg(std::unique_ptr &context, st } } -string ImageSourceImpl::GetImagePropertySync(PropertyKey key, optional_view options) +string ImageSourceImpl::GetImageProperty(PropertyKey key, optional_view options) { - OHOS::Media::ImageTrace imageTrace("ImageSourceImpl::GetImagePropertySync"); + OHOS::Media::ImageTrace imageTrace("ImageSourceImpl::GetImagePropertyImpl"); std::unique_ptr context = std::make_unique(); if (nativeImgSrc == nullptr) { @@ -834,6 +845,34 @@ string ImageSourceImpl::GetImagePropertySync(PropertyKey key, optional_viewvalueStr; } +string ImageSourceImpl::GetImagePropertySync(PropertyKey key) +{ + OHOS::Media::ImageTrace imageTrace("ImageSourceImpl::GetImagePropertySync"); + IMAGE_LOGD("GetImagePropertySync IN"); + + std::string propertyKey = key.get_value(); + std::string value = ""; + + if (nativeImgSrc != nullptr) { + uint32_t ret = nativeImgSrc->GetImagePropertyStringBySync(NUM_0, propertyKey, value); + if (ret == OHOS::Media::ERR_IMAGE_PROPERTY_NOT_EXIST) { + IMAGE_LOGE("%{public}s: Unsupported metadata, errorCode=%{public}u", __func__, ret); + ImageTaiheUtils::ThrowExceptionError(IMAGE_SOURCE_UNSUPPORTED_METADATA, "Unsupported metadata"); + } + if (ret == OHOS::Media::ERR_IMAGE_SOURCE_DATA) { + IMAGE_LOGE("%{public}s: Unsupported MIME type, errorCode=%{public}u", __func__, ret); + ImageTaiheUtils::ThrowExceptionError(IMAGE_SOURCE_UNSUPPORTED_MIMETYPE, "Unsupported MIME type"); + } + if (ret == OHOS::Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT) { + IMAGE_LOGE("%{public}s: Bad source, errorCode=%{public}u", __func__, ret); + ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_SOURCE, "Bad source"); + } + } else { + ImageTaiheUtils::ThrowExceptionError("empty native image source"); + } + return value; +} + static void GetImagePropertiesExecute(std::unique_ptr &context) { if (context == nullptr) { @@ -1258,6 +1297,24 @@ Picture ImageSourceImpl::CreatePictureSync(optional_view ImageSourceImpl::CreatePictureAtIndexSync(int32_t index) +{ + if (nativeImgSrc == nullptr) { + ImageTaiheUtils::ThrowExceptionError("empty native rImageSource"); + return optional(std::nullopt); + } + + uint32_t errCode = OHOS::Media::ERR_MEDIA_INVALID_VALUE; + auto picture = nativeImgSrc->CreatePictureAtIndex(index, errCode); + if (errCode != OHOS::Media::SUCCESS || picture == nullptr) { + auto errMsg = OHOS::Media::ImageErrorConvert::CreatePictureAtIndexMakeErrMsg(errCode); + ImageTaiheUtils::ThrowExceptionError(errMsg.first, errMsg.second); + return optional(std::nullopt); + } + auto res = make_holder(std::move(picture)); + return optional(std::in_place, res); +} #endif array ImageSourceImpl::GetSupportedFormats() @@ -1430,6 +1487,16 @@ ImageSource CreateImageSourceByPtr(int64_t ptr) { return taihe::make_holder(ptr); } + +array GetImageSourceSupportedFormats() +{ + std::set formats; + uint32_t ret = OHOS::Media::ImageSource::GetSupportedFormats(formats); + if (ret != OHOS::Media::SUCCESS) { + ImageTaiheUtils::ThrowExceptionError("Fail to get decode supported formats"); + } + return ImageTaiheUtils::ToTaiheArrayString(std::vector(formats.begin(), formats.end())); +} } // namespace ANI::Image TH_EXPORT_CPP_API_CreateImageSourceByUri(CreateImageSourceByUri); @@ -1441,4 +1508,5 @@ TH_EXPORT_CPP_API_CreateImageSourceByArrayBufferOption(CreateImageSourceByArrayB TH_EXPORT_CPP_API_CreateImageSourceByRawFileDescriptorOption(CreateImageSourceByRawFileDescriptorOption); TH_EXPORT_CPP_API_CreateIncrementalSourceByArrayBuffer(CreateIncrementalSourceByArrayBuffer); TH_EXPORT_CPP_API_CreateIncrementalSourceByArrayBufferOption(CreateIncrementalSourceByArrayBufferOption); -TH_EXPORT_CPP_API_CreateImageSourceByPtr(CreateImageSourceByPtr); \ No newline at end of file +TH_EXPORT_CPP_API_CreateImageSourceByPtr(CreateImageSourceByPtr); +TH_EXPORT_CPP_API_GetImageSourceSupportedFormats(GetImageSourceSupportedFormats); \ No newline at end of file diff --git a/frameworks/kits/taihe/src/picture_taihe.cpp b/frameworks/kits/taihe/src/picture_taihe.cpp index eeeed8a41..897131861 100644 --- a/frameworks/kits/taihe/src/picture_taihe.cpp +++ b/frameworks/kits/taihe/src/picture_taihe.cpp @@ -202,7 +202,7 @@ void PictureImpl::SetMetadataSync(MetadataType metadataType, weak::Metadata meta OHOS::Media::MetadataType type; uint32_t typeValue = metadataType.get_value(); - if (typeValue == static_cast(OHOS::Media::MetadataType::EXIF)) { + if (OHOS::Media::Picture::IsValidPictureMetadataType(static_cast(typeValue))) { type = OHOS::Media::MetadataType(typeValue); } else { ImageTaiheUtils::ThrowExceptionError(IMAGE_UNSUPPORTED_METADATA, "Unsupport MetadataType"); @@ -216,8 +216,7 @@ void PictureImpl::SetMetadataSync(MetadataType metadataType, weak::Metadata meta } std::shared_ptr imageMetadata = metadataImpl->GetNativeMetadata(); - int32_t status = nativePicture_->SetExifMetadata( - std::reinterpret_pointer_cast(imageMetadata)); + int32_t status = nativePicture_->SetMetadata(type, imageMetadata); if (status != OHOS::Media::SUCCESS) { ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERROR, "Set Metadata failed!"); } @@ -232,12 +231,12 @@ Metadata PictureImpl::GetMetadataSync(MetadataType metadataType) } uint32_t typeValue = metadataType.get_value(); - if (typeValue != static_cast(OHOS::Media::MetadataType::EXIF)) { + if (!OHOS::Media::Picture::IsValidPictureMetadataType(static_cast(typeValue))) { ImageTaiheUtils::ThrowExceptionError(IMAGE_UNSUPPORTED_METADATA, "Unsupport MetadataType"); return make_holder(); } - auto imageMetadata = std::reinterpret_pointer_cast(nativePicture_->GetExifMetadata()); + auto imageMetadata = nativePicture_->GetMetadata(static_cast(typeValue)); if (imageMetadata == nullptr) { ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERROR, "Get Metadata failed!"); return make_holder(); @@ -331,8 +330,42 @@ Picture CreatePictureByPtr(int64_t aniPtr) return make_holder(aniPtr); } +Picture CreatePictureByHdrAndSdrPixelMapSync(weak::PixelMap hdrPixelMap, weak::PixelMap sdrPixelMap) +{ + IMAGE_LOGD("CreatePictureByHdrAndSdrPixelMap IN"); + if (hdrPixelMap.is_error()) { + ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Get arg hdr Pixelmap failed"); + return make_holder(nullptr); + } + if (sdrPixelMap.is_error()) { + ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Get arg sdr Pixelmap failed"); + return make_holder(nullptr); + } + + PixelMapImpl* hdrPixelMapImpl = reinterpret_cast(hdrPixelMap->GetImplPtr()); + PixelMapImpl* sdrPixelMapImpl = reinterpret_cast(sdrPixelMap->GetImplPtr()); + if (hdrPixelMapImpl == nullptr) { + ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Get arg hdr Pixelmap failed"); + return make_holder(nullptr); + } + if (sdrPixelMapImpl == nullptr) { + ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Get arg sdr Pixelmap failed"); + return make_holder(nullptr); + } + + auto nativeHdrPixelMap = hdrPixelMapImpl->GetNativePtr(); + auto nativeSdrPixelMap = sdrPixelMapImpl->GetNativePtr(); + auto picture = OHOS::Media::Picture::CreatePictureByHdrAndSdrPixelMap(nativeHdrPixelMap, nativeSdrPixelMap); + if (picture == nullptr) { + ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERROR, "Create picture failed!"); + return make_holder(nullptr); + } + IMAGE_LOGD("CreatePictureByHdrAndSdrPixelMap OUT"); + return make_holder(std::move(picture)); +} } // namespace ANI::Image TH_EXPORT_CPP_API_CreatePictureByPixelMap(CreatePictureByPixelMap); TH_EXPORT_CPP_API_CreatePictureFromParcel(CreatePictureFromParcel); -TH_EXPORT_CPP_API_CreatePictureByPtr(CreatePictureByPtr); \ No newline at end of file +TH_EXPORT_CPP_API_CreatePictureByPtr(CreatePictureByPtr); +TH_EXPORT_CPP_API_CreatePictureByHdrAndSdrPixelMapSync(CreatePictureByHdrAndSdrPixelMapSync); \ No newline at end of file -- Gitee