Using Image_NativeModule to Encode Images
You can use the ImagePacker class to create and release ImagePacker instances.
How to Develop
Adding a Link Library
Open the src/main/cpp/CMakeLists.txt file of the native project, add libimage_packer.so and libhilog_ndk.z.so (on which the log APIs depend) to the target_link_libraries dependency.
target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so libimage_packer.so libpixelmap.so)
Calling the Native APIs
For details about the APIs, see Image_NativeModule.
Create a native C++ application in DevEco Studio. The project created by default contains the index.ets file, and a hello.cpp or napi_init.cpp file is generated in the entry\src\main\cpp directory. In this example, the generated file is hello.cpp. Implement the C APIs in hello.cpp. Refer to the sample code below.
Example of Using the Encoding APIs
NOTE
According to the MIME protocol, the standard encoding format is image/jpeg. When the APIs provided by the image module are used for encoding, image_MimeType of the encoding parameters must be set to image/jpeg. The file name extension of the encoded image file can be .jpg or .jpeg, and the file can be used on platforms that support image/jpeg decoding.
Certain APIs are supported only in API version 20 or later. You should select an appropriate API version during development.
-
Import the required header files.
#include <string> #include <hilog/log.h> #include <multimedia/image_framework/image/image_source_native.h> #include "napi/native_api.h" #include <multimedia/image_framework/image/image_common.h> #include <multimedia/image_framework/image/pixelmap_native.h> #include <set> #include <multimedia/image_framework/image/image_packer_native.h> -
Modify the log macro definition as required.
#undef LOG_DOMAIN #undef LOG_TAG #define LOG_DOMAIN 0x3200 #define LOG_TAG "IMAGE_SAMPLE" -
Define the ImageSourceNative class.
class ImageSourceNative { public: OH_ImageSource_Info *imageInfo; OH_ImageSourceNative *source = nullptr; OH_PixelmapNative *resPixMap = nullptr; OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr; uint32_t frameCnt = 0; ImageSourceNative() {} ~ImageSourceNative() {} }; -
Create an instance of ImageSourceNative.
static ImageSourceNative *g_thisImageSource = new ImageSourceNative(); -
Define a global variable to record the encoding formats supported.
static std::set<std::string> g_encodeSupportedFormats; -
Create the GetJsResult function to process the NAPI return value.
// Process the NAPI return value. napi_value GetJsResult(napi_env env, int result) { napi_value resultNapi = nullptr; napi_create_int32(env, result, &resultNapi); return resultNapi; } -
After an ImagePacker instance is created and encoding parameters are specified, the ImageSource or PixelMap object is encoded to a file or buffer.
Image_MimeType GetMimeTypeIfEncodable(const char *format) { auto it = g_encodeSupportedFormats.find(format); if (it == g_encodeSupportedFormats.end()) { return {const_cast<char *>(""), 0}; } return {const_cast<char *>(format), strlen(format)}; } Image_ErrorCode packToFileFromImageSourceTest(int fd, OH_ImageSourceNative* imageSource) { // Create an ImagePacker instance. OH_ImagePackerNative *testPacker = nullptr; Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromImageSourceTest OH_ImagePackerNative_Create failed," "errCode: %{public}d.", errCode); return errCode; } // Obtain the encoding capability range. Image_MimeType* mimeType = nullptr; size_t length = 0; errCode = OH_ImagePackerNative_GetSupportedFormats(&mimeType, &length); if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromImageSourceTest OH_ImagePackerNative_GetSupportedFormats failed," "errCode: %{public}d.", errCode); return errCode; } for (size_t count = 0; count < length; count++) { OH_LOG_INFO(LOG_APP, "Encode supportedFormats:%{public}s", mimeType[count].data); if (mimeType[count].data != nullptr) { g_encodeSupportedFormats.insert(std::string(mimeType[count].data)); } } // Specify encoding parameters and encode the ImageSource into a file. OH_PackingOptions *option = nullptr; OH_PackingOptions_Create(&option); Image_MimeType image_MimeType = GetMimeTypeIfEncodable(MIME_TYPE_JPEG); if (image_MimeType.data == nullptr || image_MimeType.size == 0) { OH_LOG_ERROR(LOG_APP, "packToFileFromImageSourceTest GetMimeTypeIfEncodable failed," "format can't support encode."); return IMAGE_BAD_PARAMETER; } OH_PackingOptions_SetMimeType(option, &image_MimeType); // HDR encoding is possible only when the device supports HDR encoding, the image resource is an HDR image, and the format of the image resource is JPEG. OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO); // Release the ImagePacker instance. errCode = OH_ImagePackerNative_Release(testPacker); testPacker = nullptr; if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromImageSourceTest OH_ImagePackerNative_Release failed," "errCode: %{public}d.", errCode); return errCode; } // Release the PackingOptions instance. errCode = OH_PackingOptions_Release(option); option = nullptr; if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromImageSourceTest OH_PackingOptions_Release failed," "errCode: %{public}d.", errCode); return errCode; } return IMAGE_SUCCESS; } Image_ErrorCode packToFileFromPixelmapTest(int fd, OH_PixelmapNative *pixelmap) { // Create an ImagePacker instance. OH_ImagePackerNative *testPacker = nullptr; Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromPixelmapTest CreatePacker OH_ImagePackerNative_Create failed," "errCode: %{public}d.", errCode); return errCode; } // Specify encoding parameters and encode the PixelMap into a file. OH_PackingOptions *option = nullptr; OH_PackingOptions_Create(&option); char type[] = "image/jpeg"; Image_MimeType image_MimeType = {type, strlen(type)}; OH_PackingOptions_SetMimeType(option, &image_MimeType); errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd); if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromPixelmapTest OH_ImagePackerNative_PackToFileFromPixelmap failed," "errCode: %{public}d.", errCode); return errCode; } // Release the ImagePacker instance. errCode = OH_ImagePackerNative_Release(testPacker); testPacker = nullptr; if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromPixelmapTest ReleasePacker OH_ImagePackerNative_Release failed," "errCode: %{public}d.", errCode); return errCode; } // Release the PackingOptions instance. errCode = OH_PackingOptions_Release(option); option = nullptr; if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromPixelmapTest OH_PackingOptions_Release failed," "errCode: %{public}d.", errCode); return errCode; } return IMAGE_SUCCESS; } napi_value PackToFileFromImageSourceTestJs(napi_env env, napi_callback_info info) { napi_value argv[1] = {0}; size_t argc = 1; if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok) { OH_LOG_ERROR(LOG_APP, "PackToFileFromImageSourceTestJs napi_get_cb_info failed."); return nullptr; } int fd; napi_get_value_int32(env, argv[0], &fd); Image_ErrorCode errCode = packToFileFromImageSourceTest(fd, g_thisImageSource->source); if (errCode == IMAGE_SUCCESS) { OH_LOG_INFO(LOG_APP, "ImagePackerNativeCTest PackToFileFromImageSourceTestJs successfully."); } return GetJsResult(env, errCode); } napi_value PackToFileFromPixelmapTestJs(napi_env env, napi_callback_info info) { napi_value argv[1] = {0}; size_t argc = 1; if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok) { OH_LOG_ERROR(LOG_APP, "PackToFileFromImageSourceTestJs napi_get_cb_info failed."); return nullptr; } int fd; napi_get_value_int32(env, argv[0], &fd); Image_ErrorCode errCode = packToFileFromPixelmapTest(fd, g_thisImageSource->resPixMap); if (errCode != IMAGE_SUCCESS) { OH_LOG_ERROR(LOG_APP, "packToFileFromPixelmapTest failed," "errCode: %{public}d.", errCode); return GetJsResult(env, errCode); } else { OH_LOG_INFO(LOG_APP, "PackToFileFromPixelmapTestJs successfully."); } return GetJsResult(env, errCode); }