Skip to content

查找文字 - FindStrFromPtr

函数简介

从指定图片中查找文字。

接口名称

FindStrFromPtr

DLL调用

long FindStrFromPtr(long instance, long source, string str, string colorJson, string dict, double matchVal);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
source长整数型源图片对象的指针。
str字符串待查找的字符串,可以是字符串组合,比如"str1|str2|str3",中间用"|"来分割字符串
colorJson字符串颜色模型配置字符串,用于限定识别区域的颜色范围,格式说明见 颜色模型说明 - ColorModel。JSON格式示例:[{"StartColor":"3278FA","EndColor":"6496FF","Type":0}];简化格式示例:3278FA-000000|6496FF-2020203278FA~6496FFFF0000|00FF00
dict字符串字库名称,为空时搜索所有字库;多个字库用 | 分割,如 dict1|dict2
matchVal双精度浮点数匹配值。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    MatchData result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9);
    if (result.MatchState) {
        int x = result.X;
        int y = result.Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0)
{
    var result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9);
    if (result.MatchState)
    {
        int x = result.X;
        int y = result.Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
img_ptr = ola.LoadImage("images/scene.png")
if img_ptr:
    result = ola.FindStrFromPtr(img_ptr, "确定", "000000", "", 0.9)
    if result.get("MatchState"):
        x = result["X"]
        y = result["Y"]
    # 后续不再使用该图时释放
    ola.FreeImage(img_ptr)
java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.ApiResult;
import com.olaplug.model.Point;
import com.olaplug.model.MatchResult;
import java.util.List;

OLAPlugServer ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    MatchResult result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9);
    if (result != null && result.MatchState) {
        int x = result.X;
        int y = result.Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
imgPtr, _ := ola.LoadImage("images/scene.png")
if imgPtr != 0 {
    result := ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9)
    if result.MatchState {
        x := result.X
        y := result.Y
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let img_ptr = ola.load_image("images/scene.png").unwrap_or(0);
if img_ptr != 0 {
    let result = ola.find_str_from_ptr(img_ptr, "确定", "000000", "", 0.9);
    if result.match_state {
        let x = result.x;
        let y = result.y;
    }
    // 后续不再使用该图时释放
    ola.free_image(img_ptr);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var imgPtr = ola.LoadImage("images/scene.png")
if(imgPtr) {
    var result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9)
    if(result.MatchState) {
        var x = result.X
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
imgPtr = ola.LoadImage("images/scene.png")
If imgPtr <> 0 Then
    Set result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9)
    If result.MatchState Then
        ' 使用 result.X、result.Y
    End If
    ' 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
End If
text
.局部变量 ola, OLAPlug
ola.Create ()
.局部变量 imgPtr, 长整数型
imgPtr = ola.LoadImage (“images/scene.png”)
.如果真 (imgPtr ≠ 0)
    result = ola.FindStrFromPtr(imgPtr, “确定“, “000000“, ““, 0.9)
    .如果真 (result.MatchState)
        ' 使用 result.X、result.Y
    .如果真结束
    ' 后续不再使用该图时释放
    ola.FreeImage (imgPtr)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var imgPtr = ola.LoadImage("images/scene.png");
if(imgPtr){
    var result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9);
    if(result.MatchState){
        var x = result.X;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 imgPtr = ola.LoadImage("images/scene.png")
如果真 (imgPtr ≠ 0)
{
    MatchData result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9)
    如果真 (result.MatchState)
        // 使用 result.X、result.Y

    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr)
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
if (imgPtr != 0) {
    MatchData result = ola.FindStrFromPtr(imgPtr, "确定", "000000", "", 0.9);
    if (result.MatchState) {
        int32_t x = result.X;
        int32_t y = result.Y;
    }
    // 后续不再使用该图时释放
    ola.FreeImage(imgPtr);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long imgPtr = LoadImage(instance, "images/scene.png");
long ptr = FindStrFromPtr(instance, imgPtr, "确定", "000000", "", 0.9);
if (ptr != 0) {
    char buffer[4096] = {0};
    GetStringFromPtr(ptr, buffer, sizeof(buffer));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[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 FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long FindStrFromPtr(long ola, long source, string str, string colorJson, string dict, double matchVal);

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long LoadImage(long ola, string path);

long instance = CreateCOLAPlugInterFace();
long imgPtr = LoadImage(instance, "images/scene.png");
long ptr = FindStrFromPtr(instance, imgPtr, "确定", "000000", "", 0.9);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string result = sb.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
img_ptr = ola.LoadImage(instance, b"images/scene.png")
ptr = ola.FindStrFromPtr(instance, img_ptr, b"确定", b"000000", b"", 0.9)
if ptr:
    buf = create_string_buffer(4096)
    ola.GetStringFromPtr(ptr, buf, 4096)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

返回值

返回值说明
(返回值)字符串指针地址,返回JSON格式的匹配结果,如:。
json
{
    "MatchVal": 0.85,
    "MatchState": true,
    "Index": 0,
    "Angle": 45.0,
    "X": 100,
    "Y": 200,
    "Width": 100,
    "Height": 100
}
字段名类型说明
MatchVal浮点数匹配相似度。
MatchState布尔是否匹配成功。
Index整数结果索引(从 0 开始)。
Angle浮点数匹配角度。
X整数X 坐标。
Y整数Y 坐标。
Width整数宽度。
Height整数高度。
  • MatchVal:数据相似度
  • MatchState:返回数据是否大于指定精度,用于快速判断识别结果
  • Index:多图识别时的返回索引
  • Angle:识别结果角度
  • X/Y:识别结果坐标
  • Width/Height:识别结果宽高

返回的字符串指针需调用 FreeStringPtr 释放内存。