主题
获取匹配图像JSON数量 - GetMatchImageAllCount
函数简介
获取匹配图像 JSON 数组中的匹配结果数量,常用于配合 ParseMatchImageAllJson 遍历全部命中点。
接口名称
GetMatchImageAllCountDLL调用
c
int GetMatchImageAllCount(string str);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | 字符串 | 匹配图像 JSON 字符串(须为数组格式)。 |
适用场景与联动
本接口作用于多结果 JSON 数组。常见来源:
| 来源接口 | 说明 |
|---|---|
| MatchImageFromPathAll | 路径多结果匹配 |
| MatchImageFromPtrAll | 内存图多结果匹配 |
| MatchImagePtrFromPathAll | 指针模板多结果 |
| 同类 All 接口 | 如 FindImage*All、MatchWindows*All、FindWindows*All 等 |
| SortPosDistance | 对匹配结果 JSON 排序后仍为本格式 |
与 ParseMatchImageAllJson 联动(推荐写法):
MatchImageFromPathAll / MatchImageFromPtrAll / ...
↓ 返回 JSON 数组字符串指针(原生)或 rawstring(易语言)
GetStringFromPtr 取出 JSON 文本
↓
GetMatchImageAllCount(json) → 得到 count
↓
for i = 0 .. count-1:
ParseMatchImageAllJson(json, i, ...) → 解析第 i 条
↓
FreeStringPtr 释放指针单结果对象请用 ParseMatchImageJson,不要用本接口。
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 多结果 JSON 数组:与 MatchImageFromPathAll 原生返回格式一致
const char* resultJson =
"[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},"
"{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
int32_t count = ola.GetMatchImageAllCount(resultJson); // 此处为 2
for (int32_t i = 0; i < count; i++) {
int32_t matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
// 与 ParseMatchImageAllJson 联动:按索引逐条解析
if (ola.ParseMatchImageAllJson(
resultJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index) == 1
&& matchState != 0) {
// 处理第 i 个匹配点 (x, y)
}
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
// 多结果 JSON 数组:与 MatchImageFromPathAll 原生返回格式一致
string resultJson =
"[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},"
+ "{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
int count = ola.GetMatchImageAllCount(resultJson); // 此处为 2
for (int i = 0; i < count; i++)
{
int ret = ola.ParseMatchImageAllJson(
resultJson, i,
out int matchState, out int x, out int y, out int width, out int height,
out double matchVal, out double angle, out int index);
if (ret == 1 && matchState != 0)
{
// 处理第 i 个匹配点 (x, y)
}
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
# 多结果 JSON 数组:与 MatchImageFromPathAll 原生返回格式一致
result_json = (
'[{"MatchState":true,"X":120,"Y":80,"Width":32,"Height":32,"MatchVal":0.95,"Angle":0,"Index":0},'
'{"MatchState":true,"X":300,"Y":200,"Width":32,"Height":32,"MatchVal":0.88,"Angle":0,"Index":1}]'
)
count = ola.GetMatchImageAllCount(result_json) # 此处为 2
for i in range(count):
ret, match_state, x, y, width, height, match_val, angle, index = ola.ParseMatchImageAllJson(result_json, i)
if ret == 1 and match_state != 0:
pass # 处理第 i 个匹配点 (x, y)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String resultJson =
"[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},"
+ "{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
int count = ola.GetMatchImageAllCount(resultJson); // 此处为 2
for (int i = 0; i < count; i++) {
// 按 Java SDK 约定调用 ParseMatchImageAllJson(resultJson, i, ...) 解析第 i 条
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
resultJson := "[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]"
count := ola.GetMatchImageAllCount(resultJson) // 此处为 2
for i := 0; i < count; i++ {
matchState, x, y, width, height, index := 0, 0, 0, 0, 0, 0
matchVal, angle := 0.0, 0.0
ret := ola.ParseMatchImageAllJson(resultJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index)
if ret == 1 && matchState != 0 {
// 处理第 i 个匹配点 (x, y)
}
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let result_json = "[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
let count = ola.get_match_image_all_count(result_json); // 此处为 2
for i in 0..count {
let mut match_state = 0i32;
let mut x = 0i32;
let mut y = 0i32;
let mut width = 0i32;
let mut height = 0i32;
let mut index = 0i32;
let mut match_val = 0.0f64;
let mut angle = 0.0f64;
let ret = ola.parse_match_image_all_json(
result_json, i,
&mut match_state, &mut x, &mut y, &mut width, &mut height,
&mut match_val, &mut angle, &mut index,
);
if ret == 1 && match_state != 0 {
// 处理第 i 个匹配点 (x, y)
}
}cpp
var ola = com("OlaPlug.OlaSoft")
var resultJson = "[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]"
var count = ola.GetMatchImageAllCount(resultJson)
for (var i = 0; i < count; i++) {
var matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0
var matchVal = 0.0, angle = 0.0
var ret = ola.ParseMatchImageAllJson(resultJson, i, matchState, x, y, width, height, matchVal, angle, index)
if (ret == 1 && matchState != 0) {
// 处理第 i 个匹配点
}
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
resultJson = "[{\""MatchState\"":true,\""X\"":120,\""Y\"":80,\""Width\"":32,\""Height\"":32,\""MatchVal\"":0.95,\""Angle\"":0,\""Index\"":0},{\""MatchState\"":true,\""X\"":300,\""Y\"":200,\""Width\"":32,\""Height\"":32,\""MatchVal\"":0.88,\""Angle\"":0,\""Index\"":1}]"
count = ola.GetMatchImageAllCount(resultJson)
For i = 0 To count - 1
Dim matchState, x, y, width, height, matchVal, angle, index
ret = ola.ParseMatchImageAllJson(resultJson, i, matchState, x, y, width, height, matchVal, angle, index)
If ret = 1 And matchState <> 0 Then
' 处理第 i 个匹配点
End If
Nexttext
.局部变量 ola, OLAPlug
.局部变量 retData, MatchData, , 数组
.局部变量 findCount, 整数型
.局部变量 rawstring, 文本型
.局部变量 count, 整数型
.局部变量 i, 整数型
.局部变量 ret, 整数型
.局部变量 matchState, 整数型
.局部变量 x, 整数型
.局部变量 y, 整数型
.局部变量 width, 整数型
.局部变量 height, 整数型
.局部变量 matchVal, 双精度小数型
.局部变量 angle, 双精度小数型
.局部变量 index, 整数型
ola.创建 ()
' 与 MatchImageFromPathAll 联动:rawstring 为原始 JSON 数组
findCount = ola.MatchImageFromPathAll (“images/scene.png”, “img/template.bmp”, 0.9, 1, 0, 1, retData, rawstring)
' 也可直接遍历 retData;需要按 JSON 解析时:
count = ola.GetMatchImageAllCount (rawstring)
.计次循环首 (count, i)
' parseIndex 从 0 开始,计次循环 i 从 1 开始时用 i-1
ret = ola.ParseMatchImageAllJson (rawstring, i - 1, matchState, x, y, width, height, matchVal, angle, index)
.如果真 (ret = 1 并且 matchState ≠ 0)
' 处理该匹配点 (x, y)
.如果真结束
.计次循环尾 ()aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var resultJson = "[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
var count = ola.GetMatchImageAllCount(resultJson);
for(i = 0; i < count; i++){
var matchState, x, y, width, height, matchVal, angle, index;
var ret = ola.ParseMatchImageAllJson(resultJson, i, matchState, x, y, width, height, matchVal, angle, index);
if(ret == 1 && matchState != 0){
// 处理第 i 个匹配点
}
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 resultJson = "[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]"
整数 count = ola.GetMatchImageAllCount(resultJson)
计次循环首 (count)
整数 i = 取循环索引 () - 1
整数 matchState = 0
整数 x = 0
整数 y = 0
整数 width = 0
整数 height = 0
整数 index = 0
双精度小数 matchVal = 0
双精度小数 angle = 0
整数 ret = ola.ParseMatchImageAllJson(resultJson, i, matchState, x, y, width, height, matchVal, angle, index)
如果真 (ret == 1 且 matchState != 0)
' 处理第 i 个匹配点
如果真结束
计次循环尾 ()cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
const char* resultJson =
"[{\"MatchState\":true,\"X\":120,\"Y\":80,\"Width\":32,\"Height\":32,\"MatchVal\":0.95,\"Angle\":0,\"Index\":0},"
"{\"MatchState\":true,\"X\":300,\"Y\":200,\"Width\":32,\"Height\":32,\"MatchVal\":0.88,\"Angle\":0,\"Index\":1}]";
int32_t count = ola.GetMatchImageAllCount(resultJson);
for (int32_t i = 0; i < count; i++) {
int32_t matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
if (ola.ParseMatchImageAllJson(
resultJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index) == 1
&& matchState != 0) {
// 处理第 i 个匹配点
}
}原生 DLL 调用(与 MatchImageFromPathAll 完整联动)
cpp
long instance = CreateCOLAPlugInterFace();
// 1. 多结果匹配,成功返回 JSON 数组指针
long ptr = MatchImageFromPathAll(
instance, "images/scene.png", "img/template.bmp", 0.9, 1, 0, 1);
if (ptr == 0) {
// 未匹配到或失败
return;
}
// 2. 取出 JSON 数组文本
char json[8192] = {0};
GetStringFromPtr(ptr, json, sizeof(json));
// 3. 先取数量,再逐条 ParseMatchImageAllJson
int count = GetMatchImageAllCount(instance, json);
for (int i = 0; i < count; i++) {
int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
if (ParseMatchImageAllJson(
instance, json, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index) == 1
&& matchState != 0) {
// 处理第 i 个匹配点 (x, y)
}
}
// 4. 释放
FreeStringPtr(instance, ptr);csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long MatchImageFromPathAll(long ola, string source, string templ, double matchVal, int type, double angle, double scale);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetMatchImageAllCount(long ola, string str);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageAllJson(long ola, string str, int parseIndex, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);
long instance = CreateCOLAPlugInterFace();
// 1. MatchImageFromPathAll → JSON 数组指针
long ptr = MatchImageFromPathAll(instance, "images/scene.png", "img/template.bmp", 0.9, 1, 0, 1);
if (ptr == 0) return;
// 2. 取出 JSON
var sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
string json = sb.ToString();
FreeStringPtr(ptr);
// 3. count + 循环 ParseMatchImageAllJson
int count = GetMatchImageAllCount(instance, json);
for (int i = 0; i < count; i++)
{
int ret = ParseMatchImageAllJson(
instance, json, i,
out int matchState, out int x, out int y, out int width, out int height,
out double matchVal, out double angle, out int index);
if (ret == 1 && matchState != 0)
{
// 处理第 i 个匹配点 (x, y)
}
}python
from ctypes import CDLL, c_int, c_int64, c_double, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.MatchImageFromPathAll.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
# 1. MatchImageFromPathAll → JSON 数组指针
ptr = ola.MatchImageFromPathAll(
instance, b"images/scene.png", b"img/template.bmp",
c_double(0.9), c_int(1), c_double(0), c_double(1))
if ptr == 0:
raise RuntimeError("match failed")
# 2. 取出 JSON
size = ola.GetStringSize(ptr)
buf = create_string_buffer(size + 1)
ola.GetStringFromPtr(ptr, buf, size + 1)
ola.FreeStringPtr(instance, ptr) # 按所用封装签名调整
result_json = buf.value
# 3. count + 循环 ParseMatchImageAllJson
count = ola.GetMatchImageAllCount(instance, result_json)
match_state = c_int(0)
x = c_int(0)
y = c_int(0)
width = c_int(0)
height = c_int(0)
index = c_int(0)
match_val = c_double(0)
angle = c_double(0)
for i in range(count):
ret = ola.ParseMatchImageAllJson(
instance, result_json, i,
match_state, x, y, width, height, match_val, angle, index)
if ret == 1 and match_state.value != 0:
pass # 处理第 i 个匹配点返回值
错误码含义见 JSON 错误码说明。
| 返回值 | 说明 |
|---|---|
| (返回值) | 返回匹配结果条数;解析失败或非数组时返回 0。 |
注意事项
| 项目 | 说明 |
|---|---|
| 输入须为数组 | JSON 须为 [{...},{...}] 数组格式。 |
| 与 ParseMatchImageAllJson 成对使用 | 先 GetMatchImageAllCount 得 count,再对 0 .. count-1 调用 ParseMatchImageAllJson。 |
| 索引从 0 开始 | parseIndex 与本接口返回的数量对应,从 0 计到 count - 1。 |
| SDK 结构化返回 | C++/C#/Python 等 SDK 的 MatchImageFromPathAll 已直接返回列表时,一般用 results.Count / len(results) 即可;本接口主要用于原生 DLL、易语言 rawstring、排序后的 JSON 等场景。 |
