• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用Image_NativeModule完成图片解码
2<!--Kit: Image Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @aulight02-->
5<!--Designer: @liyang_bryan-->
6<!--Tester: @xchaosioda-->
7<!--Adviser: @zengyawen-->
8
9创建ImageSource,获取位图的宽、高信息,以及释放ImageSource实例。
10
11## 开发步骤
12
13### 添加链接库
14
15在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加libimage_source.solibpixelmap.so以及日志依赖libhilog_ndk.z.so16
17```txt
18target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so libpixelmap.so)
19```
20
21### Native接口调用
22
23具体接口说明请参考[API文档](../../reference/apis-image-kit/capi-image-nativemodule.md)。
24
25在Deveco Studio新建Native C++应用,默认生成的项目中包含index.ets文件,在entry\src\main\cpp目录下会自动生成一个cpp文件(hello.cppnapi_init.cpp,本示例以hello.cpp文件名为例)。在hello.cpp中实现C API接口调用逻辑,示例代码如下:
26
27**解码接口使用示例**
28
29在创建ImageSource实例后,进行指定属性值的获取和修改、通过解码参数创建PixelMap对象、获取图像帧数等操作。
30
31```c++
32#include <string>
33#include <hilog/log.h>
34#include <multimedia/image_framework/image/image_common.h>
35#include <multimedia/image_framework/image/image_source_native.h>
36#include <multimedia/image_framework/image/pixelmap_native.h>
37
38#undef LOG_DOMAIN
39#undef LOG_TAG
40#define LOG_DOMAIN 0x3200
41#define LOG_TAG "MY_TAG"
42
43#define NUM_0 0
44#define NUM_1 1
45
46// 处理napi返回值。
47napi_value getJsResult(napi_env env, int result) {
48    napi_value resultNapi = nullptr;
49    napi_create_int32(env, result, &resultNapi);
50    return resultNapi;
51}
52
53static napi_value sourceTest(napi_env env, napi_callback_info info)
54{
55    napi_value argValue[NUM_1] = {0};
56    size_t argCount = NUM_1;
57    if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
58        argValue[NUM_0] == nullptr) {
59        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest napi_get_cb_info failed, argCount: %{public}lu.", static_cast<int>(argCount));
60        return getJsResult(env, IMAGE_BAD_PARAMETER);
61    }
62    char name[1024];
63    size_t nameSize = 1024;
64    napi_get_value_string_utf8(env, argValue[NUM_0], name, 1024, &nameSize);
65    // 获取解码能力范围。
66    Image_MimeType* mimeType = nullptr;
67    size_t length = 0;
68    Image_ErrorCode errCode = OH_ImageSourceNative_GetSupportedFormats(&mimeType, &length);
69    if (errCode != IMAGE_SUCCESS) {
70        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetSupportedFormats failed, errCode: %{public}d.", errCode);
71        return getJsResult(env, errCode);
72    }
73    for (size_t count = 0; count < length; count++) {
74        OH_LOG_INFO(LOG_APP, "Decode supportedFormats:%{public}s", mimeType[count].data);
75    }
76    // 创建ImageSource实例。
77    OH_ImageSourceNative *source = nullptr;
78    errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source);
79    if (errCode != IMAGE_SUCCESS) {
80        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode);
81        return getJsResult(env, errCode);
82    }
83
84    // 创建定义图片信息的结构体对象,并获取图片信息。
85    OH_ImageSource_Info *imageInfo;
86    OH_ImageSourceInfo_Create(&imageInfo);
87    errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo);
88    if (errCode != IMAGE_SUCCESS) {
89        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", errCode);
90        return getJsResult(env, errCode);
91    }
92
93    // 获取指定属性键的值。
94    uint32_t width, height;
95    OH_ImageSourceInfo_GetWidth(imageInfo, &width);
96    OH_ImageSourceInfo_GetHeight(imageInfo, &height);
97    OH_ImageSourceInfo_Release(imageInfo);
98    OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo success, width: %{public}d, height: %{public}d.", width, height);
99    Image_String getKey;
100    const std::string PIXEL_X_DIMENSION = "PixelXDimension";
101    getKey.data = (char *)PIXEL_X_DIMENSION.c_str();
102    getKey.size = PIXEL_X_DIMENSION.length();
103    Image_String getValue;
104    errCode = OH_ImageSourceNative_GetImageProperty(source, &getKey, &getValue);
105    if (errCode != IMAGE_SUCCESS) {
106        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageProperty failed, errCode: %{public}d.", errCode);
107        return getJsResult(env, errCode);
108    }
109
110    // 修改指定属性键的值。
111    Image_String setKey;
112    const std::string ORIENTATION = "Orientation";
113    setKey.data = (char *)ORIENTATION.c_str();
114    setKey.size = ORIENTATION.length();
115    Image_String setValue;
116    setValue.data = (char *)"4";
117    setValue.size = 1;
118    errCode = OH_ImageSourceNative_ModifyImageProperty(source, &setKey, &setValue);
119    if (errCode != IMAGE_SUCCESS) {
120        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_ModifyImageProperty failed, errCode: %{public}d.", errCode);
121        return getJsResult(env, errCode);
122    }
123
124    // 通过图片解码参数创建PixelMap对象。
125    OH_DecodingOptions *ops = nullptr;
126    OH_DecodingOptions_Create(&ops);
127    // 设置为AUTO会根据图片资源格式解码,如果图片资源为HDR资源则会解码为HDR的pixelmap。
128    OH_DecodingOptions_SetDesiredDynamicRange(ops, IMAGE_DYNAMIC_RANGE_AUTO);
129    OH_PixelmapNative *resPixMap = nullptr;
130
131    // ops参数支持传入nullptr, 当不需要设置解码参数时,不用创建。
132    errCode = OH_ImageSourceNative_CreatePixelmap(source, ops, &resPixMap);
133    OH_DecodingOptions_Release(ops);
134    if (errCode != IMAGE_SUCCESS) {
135        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmap failed, errCode: %{public}d.", errCode);
136        return getJsResult(env, errCode);
137    }
138
139    // 判断pixelmap是否为hdr内容。
140    OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr;
141    OH_PixelmapImageInfo_Create(&pixelmapImageInfo);
142    OH_PixelmapNative_GetImageInfo(resPixMap, pixelmapImageInfo);
143    bool pixelmapIsHdr;
144    OH_PixelmapImageInfo_GetDynamicRange(pixelmapImageInfo, &pixelmapIsHdr);
145    OH_PixelmapImageInfo_Release(pixelmapImageInfo);
146
147    // 获取图像帧数。
148    uint32_t frameCnt = 0;
149    errCode = OH_ImageSourceNative_GetFrameCount(source, &frameCnt);
150    if (errCode != IMAGE_SUCCESS) {
151        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetFrameCount failed, errCode: %{public}d.", errCode);
152        return getJsResult(env, errCode);
153    }
154
155    // 通过图片解码参数创建Pixelmap列表。
156    OH_DecodingOptions *opts = nullptr;
157    OH_DecodingOptions_Create(&opts);
158    OH_PixelmapNative **resVecPixMap = new OH_PixelmapNative*[frameCnt];
159    size_t outSize = frameCnt;
160    errCode = OH_ImageSourceNative_CreatePixelmapList(source, opts, resVecPixMap, outSize);
161    OH_DecodingOptions_Release(opts);
162    delete[] resVecPixMap;
163    if (errCode != IMAGE_SUCCESS) {
164        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmapList failed, errCode: %{public}d.", errCode);
165        return getJsResult(env, errCode);
166    }
167
168    // 获取图像延迟时间列表。
169    int32_t *delayTimeList = new int32_t[frameCnt];
170    size_t size = frameCnt;
171    errCode = OH_ImageSourceNative_GetDelayTimeList(source, delayTimeList, size);
172    delete[] delayTimeList;
173    if (errCode != IMAGE_SUCCESS) {
174        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetDelayTimeList failed, errCode: %{public}d.", errCode);
175        return getJsResult(env, errCode);
176    }
177
178    // 释放ImageSource实例。
179    OH_ImageSourceNative_Release(source);
180    OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest success.");
181    return getJsResult(env, IMAGE_SUCCESS);
182}
183```
184