主题
解析匹配图像多结果JSON - ParseMatchImageAllJson
函数简介
解析匹配图像 JSON 数组,按索引取出其中某一条匹配结果的详细信息。
接口名称
ParseMatchImageAllJsonDLL调用
c
int ParseMatchImageAllJson(string str, int parseIndex, int* matchState, int* x, int* y, int* width, int* height, double* matchVal, double* angle, int* index);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| str | 字符串 | 匹配图像 JSON 字符串(须为数组格式)。 |
| parseIndex | 整数型 | 要解析的下标,从 0 开始。 |
| matchState | 整数型指针 | 输出:匹配状态(非 0 表示匹配成功)。 |
| x | 整数型指针 | 输出:匹配点 X 坐标。 |
| y | 整数型指针 | 输出:匹配点 Y 坐标。 |
| width | 整数型指针 | 输出:匹配图片宽度。 |
| height | 整数型指针 | 输出:匹配图片高度。 |
| matchVal | 双精度浮点数指针 | 输出:匹配值。 |
| angle | 双精度浮点数指针 | 输出:匹配角度。 |
| index | 整数型指针 | 输出:匹配索引(JSON 内 Index 字段)。 |
适用场景与联动
本接口解析多结果 JSON 数组中的某一项。常见来源:
| 来源接口 | 说明 |
|---|---|
| MatchImageFromPathAll | 路径多结果匹配 |
| MatchImageFromPtrAll | 内存图多结果匹配 |
| MatchImagePtrFromPathAll | 指针模板多结果 |
| 同类 All 接口 | 如 FindImage*All、MatchWindows*All、FindWindows*All 等 |
| SortPosDistance | 排序后仍为本格式数组 |
与 GetMatchImageAllCount 联动(推荐写法):
MatchImageFromPathAll / MatchImageFromPtrAll / ...
↓
GetStringFromPtr 取出 JSON 数组文本
↓
count = GetMatchImageAllCount(json)
↓
for parseIndex = 0 .. count-1:
ParseMatchImageAllJson(json, parseIndex, ...)单结果对象请用 ParseMatchImageJson。
不要越界:parseIndex必须满足0 <= parseIndex < GetMatchImageAllCount(json)。
示例
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}]";
// 先取数量,再按索引解析(与 GetMatchImageAllCount 联动)
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;
int32_t ret = ola.ParseMatchImageAllJson(
resultJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
if (ret == 1 && matchState != 0) {
// 第 i 个匹配结果:可点击 (x, y),相似度 matchVal
}
}csharp
using OLAPlug;
var 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);
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()
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)
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);
for (int i = 0; i < count; i++) {
// 按 Java SDK 约定解析第 i 条:ParseMatchImageAllJson(resultJson, 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)
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 个匹配结果
}
}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);
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 个匹配结果
}
}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 联动
findCount = ola.MatchImageFromPathAll (“images/scene.png”, “img/template.bmp”, 0.9, 1, 0, 1, retData, rawstring)
count = ola.GetMatchImageAllCount (rawstring)
.计次循环首 (count, i)
ret = ola.ParseMatchImageAllJson (rawstring, i - 1, matchState, x, y, width, height, matchVal, angle, index)
.如果真 (ret = 1 并且 matchState ≠ 0)
' 第 i-1 个匹配结果:可点击 (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;
int32_t ret = ola.ParseMatchImageAllJson(
resultJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
if (ret == 1 && matchState != 0) {
// 第 i 个匹配结果
}
}原生 DLL 调用(与 MatchImageFromPathAll + GetMatchImageAllCount 完整联动)
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. GetMatchImageAllCount 得数量,再 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;
int ret = ParseMatchImageAllJson(
instance, json, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
if (ret == 1 && matchState != 0) {
// 第 i 个匹配点 (x, y),相似度 matchVal
}
}
// 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();
long ptr = MatchImageFromPathAll(instance, "images/scene.png", "img/template.bmp", 0.9, 1, 0, 1);
if (ptr == 0) return;
var sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
string json = sb.ToString();
FreeStringPtr(ptr);
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()
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")
size = ola.GetStringSize(ptr)
buf = create_string_buffer(size + 1)
ola.GetStringFromPtr(ptr, buf, size + 1)
ola.FreeStringPtr(instance, ptr) # 按所用封装签名调整
result_json = buf.value
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 错误码说明。
| 返回值 | 说明 |
|---|---|
1 | 成功。 |
0 | 失败(格式不符、索引越界等)。 |
注意事项
| 项目 | 说明 |
|---|---|
| parseIndex 从 0 开始 | 第 1 条为 0,最后一条为 count - 1。 |
| 先 Count 再 Parse | 推荐先 GetMatchImageAllCount 再循环,避免越界。 |
| 输入须为数组 | 单结果对象请用 ParseMatchImageJson。 |
| 输出参数 | 所有输出参数须提供有效指针/变量。 |
| SDK 结构化返回 | C++/C#/Python 等 SDK 的 MatchImageFromPathAll 已直接返回列表时,一般直接遍历列表即可;本接口主要用于原生 DLL、易语言 rawstring、SortPosDistance 等拿到原始 JSON 数组的场景。 |
