接入报点预测
======

从6.0.0(20)开始，报点预测新增C API接口。

接入报点预测功能，可以优化应用中手写效果的绘制跟手性，提升应用中手写笔书写场景的跟手体验。

#### 场景介绍

在应用的自定义界面中，获取到界面的触摸事件，通过调用报点预测的接口，可以得到预测的下一个报点的位置信息。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/b3/v3/4YiqrsOPQtKtzz5y0Yj5ww/zh-cn_image_0000002594828654.png?HW-CC-KV=V1&HW-CC-Date=20260612T073448Z&HW-CC-Expire=86400&HW-CC-Sign=25E3322823233AF583CACFA00AB03552EB53E82B2636652EE93AF75A94F062ED)

#### 接口说明

| 名称 | 描述 |
| :-- | :-- |
| int32\_t [HMS\_HandWrite\_GetPredictPoint](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-handwrite-c#hms_handwrite_getpredictpoint)(const [HandWrite\_HistoricalPoint](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-handwrite-struct-historicalpoint) \*event, int32\_t size, float \*predictPointX, float \*predictPointY) | 获取预测点。 |

#### 接入步骤

报点预测功能的历史点，通常需要在自定义渲染（XComponent）上注册触摸事件回调获得，请参考[自定义渲染开发指南](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/napi-xcomponent-guidelines)，获得历史触摸点。

#### \[h2\]在CMake脚本中链接动态库

target\_include\_directories(entry PUBLIC ${HMOS\_SDK\_NATIVE}/sysroot/usr/include) # 当编译过程中报点预测头文件缺失时尝试加入此命令
target\_link\_directories(entry PUBLIC ${HMOS\_SDK\_NATIVE}/sysroot/usr/lib/aarch64-linux-ohos) # 当编译过程中报点预测API链接异常时尝试加入此命令
target\_link\_libraries(entry PUBLIC libace\_napi.z.so libhilog\_ndk.z.so libhandwrite\_ndk.z.so)

#### \[h2\]导入模块

#include <ace/xcomponent/native\_interface\_xcomponent.h>
#include <handwrite/native\_handwrite\_api.h>
#include <hilog/log.h>

#### \[h2\]示例代码

[native\_handwrite\_api.h](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-handwrite-headerfile-declare)提供HMS\_HandWrite\_GetPredictPoint()接口获取预测点。

#include <ace/xcomponent/native\_interface\_xcomponent.h>
#include <handwrite/native\_handwrite\_api.h>
#include <hilog/log.h>

void DispatchTouchEvent(OH\_NativeXComponent \*xcomponent, void \*window)
{
    int32\_t historicalPointSize = 0;
    OH\_NativeXComponent\_HistoricalPoint \*historicalPoints = nullptr;
    if (OH\_NativeXComponent\_GetHistoricalPoints(xcomponent, window, &historicalPointSize, &historicalPoints) !=
        OH\_NATIVEXCOMPONENT\_RESULT\_SUCCESS) {
        OH\_LOG\_Print(LOG\_APP, LOG\_ERROR, 0x0000, "PenKit", "failed to get historical points");
        return;
    }

    std::vector<HandWrite\_HistoricalPoint> handWriteHisPoints(historicalPointSize);
    for (int32\_t i = 0; i < historicalPointSize; ++i) {
        handWriteHisPoints\[i\].x = historicalPoints\[i\].x;
        handWriteHisPoints\[i\].y = historicalPoints\[i\].y;
        handWriteHisPoints\[i\].timeStamp = historicalPoints\[i\].timeStamp;
        handWriteHisPoints\[i\].force = historicalPoints\[i\].force;
    }

    float predictPointX = 0.0f;
    float predictPointY = 0.0f;
    int32\_t errcode = HMS\_HandWrite\_GetPredictPoint(handWriteHisPoints.data(), historicalPointSize, &predictPointX, &predictPointY);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "PenKit", "error code is %{public}d", errcode);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "PenKit", "predict point is (%{public}f, %{public}f)", predictPointX, predictPointY);
}