images — 找图找色 / 截图
import "gomaton.dev/sdk/images"
Package images 截图、找色、找图。坐标为屏幕像素;区域 (x1,y1,x2,y2) 中 x2/y2<=0 表示到屏幕边缘。
颜色:"RRGGBB";偏色 "RRGGBB-DRDGDB";多选 "A|B"。多点找色见各函数。 sim/thresh 0..1,越大越严。找不到返回 (-1,-1) 或 Match.Found=false。
实现是【胶水】:Go 抓帧(优先桥的 in-process 截屏,回退 screencap)+ 解析颜色串, 像素循环与模板匹配在 libagovkit.so(闭源二进制)里做。
Binarize
func Binarize(gray *image.Gray, thresh int) *image.Gray
Binarize 二值化(>thresh → 255)。
Capture
func Capture(x1, y1, x2, y2 int) *image.NRGBA
Capture 区域截图为 *image.NRGBA(x2/y2<=0 到边);可再用 images 的图像处理函数加工。
Clip
func Clip(img *image.NRGBA, x1, y1, x2, y2 int) *image.NRGBA
Clip 裁剪 (x1,y1,x2,y2)(x2/y2<=0 到边),返回新图。
CmpColor
func CmpColor(x, y int, color string, sim float64) bool
CmpColor 判断 (x,y) 是否与 color 相似。
ColorOf
func ColorOf(hex string) color.NRGBA
ColorOf 把 "RRGGBB" 解析成 color.NRGBA;非法返回全 0。
CountColor
func CountColor(x1, y1, x2, y2 int, color string, sim float64) int
CountColor 统计区域内相似像素数。
Decode
func Decode(data []byte) *image.NRGBA
Decode 解码 PNG/JPEG 字节为图像对象;失败返回 nil。
DetectsMultiColors
func DetectsMultiColors(colors string, sim float64) bool
DetectsMultiColors 绝对多点比色:所有点都命中各自颜色才为 true。
Encode
func Encode(img *image.NRGBA, format string, quality int) []byte
Encode 编码为 "png" 或 "jpg"(quality 1..100,仅 jpg 用)。
EncodeBase64
func EncodeBase64(img *image.NRGBA, format string, quality int) string
EncodeBase64 编码为 base64 字符串。
FindColor
func FindColor(x1, y1, x2, y2 int, color string, sim float64) (int, int)
FindColor 区域内找第一个相似像素,返回像素坐标;找不到 (-1,-1)。
FindImage
func FindImage(tpl []byte, thresh float64) Match
FindImage 全屏找模板图(PNG/JPEG 字节),thresh 常用 0.8。
FindImageScaled
func FindImageScaled(tpl []byte, thresh float64) Match
FindImageScaled 多尺度找图(模板与屏上尺寸不一致时,慢一些)。
FindMultiColors
func FindMultiColors(x1, y1, x2, y2 int, colors string, sim float64) (int, int)
FindMultiColors 相对多点找色:找锚点且各偏移点命中 → 返回锚点像素坐标;找不到 (-1,-1)。
FindMultiColorsAll
func FindMultiColorsAll(x1, y1, x2, y2 int, colors string, sim float64) [][2]int
FindMultiColorsAll 相对多点找色找全部,返回所有命中锚点。
GrayToNRGBA
func GrayToNRGBA(gray *image.Gray) *image.NRGBA
GrayToNRGBA 灰度图转回彩色对象(给 FindImageIn / ocr.Image 等只收 NRGBA 的函数)。
Grayscale
func Grayscale(img *image.NRGBA) *image.Gray
Grayscale 转灰度图。
Pixel
func Pixel(x, y int) string
Pixel 取 (x,y) 像素颜色 "RRGGBB";越界返回 ""。
ReadBase64
func ReadBase64(s string) *image.NRGBA
ReadBase64 解码 base64 图片。
ReadFile
func ReadFile(path string) *image.NRGBA
ReadFile 读图片文件;失败返回 nil。
Resize
func Resize(img *image.NRGBA, width, height int) *image.NRGBA
Resize 缩放到 width×height(双线性)。
Rotate
func Rotate(img *image.NRGBA, degree int) *image.NRGBA
Rotate 顺时针旋转 degree 度(取 90 的整数倍;其他值四舍五入到最近的 90 倍)。
Save
func Save(img *image.NRGBA, path string, quality int) error
Save 按扩展名(.png/.jpg)保存到文件;父目录不存在会创建。
Screencap
func Screencap(targetW, quality int) []byte
Screencap 全屏截图 JPEG。targetW 目标宽(<=0 原始),quality 1..100。
Threshold
func Threshold(gray *image.Gray, thresh, maxVal int, inverse bool) *image.Gray
Threshold 二值化:>thresh 的像素置 maxVal,其余 0;inverse=true 反转。
ToNRGBA
func ToNRGBA(img image.Image) *image.NRGBA
ToNRGBA 任意 image.Image 转 *image.NRGBA(已经是则原样返回)。
WaitImage
func WaitImage(tpl []byte, thresh float64, timeoutMs int) Match
WaitImage 反复找图直到命中或超时(timeoutMs)。