【全网唯一】C++ 纯本地离线文字识别Windows版dll插件

目的

        c++开发使用的是Microsoft Visual Studio(简称VS),它是美国微软公司的开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台,包括Microsoft Windows、Windows Mobile、Windows CE、.NET Framework、.NET Compact Framework和Microsoft Silverlight 及Windows Phone。本篇文章主要讲解下c#语言的TomatoOCR纯本地离线文字识别Windows版插件如何使用和集成。

准备工作

  1. 下载Microsoft Visual Studio 2022开发工具
  2. 下载对应的TomatoOCR.dll依赖包:下载

       

  • 目前插件支持中英文、繁体字、日语、韩语识别;
  • 支持小图、区域图和单行文字识别,准确率高达99%;
  • 支持多种返回格式,json\文本\数字\自定义;
  • 支持二值化;
  • 支持找字返回坐标并点击;
  • 超高的稳定性,速度快;
  • 支持多线程;

依赖包集成

依赖包放置

注意:由于32位应用内存限制,初始化ocr线程不要太多,建议5个以下,64位应用不受限制

32位: 下载“【x86 MT】本地文字识别依赖包(中英文).dll”或者“【x86 MT】本地文字识别依赖包(中英文+繁、日、韩).dll”依赖包后,改名为TomatoOCR.dll,将文件放置在项目中

64位: 下载“【x64 MT】本地文字识别依赖包(中英文).dll”或者“【x64 MT】本地文字识别依赖包(中英文+繁、日、韩).dll”依赖包后,改名为TomatoOCR.dll,将文件放置在项目中

这里以控制台应用为例:


#define _CRT_SECURE_NO_WARNINGS

#include 
#include 
#include 
#include 
#include 

using namespace std;

class TomatoOCR; // 仅声明,不定义

typedef void(__cdecl* InitFunc)(int num);
typedef void (__cdecl* SetModeFunc)(const char*);
typedef void(__cdecl* SetHttpIntervalTimeFunc)(int httpIntervalTime);
typedef const char* (__cdecl* SetLicenseFunc)(const char* license, const char* remark);


typedef TomatoOCR* (__cdecl* LockFunc)();
typedef void (__cdecl* SetRecTypeFunc)(TomatoOCR* obj, const char* value);
typedef void (__cdecl* SetDetBoxTypeFunc)(TomatoOCR* obj, const char* value);
typedef void (__cdecl* SetDetUnclipRatioFunc)(TomatoOCR* obj, float ratio);
typedef void (__cdecl* SetRecScoreThresholdFunc)(TomatoOCR* obj, float threshold);
typedef void (__cdecl* SetReturnTypeFunc)(TomatoOCR* obj, const char* value);
typedef void (__cdecl* SetBinaryThreshFunc)(TomatoOCR* obj, int thresh);
typedef void (__cdecl* SetRunModeFunc)(TomatoOCR* obj, const char* value);
typedef void (__cdecl* SetFilterColorFunc)(TomatoOCR* obj, const char* value);

typedef const char* (__cdecl* OcrFileFunc)(TomatoOCR* obj, const char* filePath, int type);
typedef const char* (__cdecl* OcrScreenFunc)(TomatoOCR* obj, int x1, int y1, int x2, int y2, int type, const char* outImagePath);
typedef const char* (__cdecl* OcrDMImageDataFunc)(TomatoOCR* obj, const char* imageData, int type);
typedef const char* (__cdecl* FindTapPointFunc)(TomatoOCR* obj, const char* character);
typedef const char* (__cdecl* FindTapPointsFunc)(TomatoOCR* obj, const char* character);

typedef void (__cdecl* UnlockFunc)(TomatoOCR* obj);

