image-transformation-native.md 4.5 KB
Newer Older
G
Gloria 已提交
1 2
# Image Transformation (Native)

3
You will learn how to use native image APIs to process images.
G
Gloria 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

## How to Develop


**Adding Dependencies**

Open the **src/main/cpp/CMakeLists.txt** file of the native project, add **libpixelmap_ndk.z.so** of the image and **libhilog_ndk.z.so** of the log to the **target_link_libraries** dependency.

    ```txt
    target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so)
    ```

**Adding API Mappings**

Open the **src/main/cpp/hello.cpp** file and add the following API mappings to the **Init** function:

    ```c++
    EXTERN_C_START
    static napi_value Init(napi_env env, napi_value exports)
    {
        napi_property_descriptor desc[] = {
25 26 27
            { "testGetImageInfo", nullptr, TestGetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
            { "testAccessPixels", nullptr, TestAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr },
            { "testUnAccessPixels", nullptr, TestUnAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr },
G
Gloria 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        };

        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
        return exports;
    }
    EXTERN_C_END
    ```


**Calling the Native APIs**

For details about the APIs, see [Image API Reference](../reference/native-apis/image.md).

Obtain the JS resource object from the **hello.cpp** file and convert it to a native resource object. Then you can call native APIs. The sample code is as follows:
    
43 44 45
1. Obtain the **PixelMap** information and store the information to the **OhosPixelMapInfo** struct.
   ```c++
   static napi_value TestGetImageInfo(napi_env env, napi_callback_info info)
G
Gloria 已提交
46
    {
47 48
        napi_value result = nullptr;
        napi_get_undefined(env, &result);
G
Gloria 已提交
49

50 51 52
        napi_value thisVar = nullptr;
        napi_value argValue[1] = {0};
        size_t argCount = 1;
G
Gloria 已提交
53 54

        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
55 56 57 58
        
        OHOS::Media::OhosPixelMapInfo pixelMapInfo;
        OHOS::Media::OH_GetImageInfo(env, argValue[0], &pixelMapInfo);
        return result;
G
Gloria 已提交
59
    }
60 61 62 63
    ```
2. Obtain the memory address of a **PixelMap** object and lock the memory.
    ```c++
    static napi_value TestAccessPixels(napi_env env, napi_callback_info info)
G
Gloria 已提交
64
    {
65 66 67
        napi_value result = nullptr;
        napi_get_undefined(env, &result);

G
Gloria 已提交
68 69 70 71
        napi_value thisVar = nullptr;
        napi_value argValue[1] = {0};
        size_t argCount = 1;

72
        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
G
Gloria 已提交
73

74 75 76
        void* addrPtr = nullptr;
        OHOS::Media::OH_AccessPixels(env, argValue[0], &addrPtr);
        return result;
G
Gloria 已提交
77 78
    }
    ```
79
3. Unlock the memory of the **PixelMap** object.
G
Gloria 已提交
80
    ```c++
81
    static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info)
G
Gloria 已提交
82 83 84 85
    {
        napi_value result = nullptr;
        napi_get_undefined(env, &result);

86 87 88
        napi_value thisVar = nullptr;
        napi_value argValue[1] = {0};
        size_t argCount = 1;
G
Gloria 已提交
89

90
        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
G
Gloria 已提交
91

92
        OHOS::Media::OH_UnAccessPixels(env, argValue[0]);
G
Gloria 已提交
93 94 95 96 97 98 99 100 101 102 103 104
        return result;
    }
    ```

**Calling APIs on the JS Side**

1. Open **src\main\ets\pages\index.ets**, and import **libentry.so**.
    
2. Call the native APIs and pass in the JS resource object. The sample code is as follows:

    ```js
    import testNapi from 'libentry.so'
105
    import image from '@ohos.multimedia.image';
G
Gloria 已提交
106 107 108 109

    @Entry
    @Component
    struct Index {
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    @State message: string = 'IMAGE'
    @State _PixelMap: image.PixelMap = undefined

    build() {
        Row() {
        Column() {
            Button(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
                const color = new ArrayBuffer(96);
                let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 4, width: 6 } }
                image.createPixelMap(color, opts)
                .then( pixelmap => {
                    this._PixelMap = pixelmap;
G
Gloria 已提交
125 126
                })

127 128 129 130 131 132 133 134 135
                testNapi.testGetImageInfo(this._PixelMap);
                console.info("Test GetImageInfo success");

                testNapi.testAccessPixels(this._PixelMap);
                console.info("Test AccessPixels success");

                testNapi.testUnAccessPixels(this._PixelMap);
                console.info("Test UnAccessPixels success");
            })
G
Gloria 已提交
136
        }
137 138 139 140
        .width('100%')
        }
        .height('100%')
    }
G
Gloria 已提交
141 142
    }
    ```