识别指定window窗口的文本
1. 简单需求
通过图文识别读取一个指定window窗口的文本。 获取窗口句柄,截图保存成bitmap ,调用图文识别库.
测试结果是对中文下的识别不是特别好。
需要注意的是,tessdata要下载指定目录页下。
2. 引用包
a. 引用 tesseract4.1 b. Emgu.CV组件
3. 上代码
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using Tesseract;
using System.Windows.Forms;
using Pix = Tesseract.Pix;
using PageSegMode = Tesseract.PageSegMode;
public class Program
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 0x0001;
private const int KEYEVENTF_KEYUP = 0x0002;
private const int VK_MENU = 0x12; // Alt
private const int VK_S = 0x53; // S key
public static Bitmap CaptureWindow(IntPtr hWnd)
{
RECT rect;
GetWindowRect(hWnd, out rect);
Bitmap bmp = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
return bmp;
}
public static void Main()
{
string windowTitle = "*无标题 - 记事本";
//*无标题 - 记事本
IntPtr hWnd = FindWindow(null, windowTitle);
if (hWnd == IntPtr.Zero)
{
Console.WriteLine($"Could not find window with title '{windowTitle}'");
return;
}
SetForegroundWindow(hWnd);
//SendKeys.SendWait("%(s)"); //发送Alt+S事件
// Press Alt + S
//keybd_event(VK_MENU, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
//keybd_event(VK_S, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
//// Release Alt + S
//keybd_event(VK_S, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
//keybd_event(VK_MENU, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
//图文识别
Bitmap bmp = CaptureWindow(hWnd);
//tessdata/tessdata-4.1.0
TesseractEngine engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndLstm);
using (Bitmap bmpImage = new Bitmap(bmp))
{
byte[] imageData = ImageToByte(bmpImage);
using (var image = Pix.LoadFromMemory(imageData))
{
using (var page = engine.Process(image))
{
var text = page.GetText();
Console.WriteLine("Text recognized from image: {0}", text);
}
}
}
}
private static byte[] ImageToByte(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
}
上述代码使用的是net6.0 开发的。控制台程序模拟windows事件,尤其是键盘快捷键时,有点问题。暂未处理。
就这样吧,兴趣所致,就是玩玩。 天太冷了,不想写的太多。 纯粹凑字数。
识别指定window窗口的文本的更多相关文章
- (转载)JavaScript中的Window窗口对象
(转载)http://www.ijavascript.cn/jiaocheng/javascript-window-65.html 例子: <html> <head> < ...
- Window 窗口类
窗口类 WNDCLASS 总结 总结为下面的几个问题: . 什么是窗口类 . 窗口类的三种类型 . 窗口类各字段含义 . 窗口类的注册和注销 . 如何使用窗口类,子类化.超类化是什么 下面分别描述: ...
- EasyUI笔记(三)Window窗口
本系列只列出一些常用的属性.事件或方法,具体完整知识请查看API文档 Window(窗口) 窗口控件是一个浮动和可拖拽的面板可以用作应用程序窗口.默认情况下,窗口可以移动,调整大小和关闭.它的内容也可 ...
- [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...
- JavaScript:window窗口对象
在JavaScript中,window表示的就是一个窗口对象.所以在整个处理过程之中,所有的操作都是以弹框为主 的.范例1:使用警告框 <script type="text/javas ...
- [转] C#中发送消息给指定的窗口,以及接收消息
原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...
- Directshow 通过 put_Owner 指定显示窗口后,自动刷新问题
在Directshow中,我们可以对render指定显示窗口,在写程序的过程中, 发现通过put_Owner设置的显示窗口存在自动刷新问题,譬如窗口被遮挡然后再次露出时,被遮挡部分不能自动刷新,需要拖 ...
- ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
本篇讲解三个容器类控件. 一.面板控件 Ext.Panel 一个面板控件包括几个部分,有标题栏.工具栏.正文.按钮区.标题栏位于最上面,工具栏可以在四个位置放置,围绕中间部分正文,按钮区位于最小方.下 ...
- 玩转Web之JavaScript(三)-----javaScript语法总结(三) 窗口/滚动条/文本的相关语法
JS语法集锦(三) 窗口/滚动条/文本 alert("文本") 警告框:警告框经常用于确保用户可以得到某些信息,当警告框出现后,用户需要点击确定按钮才能继续进行操作. con ...
- easyUi弹出window窗口传值与调用父页面的方法,子页面给父页面赋值
<!-- 父页面 --> <!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "ht ...
随机推荐
- uview-ui toast 二次封装
开发用到uview 的toast 很常用的内容使用却很繁琐 所以做了简单封装方便使用 前后对比: this.$refs.uToast.show({ type: 'success', title: '成 ...
- 花样玩转“所见即所得”的可视化开发UI
随着技术的发展,用户对软件的界面美观度和交互体验的要求越来越高.在这样的背景下,可视化开发UI(User Interface)成为了提升用户体验和开发效率的重要工具. 通过图形界面来设计和构建用户界面 ...
- SMU Summer 2023 Contest Round 2
SMU Summer 2023 Contest Round 2 A. Treasure Hunt 当\(x1 - x2\)的差值与\(y1-y2\)的差值都能被\(x,y\)整除时,且商之和为2的倍数 ...
- AtCoder Beginner Contest 327 D
AtCoder Beginner Contest 327D D - Good Tuple Problem (atcoder.jp)(种类并查集,二分图染色) 算法学习笔记(7):种类并查集 附上典题: ...
- zuul集成apollo动态刷新配置
zuul集成apollo实现路由配置的动态刷新 import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip. ...
- 新员工一口气写完了这些C语言例子,领导给他转正了!
持续更新中... 很多想从事嵌入式Linux开发的老铁问一口君,有没有快速提升自己编程水平的小例子? 一口君根据自己多年工作经验,整理了一些基于Linux的c语言的非常实用的小例子, 这些例子在嵌入式 ...
- 瑞芯微|rk3568 uart快速上手
一.调试环境 平台:rk3568 kernel: 4.19.232 SDK: rk_android11.0_sdk Board: rk3568-evb1-ddr4-v10 二. rk3568 uart ...
- Excel 导入的开发经验
2020 年开始接触 Java Excel 导入的开发工作. 一家建筑机器人的公司离职后,来到广州找的是一家工厂,开始接触导入的开发工作.之前也没有什么使用开发经验, 是 教学视频 中看到过有些老 ...
- 一键k8s企业级集群部署(以k8s的1.18.0版本为例)
一.下载安装sealos wget https://github.com/fanux/sealos/releases/download/v3.2.0-beta.2/sealos && ...
- NumPy从入门到放弃
看前建议: 本文以jupyter notebook为编辑器进行示例,建议有一定python基础后再进行学习. python的安装:https://www.cnblogs.com/scfssq/p/17 ...