int main(){
     // 设置控制台输出编码为 UTF-8
    SetConsoleOutputCP(CP_UTF8);
    
    InitFunc init = (InitFunc)GetProcAddress(hDll, "init");
    SetModeFunc setMode = (SetModeFunc)GetProcAddress(hDll, "setMode");
    SetLicenseFunc setLicense = (SetLicenseFunc)GetProcAddress(hDll, "setLicense");
    
    LockFunc lock = (LockFunc)GetProcAddress(hDll, "lock");
    
    SetRecTypeFunc setRecType = (SetRecTypeFunc)GetProcAddress(hDll, "setRecType");
    SetDetBoxTypeFunc setDetBoxType = (SetDetBoxTypeFunc)GetProcAddress(hDll, "setDetBoxType");
    SetDetUnclipRatioFunc setDetUnclipRatio = (SetDetUnclipRatioFunc)GetProcAddress(hDll, "setDetUnclipRatio");
    SetRecScoreThresholdFunc setRecScoreThreshold = (SetRecScoreThresholdFunc)GetProcAddress(hDll, "setRecScoreThreshold");
    SetReturnTypeFunc setReturnType = (SetReturnTypeFunc)GetProcAddress(hDll, "setReturnType");
    SetBinaryThreshFunc setBinaryThresh = (SetBinaryThreshFunc)GetProcAddress(hDll, "setBinaryThresh");
    SetRunModeFunc setRunMode = (SetRunModeFunc)GetProcAddress(hDll, "setRunMode");
    SetFilterColorFunc setFilterColor = (SetFilterColorFunc)GetProcAddress(hDll, "setFilterColor");
    
    OcrFileFunc ocrFile = (OcrFileFunc)GetProcAddress(hDll, "ocrFile");
    OcrScreenFunc ocrScreen = (OcrScreenFunc)GetProcAddress(hDll, "ocrScreen");
    OcrDMImageDataFunc ocrDMImageData = (OcrDMImageDataFunc)GetProcAddress(hDll, "ocrDMImageData");
    FindTapPointFunc findTapPoint = (FindTapPointFunc)GetProcAddress(hDll, "findTapPoint");
    FindTapPointsFunc findTapPoints = (FindTapPointsFunc)GetProcAddress(hDll, "findTapPoints");
    
    UnlockFunc unlock = (UnlockFunc)GetProcAddress(hDll, "unlock");

    
    //加载dll
    HINSTANCE hDll = LoadLibrary(L"D:\\TomatoOCR.dll");
    if (hDll == NULL) {
        cout << "加载加载TomatoOCR DLL失败!" < 0) {
        setRecType(obj, "ch-3.0");
        // 注:ch、ch-2.0、ch-3.0版可切换使用,对部分场景可适当调整
        // "ch":普通中英文识别,1.0版模型
        // "ch-2.0":普通中英文识别,2.0版模型
        // "ch-3.0":普通中英文识别,3.0版模型
        // "cht":繁体,"japan":日语,"korean":韩语
    
        setDetBoxType(obj, "rect"); // 调整检测模型检测文本参数- 默认"rect": 由于手机上截图文本均为矩形文本,从该版本之后均改为rect,"quad":可准确检测倾斜文本
        setDetUnclipRatio(obj, 1.9f); // 调整检测模型检测文本参数 - 默认1.9: 值范围1.6-2.5之间
        setRecScoreThreshold(obj, 0.1f); // 识别得分过滤 - 默认0.1,值范围0.1-0.9之间
        setReturnType(obj, "json");
        // 返回类型 - 默认"json": 包含得分、坐标和文字;
        // "text":纯文字;
        // "num":纯数字;
        // 自定义输入想要返回的文本:".¥1234567890",仅只返回这些内容
    
        setBinaryThresh(obj, 0); // 二值化设定,非必须
        setRunMode(obj, "slow"); // 默认“slow”;“fast”:小图识别上会加速,但准确率会降低,推荐用默认值“slow”
        //setFilterColor(obj, "", "black"); //  设置滤色值和背景色(black\white),滤色值默认是空的,详细使用见方法说明
      
        int type = 3;
        // type=0 : 只检测
        // type=1 : 方向分类 + 识别
        // type=2 : 只识别
        // type=3 : 检测 + 识别
            
        // 例子一
        const char* result  = ocrFile(obj, "D:\\WorkSpaceProjects\\PycharmProjects\\TestLoadTomato\\test_dll\\images\\02.png", type);
        cout << "OCR: " << result << endl;
    
        
        // 例子二,传入屏幕坐标,进行识别,outImagePath:可以输出裁剪的图片,该参数可传可不传
        const char* outImagePath = "D:\\test.bmp";
        const char* res = ocrScreen(obj, 0, 0, 200, 100, type, outImagePath);
        cout << "OCR: " << res << endl;
    
        
        // 例子三,兼容大漠的方法
        long data;
        long size = 0;
        g_dm->GetScreenDataBmp(0, 0, 200, 100, &data, &size); // 大漠截图方法
        const char* charPtr = reinterpret_cast(data);
        const char* res = ocrDMImageData(obj, charPtr, type);
        cout << "OCR: " << res << endl;
        
        // 例子四,QT兼容大漠的方法
        // dm->GetScreenDataBmp(0, 0, 200, 100, data,size) // 大漠截图方法
        // QImage image = QImage::fromData((const uchar *) data.toInt(), size.toInt());
        // QByteArray byteArray;
        // QBuffer buffer(&byteArray);
        // image.save(&buffer, "bmp");
        // buffer.close();
        // ocrDMImageData(obj, byteArray, type);
    
    
        // 找字
        std::string input = u8"子"; // 确保这是有效的 UTF-8 字符串
        char* inputCStr = const_cast(input.c_str());
        const char* tapPoint = findTapPoint(obj, inputCStr);
        cout << u8"字符'子'的位置: " << tapPoint << endl;
        
        std::string inputs = u8"子"; // 确保这是有效的 UTF-8 字符串
        char* inputsCStr = const_cast(inputs.c_str());
        const char* tapPoints = findTapPoints(obj, inputsCStr);
        cout << u8"字符'子'的位置: " << tapPoints << endl;
        
        unlock(obj); // 释放句柄
    }

    FreeLibrary(hDll);
    return 0;
    
}

运行结果

总结

        提供的各种版本的dll,完全满足各种语言的需求,实现代码复用、节省资源、动态加载、便于维护,并支持模块化开发和多语言协作,c++\c#\python\java等等,关注我的专栏,所以的语言加载方式都会呈现。

你可能感兴趣的:(自动化,c++,开发语言)