目录

接入全局取色

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

场景介绍

在应用中拉起全局取色,效果如下:

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

限制与约束

接口说明

类名 接口名 说明
imageFeaturePicker pickForResult(x?:number, y?:number):Promise<PickedColorInfo> 启动取色器。此API用于启动取色器,在取色器移动时不显示色值。该接口要求设备支持手写笔功能,若设备不支持手写笔,则无法启动取色器。

开发步骤

  1. 导入相关模块。

    import { imageFeaturePicker } from '@kit.Penkit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct Index {
      @State message: string = 'Hello World';
    
      build() {
        Stack({ alignContent: Alignment.Center }) {
          Column() {
            Row() {
              Button() {
                Text('Call GlobalColorPicker from ets side')
                  .fontSize(18)
                  .fontWeight(FontWeight.Normal)
              }
              .width('50%')
              .height('60vp')
              .align(Alignment.Center)
              .onClick((event) => {
                // 此处的 displayX 和 displayY 为触摸事件触发时屏幕上的坐标位置
                imageFeaturePicker.pickForResult(event.displayX, event.displayY)
                  .then((colorInfo: imageFeaturePicker.PickedColorInfo) => {
                    if (colorInfo) {
                      console.info('colorInfo=' + JSON.stringify(colorInfo));
                    }
                  }).catch((err: BusinessError) => {
                  console.error(`pickForResult failed. Code is ${err.code}, message is ${err.message}`);
                })
              })
            }
          }
          .align(Alignment.Center)
        }
        .width('100%')
        .height('100%')
      }
    }