• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Image_NativeModule to Edit EXIF Data
2<!--Kit: Image Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @aulight02-->
5<!--SE: @liyang_bryan-->
6<!--TSE: @xchaosioda-->
7
8Image Kit provides the capabilities of reading and editing Exchangeable Image File Format (EXIF) data.
9
10EXIF is a file format dedicated for photos taken by digital cameras and is used to record attributes and shooting data of the photos. Currently, JPEG, PNG, and HEIF images that contain EXIF data are supported.
11
12Users may need to view or modify the EXIF data of photos in the Gallery application, for example, when the manual lens parameters of the camera are not automatically written as part of the EXIF data or the shooting time is incorrect due to camera power-off.
13
14Currently, OpenHarmony allows you to view and modify part of EXIF data. For details, see [OHOS_IMAGE_PROPERTY_XXX](../../reference/apis-image-kit/capi-image-common-h.md#variables).
15
16## How to Develop
17
18### Adding a Link Library
19
20Open the **src/main/cpp/CMakeLists.txt** file of the native project, add **libimage_source.so** and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency.
21
22```txt
23target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so)
24```
25
26### Calling the Native APIs
27
28For details about the C APIs for reading and editing EXIF data, see [OH_ImageSource_GetImageProperty](../../reference/apis-image-kit/capi-image-source-native-h.md#oh_imagesourcenative_getimageproperty) and [OH_ImageSource_ModifyImageProperty](../../reference/apis-image-kit/capi-image-source-native-h.md#oh_imagesourcenative_modifyimageproperty).
29
30Create 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.
31
32**Example for Reading and Editing EXIF Data**
33
34After an ImageSource instance is created, read and edit its EXIF data.
35
36```c++
37
38#include <linux/kd.h>
39#include <string>
40
41#include <hilog/log.h>
42#include <multimedia/image_framework/image/image_source_native.h>
43
44#undef LOG_DOMAIN
45#undef LOG_TAG
46#define LOG_DOMAIN 0x3200
47#define LOG_TAG "MY_TAG"
48
49#define NUM_0 0
50#define NUM_1 1
51
52// Process the NAPI return value.
53napi_value getJsResult(napi_env env, int result) {
54    napi_value resultNapi = nullptr;
55    napi_create_int32(env, result, &resultNapi);
56    return resultNapi;
57}
58
59static napi_value exifTest(napi_env env, napi_callback_info info)
60{
61    napi_value argValue[NUM_1] = {0};
62    size_t argCount = NUM_1;
63    if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
64        argValue[NUM_0] == nullptr) {
65        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest exifTest napi_get_cb_info failed, argCount: %{public}d.", argCount);
66        return getJsResult(env, IMAGE_BAD_PARAMETER);
67    }
68    char name[1024];
69    size_t nameSize = 1024;
70    napi_get_value_string_utf8(env, argValue[NUM_0], name, 1024, &nameSize);
71
72    // Create an ImageSource instance.
73    OH_ImageSourceNative *source = nullptr;
74    Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source);
75    if (errCode != IMAGE_SUCCESS) {
76        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest exifTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode);
77        return getJsResult(env, errCode);
78    }
79
80    // Read the EXIF data, where OHOS_IMAGE_PROPERTY_BITS_PER_SAMPLE indicates the number of bits per pixel.
81    Image_String getKey;
82    const std::string PIXEL_X_DIMENSION = OHOS_IMAGE_PROPERTY_BITS_PER_SAMPLE;
83    getKey.data = (char *)PIXEL_X_DIMENSION.c_str();
84    getKey.size = PIXEL_X_DIMENSION.length();
85    Image_String getValue;
86    errCode = OH_ImageSourceNative_GetImageProperty(source, &getKey, &getValue);
87    if (errCode != IMAGE_SUCCESS) {
88        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest exifTest OH_ImageSourceNative_GetImageProperty failed, errCode: %{public}d.", errCode);
89        return getJsResult(env, errCode);
90    }
91
92    // Edit the EXIF data, where OHOS_IMAGE_PROPERTY_ORIENTATION indicates the image orientation.
93    Image_String setKey;
94    const std::string ORIENTATION = OHOS_IMAGE_PROPERTY_ORIENTATION;
95    setKey.data = (char *)ORIENTATION.c_str();
96    setKey.size = ORIENTATION.length();
97    Image_String setValue;
98    setValue.data = (char *)"4";
99    setValue.size = 1;
100    errCode = OH_ImageSourceNative_ModifyImageProperty(source, &setKey, &setValue);
101    if (errCode != IMAGE_SUCCESS) {
102        OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest exifTest OH_ImageSourceNative_ModifyImageProperty failed, errCode: %{public}d.", errCode);
103        return getJsResult(env, errCode);
104    }
105
106    // Release the ImageSource instance.
107    OH_ImageSourceNative_Release(source);
108    OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest exifTest success.");
109    return getJsResult(env, IMAGE_SUCCESS);
110}
111```
112