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.so。 16 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.cpp或napi_init.cpp,本示例以hello.cpp文件名为例)。在hello.cpp中实现C API接口调用逻辑,示例代码如下: 26 27**编码接口使用示例** 28 29在创建ImagePacker实例,指定编码参数后将ImageSource或Pixelmap编码至文件或者缓冲区。 30 31> **说明:** 32> 根据MIME标准,标准编码格式为image/jpeg。当使用image编码时,编码参数中的编码格式image_MimeType设置为image/jpeg,image编码后的文件扩展名可设为.jpg或.jpeg,可在支持image/jpeg解码的平台上使用。 33 34```c++ 35#include <string> 36#include <set> 37#include <hilog/log.h> 38#include <multimedia/image_framework/image/image_common.h> 39#include <multimedia/image_framework/image/image_packer_native.h> 40#include <multimedia/image_framework/image/pixelmap_native.h> 41#include <multimedia/image_framework/image/image_source_native.h> 42 43#undef LOG_DOMAIN 44#undef LOG_TAG 45#define LOG_DOMAIN 0x3200 46#define LOG_TAG "MY_TAG" 47 48static std::set<std::string> g_encodeSupportedFormats; 49 50Image_MimeType GetMimeTypeIfEncodable(const char *format) 51{ 52 auto it = g_encodeSupportedFormats.find(format); 53 if (it == g_encodeSupportedFormats.end()) { 54 return {const_cast<char *>(""), 0}; 55 } 56 return {const_cast<char *>(format), strlen(format)}; 57} 58 59Image_ErrorCode packToFileFromImageSourceTest(int fd) 60{ 61 //创建ImagePacker实例。 62 OH_ImagePackerNative *testPacker = nullptr; 63 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 64 if (errCode != IMAGE_SUCCESS) { 65 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 66 return errCode; 67 } 68 // 获取编码能力范围。 69 Image_MimeType* mimeType = nullptr; 70 size_t length = 0; 71 errCode = OH_ImagePackerNative_GetSupportedFormats(&mimeType, &length); 72 if (errCode != IMAGE_SUCCESS) { 73 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_GetSupportedFormats failed, errCode: %{public}d.", errCode); 74 return errCode; 75 } 76 for (size_t count = 0; count < length; count++) { 77 OH_LOG_INFO(LOG_APP, "Encode supportedFormats:%{public}s", mimeType[count].data); 78 if (mimeType[count].data != nullptr) { 79 g_encodeSupportedFormats.insert(std::string(mimeType[count].data)); 80 } 81 } 82 // 创建ImageSource实例。 83 OH_ImageSourceNative* imageSource = nullptr; 84 errCode = OH_ImageSourceNative_CreateFromFd(fd, &imageSource); 85 if (errCode != IMAGE_SUCCESS) { 86 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImageSourceNative_CreateFromFd failed, errCode: %{public}d.", errCode); 87 return errCode; 88 } 89 90 // 指定编码参数,将ImageSource直接编码进文件。 91 OH_PackingOptions *option = nullptr; 92 OH_PackingOptions_Create(&option); 93 Image_MimeType image_MimeType = GetMimeTypeIfEncodable(MIME_TYPE_JPEG); 94 if (image_MimeType.data == nullptr || image_MimeType.size == 0) { 95 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest GetMimeTypeIfEncodable failed, format can't support encode."); 96 return IMAGE_BAD_PARAMETER; 97 } 98 OH_PackingOptions_SetMimeType(option, &image_MimeType); 99 // 编码为hdr内容(需要资源本身为hdr,支持jpeg格式)。 100 OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO); 101 errCode = OH_ImagePackerNative_PackToFileFromImageSource(testPacker, option, imageSource, fd); 102 if (errCode != IMAGE_SUCCESS) { 103 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromImageSource failed, errCode: %{public}d.", errCode); 104 return errCode; 105 } 106 107 // 释放ImagePacker实例。 108 errCode = OH_ImagePackerNative_Release(testPacker); 109 if (errCode != IMAGE_SUCCESS) 110 { 111 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 112 return errCode; 113 } 114 // 释放ImageSource实例。 115 errCode = OH_ImageSourceNative_Release(imageSource); 116 if (errCode != IMAGE_SUCCESS) 117 { 118 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImageSourceNative_Release failed, errCode: %{public}d.", errCode); 119 return errCode; 120 } 121 122 return IMAGE_SUCCESS; 123} 124 125Image_ErrorCode packToFileFromPixelmapTest(uint8_t *buffer, size_t bufferSize, int fd) 126{ 127 // 创建ImagePacker实例。 128 OH_ImagePackerNative *testPacker = nullptr; 129 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 130 if (errCode != IMAGE_SUCCESS) { 131 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 132 return errCode; 133 } 134 135 // 创建Pixelmap实例。 136 OH_Pixelmap_InitializationOptions *createOpts; 137 OH_PixelmapInitializationOptions_Create(&createOpts); 138 OH_PixelmapInitializationOptions_SetWidth(createOpts, 6); 139 OH_PixelmapInitializationOptions_SetHeight(createOpts, 4); 140 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, 3); 141 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, 0); 142 OH_PixelmapNative *pixelmap = nullptr; 143 errCode = OH_PixelmapNative_CreatePixelmap(buffer, bufferSize, createOpts, &pixelmap); 144 if (errCode != IMAGE_SUCCESS) { 145 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_PixelmapNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 146 return errCode; 147 } 148 149 // 指定编码参数,将PixelMap直接编码进文件。 150 OH_PackingOptions *option = nullptr; 151 OH_PackingOptions_Create(&option); 152 char type[] = "image/jpeg"; 153 Image_MimeType image_MimeType = {type, strlen(type)}; 154 OH_PackingOptions_SetMimeType(option, &image_MimeType); 155 errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd); 156 if (errCode != IMAGE_SUCCESS) { 157 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromPixelmap failed, errCode: %{public}d.", errCode); 158 return errCode; 159 } 160 161 // 释放ImagePacker实例。 162 errCode = OH_ImagePackerNative_Release(testPacker); 163 if (errCode != IMAGE_SUCCESS) 164 { 165 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 166 return errCode; 167 } 168 169 // 释放Pixelmap实例。 170 errCode = OH_PixelmapNative_Release(pixelmap); 171 if (errCode != IMAGE_SUCCESS) 172 { 173 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_PixelmapNative_Release failed, errCode: %{public}d.", errCode); 174 return errCode; 175 } 176 177 return IMAGE_SUCCESS; 178} 179``` 180