【全网唯一】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,将文件放置在项目中

这里以DllImport(非托管DLL)为例:

复制代码
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void init(int a);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setMode(string mode);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setHttpIntervalTime(int second);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern string setLicense(string a, string b);


[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern IntPtr startLock();

[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void stopLock(IntPtr obj);


[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setRecType(IntPtr obj, string value);

[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setDetBoxType(IntPtr obj, string value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setDetUnclipRatio(IntPtr obj, float value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setRecScoreThreshold(IntPtr obj, float value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setReturnType(IntPtr obj, string value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setBinaryThresh(IntPtr obj, int value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setRunMode(IntPtr obj, string value);
 
[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern void setFilterColor(IntPtr obj, string value);


[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern string ocrFile(IntPtr obj, string path, int type);

[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern string ocrScreen(IntPtr obj, int x1, int y1, int x2, int y2, int type, string path);

[DllImport("TomatoOCR.dll", CallingConvention = CallingConvention.Cdecl)]
 static extern string ocrBase64(IntPtr obj, string value, int type);

// ---------------下面开始识别---------------

init(2); // 初始化两个线程
// setMode("dev")
// setHttpIntervalTime(28800)
string license = ""; // 设置license,见授权码获取
string remark = ""; // 设置备注
string flag = setLicense(license, remark);
Console.WriteLine(flag);

// *******************************以上是初始化代码,只需要写一次************************************
IntPtr obj = startLock(); // 获取句柄,与stopLock一一对应
if (obj > 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); // 二值化设定,非必须, 0-255
    setRunMode(obj, "slow"); // 默认"slow";"fast":小图识别上会加速,但准确率会降低,推荐用默认值"slow"
    //setFilterColor(obj, "", "black"); //  设置滤色值和背景色(black\white),滤色值默认是空的,详细使用见方法说明
      
    int type = 3;
    // type=0 : 只检测
    // type=1 : 方向分类 + 识别
    // type=2 : 只识别
    // type=3 : 检测 + 识别
        
    // 例子一
    string result1 = ocrFile(obj, "D:\\WorkSpaceProjects\\PycharmProjects\\TestLoadTomato\\test_dll\\images\\02.png", type);
    Console.WriteLine(result1);
    
    // 例子二,传入屏幕坐标,进行识别,outImagePath:可以输出裁剪的图片,该参数可传可不传
    string outImagePath = "D:\\test.bmp";
    string result2 = ocrScreen(obj, 0, 0, 200, 100, type, outImagePath);
    Console.WriteLine(result2);
    
    // 找字
    string tapPoint = findTapPoint(obj, "百度");
    Console.WriteLine(tapPoint);
    
    string tapPoints = findTapPoints(obj, "百度");
    Console.WriteLine(tapPoints);
    
    stopLock(obj); // 释放句柄
}

运行

总结

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

相关推荐
froginwe112 分钟前
Web 品质国际化
开发语言
亮子AI3 分钟前
【Svelte】怎样实现一个图片上传功能?
开发语言·前端·javascript·svelte
shehuiyuelaiyuehao4 分钟前
7类和对象
java·开发语言
落羽的落羽6 分钟前
【C++】深入浅出“图”——图的基本概念与存储结构
服务器·开发语言·数据结构·c++·人工智能·机器学习·图搜索算法
bugcome_com6 分钟前
C# 中 Overload(重载)与 Override(重写)的核心区别与实战解析
开发语言·c#
巴塞罗那的风12 分钟前
从蓝图到执行:智能体中的“战略家思维
开发语言·后端·ai·语言模型·golang
JAVA+C语言15 分钟前
Python新手学习
开发语言·python·学习
六bring个六24 分钟前
文件工具类(一)
开发语言·文件操作工具类
Matlab光学44 分钟前
MATLAB仿真:离轴干涉法实现光学全息加密与解密
开发语言·matlab
小鸡吃米…1 小时前
Python - JSON
开发语言·python·json