我正在使用Tesseract从屏幕上检测数字值,只要我的程序正在运行,我就需要它始终扫描屏幕上的更改。这一直很好,但是我注意到它非常占用CPU。在寻求有关此主题的帮助时,我遇到了这个question here,但是我不确定如何更改代码以重用现有的Tesseract Engine。我将在下面发布示例代码,但是在我的程序中,我需要扫描屏幕的4个不同区域以查找各种数字,所以我现在有4个Tesseract Engines。
以下是一些示例代码:
MainClass在程序处于活动状态时循环。
var thread_Character_CurrentMana = new Thread(Character.Character_CurrentMana)
{
Name = "thread_Character_CurrentMana",
IsBackground = true
};
thread_Character_CurrentMana.Start();
Character.cs
public static void Character_CurrentMana()
{
CurrentMana = CharacterCurrentMana();
}
private static int CharacterCurrentMana()
{
if (UI.MyCurrentMana_Width == -999999999 || UI.MyCurrentMana_Height == -999999999 ||
UI.MyCurrentMana_PositionX == -999999999 ||
UI.MyCurrentMana_PositionY == -999999999)
return -1;
try
{
using (var memoryImage =
new Bitmap(UI.MyCurrentMana_Width, UI.MyCurrentMana_Height))
{
using (var memoryGraphics = Graphics.FromImage(memoryImage))
{
var _size = new Size(UI.MyCurrentMana_Width, UI.MyCurrentMana_Height);
memoryGraphics.CopyFromScreen(UI.MyCurrentMana_PositionX,
UI.MyCurrentMana_PositionY, 0, 0, _size);
using (var ocr = new TesseractEngine(Program.TesseractDataFolder, "eng",
EngineMode.TesseractAndCube))
{
using (var page = ocr.Process(memoryImage, PageSegMode.SingleWord))
{
var value = 0;
if (int.TryParse(page.GetText(), out value))
{
var _MyCurrentMana = Convert.ToInt32(page.GetText());
return _MyCurrentMana;
}
return -1;
}
}
}
}
}
catch
{
return -1;
}
}