接入全局取色
======

接入全局取色功能，用户可以使用手指或者手写笔操作取色器在屏幕上移动，在目标位置抬起手指/抬起手写笔，会生成该位置色值对应的图像信息。

#### 场景介绍

在应用中拉起全局取色，效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0f/v3/25vVSBuvSWmbnhCx-sIS3w/zh-cn_image_0000002625188101.png?HW-CC-KV=V1&HW-CC-Date=20260612T073448Z&HW-CC-Expire=86400&HW-CC-Sign=B053C05E1B8985176E1ACA7E8FA55F69B29C62B155FBBEF9EB47181EEA48CA9B)

支持获取当前屏幕上选中位置的色值和色域空间。

#### 限制与约束

全局取色能力支持设备：Tablet、PC/2in1，并且从5.1.1(19)版本开始，新增支持设备：Phone（只支持部分机型）。

#### 接口说明

| 名称 | 描述 |
| :-- | :-- |
| int32\_t [HMS\_GCP\_StartColorPicker](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-imagefeaturepicker-c#hms_gcp_startcolorpicker) (int32\_t initialPosX, int32\_t initialPosY, [HMS\_GCP\_OnResult](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-imagefeaturepicker-c#hms_gcp_onresult) onResultCallback, void \*userData) | 启动取色器。此API用于启动取色器，在取色器移动时不显示色值。 |
| int32\_t [HMS\_GCP\_StartColorPickerWithColorValue](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-imagefeaturepicker-c#hms_gcp_startcolorpickerwithcolorvalue) (int32\_t initialPosX, int32\_t initialPosY, [HMS\_GCP\_OnResult](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/pen-imagefeaturepicker-c#hms_gcp_onresult) onResultCallback, void \*userData) | 启动取色器。此API用于启动取色器，在取色器移动时显示色值。 |

#### 开发步骤

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

target\_link\_libraries(entry PUBLIC libace\_napi.z.so libcolorpicker\_ndk.z.so  libhilog\_ndk.z.so)

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

#include "color\_picker/native\_gcp\_api.h"
#include "hilog/log.h"

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

native\_gcp\_api.h提供HMS\_GCP\_StartColorPicker()和HMS\_GCP\_StartColorPickerWithColorValue()两种方式启动全局取色功能。

通过调用HMS\_GCP\_StartColorPicker()，实现启动取色器。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/58/v3/lI2kuwaDQDuLUpC5IVm_JQ/notice_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073448Z&HW-CC-Expire=86400&HW-CC-Sign=45342F4D694B460BE4A74E7FDB7A06B42EE5EC7FE465C62CAD9F4DF6A5BC1338)

该启动方法不支持实时显示色值。

void onSuccessCallback(void \*userData, HMS\_GCP\_PickedColorInfo pickedColorInfo, const int32\_t code) {
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked code is: %{public}d", code);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Red is: %{public}d", pickedColorInfo.color.red);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Green is:%{public}d ", pickedColorInfo.color.green);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Blue is: %{public}d", pickedColorInfo.color.blue);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Alpha is:%{public}d ", pickedColorInfo.color.alpha);
    delete userData;
}
void startPick() {
    int32\_t posX = 200;
    int32\_t posY = 200;
    void \*userData = nullptr;
    HMS\_GCP\_StartColorPicker(posX, posY, onSuccessCallback, userData);
}

通过调用HMS\_GCP\_StartColorPickerWithColorValue()，实现启动取色器。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/d9/v3/ABnOWqlVS8yTMVYXObI1qA/notice_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073448Z&HW-CC-Expire=86400&HW-CC-Sign=89B4C8ABD1E1C73C8F7E57CF2610B5D706B2383C1E6A9A765087041D6BEB94DC)

该启动方法支持实时显示色值。

void onSuccessCallback(void \*userData, HMS\_GCP\_PickedColorInfo pickedColorInfo, const int32\_t code) {
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked code is: %{public}d", code);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Red is: %{public}d", pickedColorInfo.color.red);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Green is:%{public}d ", pickedColorInfo.color.green);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Blue is: %{public}d", pickedColorInfo.color.blue);
    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, 0x0000, "penkit", "picked Alpha is:%{public}d ", pickedColorInfo.color.alpha);
    delete userData;
}
void startPickWithColorValue() {
    int32\_t posX = 200;
    int32\_t posY = 200;
    void \*userData = nullptr;
    HMS\_GCP\_StartColorPickerWithColorValue(posX, posY, onSuccessCallback, userData);
}