如何吸取一张图片的色值?

如何吸取一张图片的色值?有api吗?地址在哪?

HarmonyOS
2024-08-26 17:30:20
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

图片取色,文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-effectkit-V5#colorpicker

可参考以下代码:

import image from "@ohos.multimedia.image"; 
import { effectKit } from "@kit.ArkGraphics2D"; 
import { resourceManager } from '@kit.LocalizationKit'; 
 
@Entry 
@Component 
struct Index { 
  async aboutToAppear(): Promise<void> { 
 
    const context: Context = getContext(this); 
    // 获取resourceManager资源管理 
    const resourceMgr: resourceManager.ResourceManager = context.resourceManager; 
    const fileData: Uint8Array = await resourceMgr.getRawFileContent('startIcon.png'); 
    // 获取图片的ArrayBuffer 
    const buffer = fileData.buffer as ArrayBufferLike; 
    const imageSource: image.ImageSource = image.createImageSource(buffer.slice(0, buffer.byteLength)); 
    const pixelMap1: image.PixelMap = await imageSource.createPixelMap(); 
 
    effectKit.createColorPicker(pixelMap1, (error, colorPicker) => { 
      if (error) { 
        console.error('Failed to create color picker.'); 
      } else { 
        console.info('Succeeded in creating color picker.'); 
        colorPicker.getMainColor().then(color => { 
          console.info('Succeeded in getting main color.'); 
          console.info(`color[ARGB]=${color.alpha},${color.red},${color.green},${color.blue}`); 
        }) 
      } 
    }) 
 
  } 
 
  build() { 
    Text('hello') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-26 21:50:29
相关问题
如何一张图片转化为PixelMapElement
10024浏览 • 1回复 待解决
如何保存一张PNG图片到相册中
1854浏览 • 1回复 待解决
如何生成个十六进制颜色
428浏览 • 1回复 待解决