• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi/native_api.h"
17 
18 // [Start ndk_pixelmap_js_include_module_in_decode]
19 // 引入图片框架、raw文件、raw文件管理和日志打印头文件
20 #include <cstdlib>
21 #include <cstring>
22 #include <multimedia/image_framework/image_pixel_map_napi.h>
23 #include <multimedia/image_framework/image_source_mdk.h>
24 #include <multimedia/image_framework/image_pixel_map_mdk.h>
25 #include <rawfile/raw_file.h>
26 #include <rawfile/raw_file_manager.h>
27 #include <hilog/log.h>
28 // [End ndk_pixelmap_js_include_module_in_decode]
29 // [Start ndk_pixelmap_js_include_module_in_pixelmap_operation]
30 // [Start ndk_pixelmap_js_include_module_in_image_transformation]
31 #include <multimedia/image_framework/image_mdk_common.h>
32 // [End ndk_pixelmap_js_include_module_in_image_transformation]
33 #include <cstdlib>
34 // [End ndk_pixelmap_js_include_module_in_pixelmap_operation]
35 #include <fcntl.h>
36 #include <unistd.h>
37 
38 // [Start ndk_pixelmap_js_include_module_in_encode]
39 // 引入编码器image_packer_mdk.h头文件。
40 #include "multimedia/image_framework/image_packer_mdk.h"
41 // [End ndk_pixelmap_js_include_module_in_encode]
42 
43 const uint32_t BUFFER_MAX = 1024 * 1024;
44 const uint16_t BUFFER_SIZE = 2048;
45 const uint16_t OPTS_QUALITY = 100;
46 const uint16_t RESPONSE_SIZE = 100;
47 const uint16_t OPS_WIDTH = 500;
48 const uint16_t OPS_HEIGHT = 500;
49 const uint16_t OPS_PIXEL_FORMAT = 4;
50 const uint16_t OPS_ALPHA_TYPE = 0;
51 
52 // [Start ndk_pixelmap_js_get_sync_pixelmap]
getSyncPixelMap(napi_env env,napi_callback_info info)53 static napi_value getSyncPixelMap(napi_env env, napi_callback_info info)
54 {
55     size_t argc = 2;
56     napi_value args[2] = {nullptr};
57 
58     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
59 
60     napi_valuetype srcType;
61     napi_typeof(env, args[0], &srcType);
62 
63     // 入参args[0]是资源管理器,用来初始化native层的资源管理器
64     NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, args[0]);
65 
66     size_t strSize;
67     char srcBuf[BUFFER_SIZE];
68     // 入参args[1]是文件名称
69     napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize);
70 
71     // 用资源管理器打开Raw文件
72     RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf);
73     if (rawFile != NULL) {
74         // 获取文件大小,并读取数据
75         long len = OH_ResourceManager_GetRawFileSize(rawFile);
76         uint8_t *data = nullptr;
77         if (len > 0 && len <= BUFFER_MAX) {
78             data = static_cast<uint8_t *>(malloc(len));
79         }
80         int res = OH_ResourceManager_ReadRawFile(rawFile, data, len);
81 
82         OhosImageSource imageSource_c;
83         imageSource_c.buffer = data;
84         imageSource_c.bufferSize = len;
85 
86         OhosImageSourceOps ops{};
87         napi_value imageSource;
88         napi_value pixelMap;
89 
90         // 用读取到的Raw数据创建ImageSource
91         int32_t ret = OH_ImageSource_Create(env, &imageSource_c, &ops, &imageSource);
92 
93         // 初始化native层的ImageSource
94         ImageSourceNative *imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource);
95         OhosImageDecodingOps decodingOps{};
96         // 创建pixelMap
97         OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap);
98 
99         OhosImageSourceInfo info{};
100         // 读取图片宽高
101         OH_ImageSource_GetImageInfo(imageSourceNative_c, 0, &info);
102         OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[decode]", "imageInfo width:%{public}d , height:%{public}d",
103                      info.size.width, info.size.height);
104 
105         // 读取图片源的ImageWidth配置参数并打印日志
106         OhosImageSourceProperty target;
107         const char* exifKeyC = "ImageWidth";
108         target.size = strlen(exifKeyC);
109         target.value = const_cast<char*>(exifKeyC);
110 
111         OhosImageSourceProperty response{};
112         response.size = RESPONSE_SIZE;
113         response.value = static_cast<char *>(malloc(RESPONSE_SIZE));
114         OH_ImageSource_GetImageProperty(imageSourceNative_c, &target, &response);
115         OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[decode]", "ImageProperty width after modify:%{public}s",
116                      response.value);
117 
118         // 处理完毕,释放native层资源
119         OH_ImageSource_Release(imageSourceNative_c);
120         OH_ResourceManager_CloseRawFile(rawFile);
121         return pixelMap;
122     }
123     OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
124     return nullptr;
125 }
126 // [End ndk_pixelmap_js_get_sync_pixelmap]
127 
DecodeIncrementally(napi_env env,long len,RawFile * rawFile)128 static napi_value DecodeIncrementally(napi_env env, long len, RawFile *rawFile)
129 {
130     uint8_t *data = nullptr;
131     if (len > 0 && len <= BUFFER_MAX) {
132         data = static_cast<uint8_t *>(malloc(len));
133     }
134     // 读取文件全部数据
135     int res = OH_ResourceManager_ReadRawFile(rawFile, data, len);
136 
137     uint8_t *holderdata = static_cast<uint8_t *>(malloc(len));
138     if (holderdata == nullptr) {
139         return nullptr;
140     }
141 
142     OhosImageSource imageSource_c;
143     // imageSource_c的buffer分配了空间,但是数据是空的
144     imageSource_c.buffer = holderdata;
145     imageSource_c.bufferSize = len;
146     OhosImageSourceOps ops{};
147     napi_value imageSource;
148     // 初始化增量ImageSource
149     OH_ImageSource_CreateIncremental(env, &imageSource_c, &ops, &imageSource);
150 
151     // 初始化native层的ImageSource
152     ImageSourceNative *imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource);
153 
154     // 以下模拟分片加载场景,分两次加载分片。第一次加载2048字节,第二次加载剩余的数据。
155     OhosImageSourceUpdateData firstData{};
156     firstData.buffer = data;    // 图片数据
157     firstData.bufferSize = len; // 图片数据总大小
158     firstData.isCompleted = false;
159     firstData.offset = 0;                 // 第一次重头开始加载
160     firstData.updateLength = BUFFER_SIZE; // 第一次加载了2048字节
161     OH_ImageSource_UpdateData(imageSourceNative_c, &firstData);
162 
163     OhosImageSourceUpdateData secondData{};
164     secondData.buffer = data;
165     secondData.bufferSize = len;
166     secondData.isCompleted = true;   // 最后一次加载,要标记加载完成
167     secondData.offset = BUFFER_SIZE; // 已经加载过2048字节了,第二次偏移已经加载的量
168     secondData.updateLength = len - BUFFER_SIZE; // 第二次加载剩余的数据
169     OH_ImageSource_UpdateData(imageSourceNative_c, &secondData);
170 
171     napi_value pixelMap;
172     OhosImageDecodingOps decodingOps{};
173     decodingOps.index = 0;
174     // 创建pixelMap
175     OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap);
176 
177     // 处理完毕,释放native层资源
178     OH_ImageSource_Release(imageSourceNative_c);
179     OH_ResourceManager_CloseRawFile(rawFile);
180     return pixelMap;
181 }
182 
183 // [Start ndk_pixelmap_js_get_sync_pixelmap_incremental]
getSyncPixelMapIncremental(napi_env env,napi_callback_info info)184 static napi_value getSyncPixelMapIncremental(napi_env env, napi_callback_info info)
185 {
186     size_t argc = 2;
187     napi_value args[2] = {nullptr};
188 
189     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
190 
191     napi_valuetype srcType;
192     napi_typeof(env, args[0], &srcType);
193 
194     // 入参args[0]是资源管理器,用来初始化native层的资源管理器
195     NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, args[0]);
196 
197     size_t strSize;
198     char srcBuf[2048];
199     // 入参args[1]是文件名称
200     napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize);
201 
202     // 用资源管理器打开Raw文件
203     RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf);
204     if (rawFile != NULL) {
205         // 获取文件大小,若大于2048字节,则增量式解码,否则直接全部解码
206         long len = OH_ResourceManager_GetRawFileSize(rawFile);
207         if (len > BUFFER_SIZE) {
208             return DecodeIncrementally(env, len, rawFile);
209         }
210         // 读取Raw文件全部数据
211         uint8_t *data = static_cast<uint8_t *>(malloc(len));
212         int res = OH_ResourceManager_ReadRawFile(rawFile, data, len);
213 
214         OhosImageSource imageSource_c;
215         imageSource_c.buffer = data;
216         imageSource_c.bufferSize = len;
217 
218         OhosImageSourceOps ops{};
219         napi_value imageSource;
220         napi_value pixelMap;
221 
222         // 用读取到的Raw数据创建ImageSource
223         int32_t ret = OH_ImageSource_Create(env, &imageSource_c, &ops, &imageSource);
224 
225         // 初始化native层的ImageSource
226         ImageSourceNative *imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource);
227         OhosImageDecodingOps decodingOps{};
228 
229         // 创建pixelMap
230         OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap);
231 
232         // 处理完毕,释放native层资源
233         OH_ImageSource_Release(imageSourceNative_c);
234         OH_ResourceManager_CloseRawFile(rawFile);
235         return pixelMap;
236     }
237     OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
238     return nullptr;
239 }
240 // [End ndk_pixelmap_js_get_sync_pixelmap_incremental]
241 // [Start ndk_pixelmap_js_test_get_image_info]
TestGetImageInfo(napi_env env,napi_callback_info info)242 static napi_value TestGetImageInfo(napi_env env, napi_callback_info info)
243 {
244     napi_value result = nullptr;
245     napi_get_undefined(env, &result);
246 
247     napi_value thisVar = nullptr;
248     napi_value argValue[1] = {0};
249     size_t argCount = 1;
250 
251     napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
252 
253     OHOS::Media::OhosPixelMapInfo pixelMapInfo;
254     OHOS::Media::OH_GetImageInfo(env, argValue[0], &pixelMapInfo);
255     return result;
256 }
257 // [End ndk_pixelmap_js_test_get_image_info]
258 // [Start ndk_pixelmap_js_test_access_pixels]
TestAccessPixels(napi_env env,napi_callback_info info)259 static napi_value TestAccessPixels(napi_env env, napi_callback_info info)
260 {
261     napi_value result = nullptr;
262     napi_get_undefined(env, &result);
263 
264     napi_value thisVar = nullptr;
265     napi_value argValue[1] = {0};
266     size_t argCount = 1;
267 
268     napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
269 
270     void *addrPtr = nullptr;
271     OHOS::Media::OH_AccessPixels(env, argValue[0], &addrPtr);
272     return result;
273 }
274 // [End ndk_pixelmap_js_test_access_pixels]
275 // [Start ndk_pixelmap_js_test_un_access_pixels]
TestUnAccessPixels(napi_env env,napi_callback_info info)276 static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info)
277 {
278     napi_value result = nullptr;
279     napi_get_undefined(env, &result);
280 
281     napi_value thisVar = nullptr;
282     napi_value argValue[1] = {0};
283     size_t argCount = 1;
284 
285     napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
286 
287     OHOS::Media::OH_UnAccessPixels(env, argValue[0]);
288     return result;
289 }
290 // [End ndk_pixelmap_js_test_un_access_pixels]
291 // [Start ndk_pixelmap_js_create_pixelmap_test]
CreatePixelMapTest(napi_env env,napi_callback_info info)292 napi_value CreatePixelMapTest(napi_env env, napi_callback_info info)
293 {
294     napi_value udfVar = nullptr;
295     napi_value pixelMap = nullptr;
296 
297     struct OhosPixelMapCreateOps createOps;
298     createOps.width = OPS_WIDTH; // 修改创建设置中的宽高为500,使图像易于展示
299     createOps.height = OPS_HEIGHT;
300     createOps.pixelFormat = OPS_PIXEL_FORMAT;
301     createOps.alphaType = OPS_ALPHA_TYPE;
302     size_t bufferSize = createOps.width * createOps.height * 4;
303     void *buff = malloc(bufferSize);
304     if (buff == nullptr) {
305         return udfVar;
306     }
307 
308     char *cc = (char *)buff;
309     for (int i = 0; i < bufferSize; i++) { // 处理全部的像素点,使图像易于展示
310         *(cc++) = static_cast<char>(i);
311     }
312 
313     int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, (uint8_t *)buff, bufferSize, &pixelMap);
314     free(buff);
315     if (res != IMAGE_RESULT_SUCCESS || pixelMap == nullptr) {
316         return udfVar;
317     }
318     return pixelMap;
319 }
320 // [End ndk_pixelmap_js_create_pixelmap_test]
321 // [Start ndk_pixelmap_js_create_alpha_pixelmap]
CreateAlphaPixelMap(napi_env env,napi_callback_info info)322 napi_value CreateAlphaPixelMap(napi_env env, napi_callback_info info)
323 {
324     napi_value udfVar = nullptr;
325     napi_value thisVar = nullptr;
326     napi_value argValue[1] = {0};
327     size_t argCount = 1;
328 
329     napi_value alphaPixelMap = nullptr;
330 
331     napi_get_undefined(env, &udfVar);
332 
333     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
334         argValue[0] == nullptr) {
335         return udfVar;
336     }
337     int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[0], &alphaPixelMap);
338     if (res != IMAGE_RESULT_SUCCESS || alphaPixelMap == nullptr) {
339         return udfVar;
340     }
341     return alphaPixelMap;
342 }
343 // [End ndk_pixelmap_js_create_alpha_pixelmap]
344 // [Start ndk_pixelmap_js_transform]
Transform(napi_env env,napi_callback_info info)345 napi_value Transform(napi_env env, napi_callback_info info)
346 {
347     napi_value thisVar = nullptr;
348     napi_value argValue[1] = {0};
349     size_t argCount = 1;
350 
351     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
352         argValue[0] == nullptr) {
353         return nullptr;
354     }
355     napi_value result = nullptr;
356     napi_get_undefined(env, &result);
357 
358     // 初始化NativePixelMap对象。
359     NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue[0]);
360 
361     // 获取图片信息。
362     struct OhosPixelMapInfos pixelMapInfo;
363     OH_PixelMap_GetImageInfo(native, &pixelMapInfo);
364 
365     // 获取PixelMap对象每行字节数。
366     int32_t rowBytes;
367     OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes);
368 
369     // 获取PixelMap对象是否可编辑的状态。
370     int32_t editable = 0;
371     OH_PixelMap_GetIsEditable(native, &editable);
372 
373     // 获取PixelMap对象是否支持Alpha通道。
374     int32_t supportAlpha = 0;
375     OH_PixelMap_IsSupportAlpha(native, &supportAlpha);
376 
377     // 设置PixelMap对象的Alpha通道。
378     int32_t alphaAble = 0;
379     OH_PixelMap_SetAlphaAble(native, alphaAble);
380 
381     // 获取PixelMap对象像素密度。
382     int32_t densityG;
383     OH_PixelMap_GetDensity(native, &densityG);
384 
385     // 设置PixelMap对象像素密度。
386     int32_t densityS = 100;
387     OH_PixelMap_SetDensity(native, densityS);
388 
389     // 设置PixelMap对象的透明度。
390     float opacity = 0.5;
391     OH_PixelMap_SetOpacity(native, opacity);
392 
393     // 设置缩放比例。
394     // scaleX: 宽为原来的0.5。
395     // scaleY: 高为原来的0.5。
396     float scaleX = 0.5;
397     float scaleY = 0.5;
398     OH_PixelMap_Scale(native, scaleX, scaleY);
399 
400     // 设置偏移。
401     // translateX: 向下偏移50。
402     // translateY: 向右偏移50。
403     float translateX = 50;
404     float translateY = 50;
405     OH_PixelMap_Translate(native, translateX, translateY);
406 
407     // 设置顺时针旋转90度。
408     float angle = 90;
409     OH_PixelMap_Rotate(native, angle);
410 
411     // 设置翻转
412     // flipX: 水平翻转,0为不翻转,1为翻转。
413     // flipY: 垂直翻转,0为不翻转,1为翻转。
414     int32_t flipX = 0;
415     int32_t flipY = 1;
416     OH_PixelMap_Flip(native, flipX, flipY);
417 
418     // 设置裁剪区域。
419     // cropX: 裁剪起始点横坐标。
420     // cropY: 裁剪起始点纵坐标。
421     // cropH: 裁剪高度10,方向为从上往下(裁剪后的图片高度为10)。
422     // cropW: 裁剪宽度10,方向为从左到右(裁剪后的图片宽度为10)。
423     int32_t cropX = 1;
424     int32_t cropY = 1;
425     int32_t cropW = 100; // 裁剪后的宽高修改为100,使图像易于展示
426     int32_t cropH = 100;
427     OH_PixelMap_Crop(native, cropX, cropY, cropW, cropH);
428 
429     // 获取PixelMap对象数据的内存地址,并锁定该内存。
430     void *pixelAddr = nullptr;
431     OH_PixelMap_AccessPixels(native, &pixelAddr);
432 
433     // 释放PixelMap对象数据的内存锁。
434     OH_PixelMap_UnAccessPixels(native);
435 
436     return result;
437 }
438 // [End ndk_pixelmap_js_transform]
439 
EncodeImagesToFile(napi_env env,napi_callback_info info)440 napi_value EncodeImagesToFile(napi_env env, napi_callback_info info)
441 {
442     napi_value thisVar = nullptr;
443     napi_value argValue[1] = {0};
444     size_t argCount = 1;
445 
446     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
447         argValue[0] == nullptr) {
448         return nullptr;
449     }
450 
451     napi_value source = argValue[0];
452 
453     if (source == nullptr) {
454         return nullptr;
455     }
456     // [Start ndk_pixelmap_create_native_packer]
457     // 使用napi_value 承接创建的编码器对象
458     napi_value packer;
459     // 通过 napi_env 创建编码器,返回result为 IMAGE_RESULT_SUCCESS则创建成功
460     int32_t result = OH_ImagePacker_Create(env, &packer);
461     // [End ndk_pixelmap_create_native_packer]
462 
463     // [Start ndk_pixelmap_init_native_packer]
464     // 通过 napi_env 及上述创建的编码器对象初始化原生实例对象
465     ImagePacker_Native *nativePacker = OH_ImagePacker_InitNative(env, packer);
466     // [End ndk_pixelmap_init_native_packer]
467 
468     // [Start ndk_pixelmap_js_decode_to_file]
469     // 编码参数
470     struct ImagePacker_Opts_ opts;
471     // 配置编码格式(必须)
472     opts.format = "image/jpeg";
473     // 配置编码质量(必须)
474     opts.quality = OPTS_QUALITY;
475     // 打开需要输出的文件(请确保应用有权限访问这个路径)
476     // 修改路径为可访问的沙箱路径
477     int fd = open("/data/storage/el2/base/haps/entry/cache/test.jpg", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
478     OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[encode]", "EncodeImagesToFile fd:%{public}d", fd);
479     if (fd >= 0) {
480         // 开始对输入source进行编码过程,返回result为 IMAGE_RESULT_SUCCESS则编码成功
481         int32_t res = OH_ImagePacker_PackToFile(nativePacker, source, &opts, fd);
482         if (res != IMAGE_RESULT_SUCCESS) {
483             OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "[encode]", "EncodeToFile failed, errCode: %{public}d", result);
484         } else {
485             OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[encode]", "EncodeToFile success, result: %{public}d", result);
486         }
487         // 关闭输出文件
488         close(fd);
489     }
490     // [End ndk_pixelmap_js_decode_to_file]
491 
492     // [Start ndk_pixelmap_release_native_packer]
493     // 调用OH_ImagePacker_Release, 销毁编码器
494     int32_t ret = OH_ImagePacker_Release(nativePacker);
495     if (ret != IMAGE_RESULT_SUCCESS) {
496         // 异常处理
497     } else {
498         nativePacker = NULL; // 不可重复destroy
499     }
500     // [End ndk_pixelmap_release_native_packer]
501     return nullptr;
502 }
503 
EncodeImagesToBuffer(napi_env env,napi_callback_info info)504 napi_value EncodeImagesToBuffer(napi_env env, napi_callback_info info)
505 {
506     napi_value thisVar = nullptr;
507     napi_value argValue[1] = {0};
508     size_t argCount = 1;
509 
510     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
511         argValue[0] == nullptr) {
512         return nullptr;
513     }
514 
515     napi_value source;
516     napi_create_object(env, &source);
517     if (source == nullptr) {
518         return nullptr;
519     }
520 
521     // 使用napi_value 承接创建的编码器对象
522     napi_value packer;
523     // 通过 napi_env 创建编码器,返回result为 IMAGE_RESULT_SUCCESS则创建成功
524     int32_t resultCreate = OH_ImagePacker_Create(env, &packer);
525 
526     // 通过 napi_env 及上述创建的编码器对象初始化原生实例对象
527     ImagePacker_Native *nativePacker = OH_ImagePacker_InitNative(env, packer);
528 
529     // [Start ndk_pixelmap_js_decode_to_buffer]
530     // 编码参数
531     struct ImagePacker_Opts_ opts;
532     // 配置编码格式(必须)
533     opts.format = "image/jpeg";
534     // 配置编码质量(必须)
535     opts.quality = OPTS_QUALITY;
536     // 配置输出的缓存区大小为4k(缓存区大小视应用场景定)
537     size_t bufferSize = 4 * 1024;
538     // 申请图片编码缓存区
539     uint8_t *outData = (uint8_t *)(malloc(bufferSize));
540     // 开始对输入source进行编码过程,返回result为
541     // IMAGE_RESULT_SUCCESS则编码成功,同时bufferSize中包含编码实际使用缓存区大小
542     int32_t result = OH_ImagePacker_PackToData(nativePacker, source, &opts, outData, &bufferSize);
543     // [End ndk_pixelmap_js_decode_to_buffer]
544 
545     // 调用OH_ImagePacker_Release, 销毁编码器
546     int32_t ret = OH_ImagePacker_Release(nativePacker);
547     if (result != IMAGE_RESULT_SUCCESS) {
548         // 异常处理
549     } else {
550         nativePacker = NULL; // 不可重复destroy
551     }
552     return nullptr;
553 }
554