图像处理接口
版本:1.26.0613
imageToGray
cpp
EXPORT_DLL_API const char* imageToGray(const char* srcPath, const char* outPath);功能:将图片转为灰度图并保存。若 outPath 传空字符串,自动生成 原文件名_gray.扩展名。
| 参数 | 类型 | 说明 |
|---|---|---|
srcPath | 输入 | 原图片路径 |
outPath | 输入 | 输出图片路径。传 "" 时自动生成 |
返回值示例:
json
// 成功
{"success":true}
// 失败
{"success":false,"error":"原图片路径为空"}
{"success":false,"error":"原图片不存在或无法读取"}
{"success":false,"error":"不支持的图片通道数"}
{"success":false,"error":"保存图片失败"}自动命名:photo.jpg → photo_gray.jpg
调用示例:
cpp
imageToGray("photo.jpg", ""); // 自动命名 photo_gray.jpg
imageToGray("photo.jpg", "gray/photo.bmp"); // 指定输出路径screenCapture
cpp
EXPORT_DLL_API const char* screenCapture(const char* outPath, int left, int top, int width, int height);功能:屏幕截图。使用 Windows GDI 截取指定屏幕区域并保存。
| 参数 | 类型 | 说明 |
|---|---|---|
outPath | 输入 | 输出图片路径 |
left | 输入 | 截图区域左上角 x 坐标 |
top | 输入 | 截图区域左上角 y 坐标 |
width | 输入 | 截图区域宽度 |
height | 输入 | 截图区域高度 |
返回值示例:
json
// 成功
{"success":true, "path":"C:/screenshot.png"}
// 失败
{"success":false,"error":"输出路径为空"}
{"success":false,"error":"截图宽高必须大于0"}
{"success":false,"error":"保存截图失败"}调用示例:
cpp
const char* result = screenCapture("screenshot.png", 0, 0, 1920, 1080);
printf("截图: %s\n", result);imageCrop
cpp
EXPORT_DLL_API const char* imageCrop(const char* srcPath, int x, int y, int w, int h, const char* outPath);功能:裁剪图片指定区域。
| 参数 | 类型 | 说明 |
|---|---|---|
srcPath | 输入 | 原图片路径 |
x | 输入 | 裁剪区域左上角 x 坐标 |
y | 输入 | 裁剪区域左上角 y 坐标 |
w | 输入 | 裁剪区域宽度 |
h | 输入 | 裁剪区域高度 |
outPath | 输入 | 输出图片路径 |
返回值示例:
json
// 成功
{"success":true}
// 失败
{"success":false,"error":"原图片路径为空"}
{"success":false,"error":"输出路径为空"}
{"success":false,"error":"裁剪宽高必须大于0"}
{"success":false,"error":"原图片不存在或无法读取"}
{"success":false,"error":"裁剪区域超出图片范围"}
{"success":false,"error":"保存裁剪图片失败"}调用示例:
cpp
const char* result = imageCrop("screenshot.png", 100, 200, 300, 400, "crop.png");
printf("裁剪: %s\n", result);imageResize
cpp
EXPORT_DLL_API const char* imageResize(const char* srcPath, int width, int height, const char* outPath);功能:将图片缩放到指定尺寸。
| 参数 | 类型 | 说明 |
|---|---|---|
srcPath | 输入 | 原图片路径 |
width | 输入 | 目标宽度 |
height | 输入 | 目标高度 |
outPath | 输入 | 输出图片路径 |
返回值示例:
json
// 成功
{"success":true}
// 失败
{"success":false,"error":"原图片路径为空"}
{"success":false,"error":"输出路径为空"}
{"success":false,"error":"目标宽高必须大于0"}
{"success":false,"error":"原图片不存在或无法读取"}
{"success":false,"error":"保存缩放图片失败"}调用示例:
cpp
const char* result = imageResize("screenshot.png", 640, 480, "resized.png");
printf("缩放: %s\n", result);findColor
cpp
EXPORT_DLL_API const char* findColor(const char* imgPath, const char* colorHex, int tolerance);功能:在图片中查找指定颜色的所有像素位置。
| 参数 | 类型 | 说明 |
|---|---|---|
imgPath | 输入 | 图片路径 |
colorHex | 输入 | 颜色值,六位十六进制,如 "FF0000"(红色,RGB 顺序) |
tolerance | 输入 | 容差(0~255),每个通道允许的偏差范围 |
返回值示例:
json
// 成功(最多 10000 个点,超出时 truncated=true)
{"success":true, "count":5, "points":[[100,200],[150,220],[180,240],[200,260],[220,280]]}
// 失败
{"success":false,"error":"图片路径为空"}
{"success":false,"error":"颜色值为空"}
{"success":false,"error":"颜色值格式错误,需6位十六进制"}
{"success":false,"error":"图片不存在或无法读取"}调用示例:
cpp
const char* result = findColor("screen.png", "FF0000", 10); // 找红色,容差 10
printf("找色: %s\n", result);getPixelColor
cpp
EXPORT_DLL_API const char* getPixelColor(const char* imgPath, int x, int y);功能:获取图片中指定像素点的颜色值。
| 参数 | 类型 | 说明 |
|---|---|---|
imgPath | 输入 | 图片路径 |
x | 输入 | 像素 x 坐标 |
y | 输入 | 像素 y 坐标 |
返回值示例:
json
// 成功
{"success":true, "color":"FF0000", "hex":"#FF0000"}
// 失败
{"success":false,"error":"图片路径为空"}
{"success":false,"error":"图片不存在或无法读取"}
{"success":false,"error":"坐标超出图片范围"}调用示例:
cpp
const char* result = getPixelColor("screen.png", 100, 200);
printf("取色: %s\n", result);imageCompare
cpp
EXPORT_DLL_API const char* imageCompare(const char* img1, const char* img2);功能:比较两张图片的相似度。基于 HSV 直方图相关性,返回 0~1(1 表示完全相同)。尺寸不同时自动缩放后比较。
| 参数 | 类型 | 说明 |
|---|---|---|
img1 | 输入 | 第一张图片路径 |
img2 | 输入 | 第二张图片路径 |
返回值示例:
json
// 成功
{"success":true, "similarity":0.95}
// 失败
{"success":false,"error":"图片路径为空"}
{"success":false,"error":"图片不存在或无法读取"}调用示例:
cpp
const char* result = imageCompare("img1.png", "img2.png");
printf("相似度: %s\n", result);imageDrawRect
cpp
EXPORT_DLL_API const char* imageDrawRect(const char* srcPath, const char* outPath, int x, int y, int w, int h, int thickness, const char* colorHex);功能:在图片中绘制矩形框并保存到输出路径。
| 参数 | 类型 | 说明 |
|---|---|---|
srcPath | 输入 | 原图路径 |
outPath | 输入 | 输出图片路径(绘制后保存) |
x | 输入 | 矩形左上角 x 坐标 |
y | 输入 | 矩形左上角 y 坐标 |
w | 输入 | 矩形宽度 |
h | 输入 | 矩形高度 |
thickness | 输入 | 线条粗细(像素),-1 为填充整个矩形 |
colorHex | 输入 | 颜色值,支持 "#409EFF" 或 "409EFF" 格式(RGB 顺序) |
返回值示例:
json
// 成功
{"success":true}
// 失败
{"success":false,"error":"原图路径为空"}
{"success":false,"error":"输出路径为空"}
{"success":false,"error":"颜色值为空"}
{"success":false,"error":"图片不存在或无法读取"}
{"success":false,"error":"颜色值格式错误,需6位十六进制"}调用示例:
cpp
// 绘制红色矩形框,线条粗 2px
imageDrawRect("screen.png", "result.png", 100, 200, 50, 80, 2, "#FF0000");
// 填充半透明效果的蓝色矩形(thickness = -1 表示填充)
imageDrawRect("screen.png", "result.png", 300, 400, 100, 60, -1, "#409EFF");