• 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图像编码类,用于创建以及释放ImagePacker实例,并编码多图对象。
10
11## 开发步骤
12
13### 添加链接库
14
15在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加libimage_packer.so 以及日志依赖libhilog_ndk.z.so16
17```txt
18target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so libimage_packer.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在创建ImagePacker实例,指定编码参数后将Picture多图对象编码至文件或者缓冲区。
30
31> **说明:**
32> 根据MIME标准,标准编码格式为image/jpeg。当使用image编码时,编码参数中的编码格式image_MimeType设置为image/jpeg,image编码后的文件扩展名可设为.jpg或.jpeg,可在支持image/jpeg解码的平台上使用。
33
34```c++
35#include <hilog/log.h>
36#include <multimedia/image_framework/image/image_native.h>
37#include <multimedia/image_framework/image/image_packer_native.h>
38#include <multimedia/image_framework/image/image_source_native.h>
39#include <multimedia/image_framework/image/picture_native.h>
40#include <multimedia/image_framework/image/pixelmap_native.h>
41
42#define AUTO 0
43#define SDR 1
44
45class ImagePictureNative {
46public:
47    Image_ErrorCode errorCode = IMAGE_SUCCESS;
48    OH_DecodingOptionsForPicture *options = nullptr;
49    OH_ImagePackerNative *imagePacker = nullptr;
50    OH_PackingOptions *packerOptions = nullptr;
51    OH_PictureNative *picture = nullptr;
52    OH_ImageSourceNative *source = nullptr;
53    ImagePictureNative() {}
54    ~ImagePictureNative() {}
55};
56
57static ImagePictureNative *thisPicture = new ImagePictureNative();
58
59// 处理napi返回值。
60napi_value getJsResult(napi_env env, int result) {
61    napi_value resultNapi = nullptr;
62    napi_create_int32(env, result, &resultNapi);
63    return resultNapi;
64}
65
66// 释放ImageSource。
67Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) {
68    if (source != nullptr) {
69        thisPicture->errorCode = OH_ImageSourceNative_Release(source);
70        source = nullptr;
71        return thisPicture->errorCode;
72    }
73OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null!");
74    return IMAGE_SUCCESS;
75}
76
77// 设置编码参数。
78void SetPackOptions(OH_PackingOptions *packerOptions, Image_MimeType format, uint32_t quality, bool needsPackProperties,
79                    int32_t desiredDynamicRange) {
80    OH_PackingOptions_SetMimeType(packerOptions, &format);
81    OH_PackingOptions_SetQuality(packerOptions, quality);
82    OH_PackingOptions_SetNeedsPackProperties(packerOptions, needsPackProperties);
83    OH_PackingOptions_SetDesiredDynamicRange(packerOptions, desiredDynamicRange);
84}
85
86// 编码PackToData。
87static napi_value PackToDataFromPicture(napi_env env, napi_callback_info info) {
88    size_t argc = 2;
89    napi_value args[2] = {nullptr};
90    if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
91        OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed!");
92        return getJsResult(env, thisPicture->errorCode);
93    }
94    uint32_t fd = 0;
95    napi_get_value_uint32(env, args[0], &fd);
96    size_t outDataSize = 10000 * 10000;
97    uint8_t *outData = new uint8_t[outDataSize];
98
99    if (thisPicture->packerOptions == nullptr) {
100        thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions);
101    }
102    if (thisPicture->imagePacker == nullptr) {
103        thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker);
104    }
105
106    char strFormat[20];
107    size_t strFormatSize;
108    napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize);
109    OH_LOG_DEBUG(LOG_APP, "PackToDataFromPicture format: %{public}s", strFormat);
110
111    Image_MimeType format;
112    format.size = strFormatSize;
113    format.data = const_cast<char *>(strFormat);
114    uint32_t quality = 98;
115    bool needsPackProperties = true;
116    int32_t desiredDynamicRange = AUTO;
117    SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange);
118
119    thisPicture->errorCode = OH_ImagePackerNative_PackToDataFromPicture(
120        thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, outData, &outDataSize);
121    if (thisPicture->errorCode != IMAGE_SUCCESS) {
122    OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture failed, errCode: %{public}d.",
123            thisPicture->errorCode);
124        delete[] outData;
125        return getJsResult(env, thisPicture->errorCode);
126    } else {
127        ReleaseImageSource(thisPicture->source);
128        OH_ImageSourceNative_CreateFromData(outData, outDataSize, &thisPicture->source);
129        OH_ImagePackerNative_PackToFileFromImageSource(thisPicture->imagePacker, thisPicture->packerOptions,
130                                                    thisPicture->source, fd);
131        ReleaseImageSource(thisPicture->source);
132    OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture success !");
133    }
134	delete[] outData;
135    return getJsResult(env, thisPicture->errorCode);
136}
137
138// 编码PackToFile。
139static napi_value PackToFileFromPicture(napi_env env, napi_callback_info info) {
140    size_t argc = 2;
141    napi_value args[2] = {nullptr};
142    if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
143    OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed!");
144        return getJsResult(env, thisPicture->errorCode);
145    }
146    uint32_t fd = 0;
147    napi_get_value_uint32(env, args[0], &fd);
148
149    if (thisPicture->packerOptions == nullptr) {
150        thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions);
151    }
152    if (thisPicture->imagePacker == nullptr) {
153        thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker);
154    }
155
156    char strFormat[20];
157    size_t strFormatSize;
158    napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize);
159OH_LOG_ERROR(LOG_APP, "PackToFileFromPicture format: %{public}s", strFormat);
160
161    Image_MimeType format;
162    format.size = strFormatSize;
163    format.data = const_cast<char *>(strFormat);
164    uint32_t quality = 98;
165    bool needsPackProperties = false;
166    int32_t desiredDynamicRange = SDR;
167    SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange);
168
169    thisPicture->errorCode = OH_ImagePackerNative_PackToFileFromPicture(
170        thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, fd);
171
172    if (thisPicture->errorCode != IMAGE_SUCCESS) {
173    OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture failed, errCode: %{public}d.",
174        thisPicture->errorCode);
175
176        return getJsResult(env, thisPicture->errorCode);
177    } else {
178    OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture success !");
179    }
180
181    return getJsResult(env, thisPicture->errorCode);
182}
183```
184