rust-rpa 0.1.7 → 0.2.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -9
- package/bin/rpa.js +516 -1
- package/index.d.ts +501 -1
- package/index.js +37 -14
- package/models/PP-OCRv5_mobile_det.mnn +0 -0
- package/{rust-rpa.win32-x64-msvc.node → models/PP-OCRv5_mobile_rec.mnn} +0 -0
- package/models/README.md +61 -0
- package/models/ppocrv5_dict.txt +18383 -0
- package/package.json +15 -4
- package/rust-rpa.darwin-arm64.node +0 -0
- package/rust-rpa.darwin-x64.node +0 -0
- package/rust-rpa.win32-ia32-msvc.node +0 -0
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
- 🖥️ **多显示器支持**: Monitor 类,支持多显示器设置
|
|
15
15
|
- 📸 **屏幕截图**: 支持全屏和窗口截图,多种图片格式
|
|
16
16
|
- 🔑 **权限管理**: Permission 类,自动检测并弹出系统权限授权对话框
|
|
17
|
+
- 🔍 **OCR 文字识别**: 基于 PP-OCRv5_mobile 模型,支持中英日文字识别及文字位置查找
|
|
17
18
|
|
|
18
19
|
## 安装
|
|
19
20
|
|
|
@@ -216,7 +217,14 @@ const windows = Window.all();
|
|
|
216
217
|
- `getBounds(): WindowBounds` - 获取窗口边界(位置和大小)
|
|
217
218
|
- `setBounds(bounds): Promise<void>` - 设置窗口边界(位置和大小)
|
|
218
219
|
- `toJSON(): WindowToJson` - 转为 JSON 可序列化对象
|
|
219
|
-
- `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取窗口图像;可选 `options.size` 指定目标宽高并自动缩放;`options.from` 为 `'screen'` 时截取窗口所在显示器整屏;`options.region` 指定仅截取逻辑像素区域;`options.autoSize`
|
|
220
|
+
- `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取窗口图像;可选 `options.size` 指定目标宽高并自动缩放;`options.from` 为 `'screen'` 时截取窗口所在显示器整屏;`options.region` 指定仅截取逻辑像素区域;`options.autoSize` 默认为 `true`,在 Windows 高 DPI 下自动缩放到逻辑像素尺寸
|
|
221
|
+
- `findIcon(template, options?): Promise<MatchResult>` - 在窗口截图中查找模板图标(先截图 autoSize=true,再执行模板匹配)
|
|
222
|
+
- `recognizeText(): Promise<TextRecognitionResult[]>` - 识别窗口截图中的文字(先截图 autoSize=true,再执行 OCR)
|
|
223
|
+
- `findText(text, options?): Promise<TextRecognitionResult | null>` - 在窗口截图中查找指定文字,找到返回位置信息,未找到返回 null(先截图 autoSize=true,再执行 OCR);`options.regions` 可指定查找区域列表以提升性能
|
|
224
|
+
- `waitText(text, options?): Promise<TextRecognitionResult>` - 等待指定文字出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
|
|
225
|
+
- `waitIcon(template, options?): Promise<MatchResult>` - 等待指定图标出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
|
|
226
|
+
- `clickText(text, options?): Promise<void>` - 点击指定文字,先等待文字出现,然后点击文字中心点
|
|
227
|
+
- `clickIcon(template, options?): Promise<void>` - 点击指定图标,先等待图标出现,然后点击图标中心点
|
|
220
228
|
|
|
221
229
|
### Monitor 类
|
|
222
230
|
|
|
@@ -243,7 +251,14 @@ const primary = monitors.find(m => m.isPrimary());
|
|
|
243
251
|
- `height(): number` - 获取显示器高度
|
|
244
252
|
- `scaleFactor(): number` - 获取缩放因子
|
|
245
253
|
- `isPrimary(): boolean` - 是否主显示器
|
|
246
|
-
- `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取显示器图像;可选 `options.size`
|
|
254
|
+
- `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取显示器图像;可选 `options.size` 指定目标宽高并自动缩放;`options.autoSize` 默认为 `true`,在 Windows 高 DPI 下自动缩放到逻辑像素尺寸
|
|
255
|
+
- `findIcon(template, options?): Promise<MatchResult>` - 在显示器截图中查找模板图标(先截图 autoSize=true,再执行模板匹配)
|
|
256
|
+
- `recognizeText(): Promise<TextRecognitionResult[]>` - 识别显示器截图中的文字(先截图 autoSize=true,再执行 OCR)
|
|
257
|
+
- `findText(text, options?): Promise<TextRecognitionResult | null>` - 在显示器截图中查找指定文字,找到返回位置信息,未找到返回 null(先截图 autoSize=true,再执行 OCR);`options.regions` 可指定查找区域列表以提升性能
|
|
258
|
+
- `waitText(text, options?): Promise<TextRecognitionResult>` - 等待指定文字出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
|
|
259
|
+
- `waitIcon(template, options?): Promise<MatchResult>` - 等待指定图标出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
|
|
260
|
+
- `clickText(text, options?): Promise<void>` - 点击指定文字,先等待文字出现,然后点击文字中心点
|
|
261
|
+
- `clickIcon(template, options?): Promise<void>` - 点击指定图标,先等待图标出现,然后点击图标中心点
|
|
247
262
|
|
|
248
263
|
### ImageData 类
|
|
249
264
|
|
|
@@ -274,6 +289,8 @@ const primary = monitors.find(m => m.isPrimary());
|
|
|
274
289
|
- `resize(width, height): Promise<ImageData>` - 缩放图像(使用 Lanczos3 高质量算法)
|
|
275
290
|
- `grayscale(): Promise<ImageData>` - 转换为灰度图(使用标准加权算法:0.299R + 0.587G + 0.114B)
|
|
276
291
|
- `findIcon(template, options?): Promise<MatchResult>` - 查找模板图标(模板匹配)
|
|
292
|
+
- `recognizeText(): Promise<TextRecognitionResult[]>` - 识别图片中的文字(OCR)
|
|
293
|
+
- `findText(text): Promise<TextRecognitionResult | null>` - 查找指定文字,找到返回位置信息,未找到返回 null(OCR)
|
|
277
294
|
|
|
278
295
|
**数据访问**
|
|
279
296
|
|
|
@@ -344,7 +361,7 @@ interface CaptureImageOptions {
|
|
|
344
361
|
// 以下仅对 Window.captureImage 有效:
|
|
345
362
|
from?: 'window' | 'screen'; // 默认 'window' 截窗口;'screen' 截窗口所在显示器整屏(可配合 getBounds 与 image.crop 裁剪出窗口)
|
|
346
363
|
region?: CaptureRegion | null; // 仅截取该逻辑像素区域,不填则截全图
|
|
347
|
-
autoSize?: boolean | null; //
|
|
364
|
+
autoSize?: boolean | null; // 默认为 true,在 Windows 存在 DPI 缩放时自动将图像缩放到逻辑像素宽高(与 getSize/getBounds 一致)
|
|
348
365
|
}
|
|
349
366
|
```
|
|
350
367
|
|
|
@@ -401,7 +418,32 @@ interface CaptureImageOptions {
|
|
|
401
418
|
- `Permission.checkAccessibility(prompt?): boolean` - 检查辅助功能权限(鼠标、键盘、窗口操作等需要此权限)
|
|
402
419
|
- `Permission.checkScreenCapture(prompt?): boolean` - 检查屏幕录制权限(截图功能需要此权限)
|
|
403
420
|
|
|
421
|
+
### pause 函数
|
|
422
|
+
|
|
423
|
+
暂停/等待指定时间的辅助函数。
|
|
424
|
+
|
|
425
|
+
#### 函数签名
|
|
426
|
+
|
|
427
|
+
- `pause(ms: number): Promise<void>` - 等待指定毫秒数
|
|
428
|
+
|
|
429
|
+
```javascript
|
|
430
|
+
const { pause } = require('rust-rpa');
|
|
431
|
+
|
|
432
|
+
// 等待 1 秒
|
|
433
|
+
await pause(1000);
|
|
434
|
+
|
|
435
|
+
// 在操作之间添加延迟
|
|
436
|
+
await Mouse.click('left');
|
|
437
|
+
await pause(500); // 等待 500ms
|
|
438
|
+
await Keyboard.typeText('Hello');
|
|
439
|
+
```
|
|
440
|
+
|
|
404
441
|
**参数说明:**
|
|
442
|
+
- `ms`:等待时间,单位为毫秒
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
**Permission 类参数说明:**
|
|
405
447
|
- `prompt`(可选,默认 `true`):无权限时是否自动弹出系统授权对话框(仅 macOS 生效)
|
|
406
448
|
|
|
407
449
|
**平台行为差异:**
|
|
@@ -606,11 +648,75 @@ await Clipboard.pasteImage('image.png');
|
|
|
606
648
|
await Clipboard.pasteFile('/path/to/file.pdf');
|
|
607
649
|
```
|
|
608
650
|
|
|
609
|
-
###
|
|
651
|
+
### OCR 文字识别
|
|
652
|
+
|
|
653
|
+
利用 PP-OCRv5_mobile 模型,支持在 **ImageData**、**Window** 和 **Monitor** 上直接执行 OCR。
|
|
610
654
|
|
|
611
655
|
```javascript
|
|
612
|
-
const
|
|
613
|
-
|
|
656
|
+
const { Monitor, Window, ImageData } = require('rust-rpa');
|
|
657
|
+
|
|
658
|
+
// --- ImageData 正中 OCR ---
|
|
659
|
+
const image = await ImageData.fromFile('screenshot.png');
|
|
660
|
+
|
|
661
|
+
// 识别图片中所有文字
|
|
662
|
+
const results = await image.recognizeText();
|
|
663
|
+
for (const result of results) {
|
|
664
|
+
console.log(`文字: ${result.text}`);
|
|
665
|
+
console.log(` 位置: (${result.x}, ${result.y}), 大小: ${result.width}x${result.height}`);
|
|
666
|
+
console.log(` 置信度: ${(result.confidence * 100).toFixed(1)}%`);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// 查找指定文字
|
|
670
|
+
const found = await image.findText('确定');
|
|
671
|
+
if (found) {
|
|
672
|
+
console.log(`找到"确定"在: (${found.x}, ${found.y})`);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// --- Monitor 上 OCR ---
|
|
676
|
+
const monitors = Monitor.all();
|
|
677
|
+
const monitorResults = await monitors[0].recognizeText();
|
|
678
|
+
console.log(`显示器上数到 ${monitorResults.length} 条文字`);
|
|
679
|
+
|
|
680
|
+
// 在显示器上查找文字
|
|
681
|
+
const monitorFound = await monitors[0].findText('文件');
|
|
682
|
+
if (monitorFound) {
|
|
683
|
+
console.log(`在显示器上找到"文件": (${monitorFound.x}, ${monitorFound.y})`);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// --- Window 上 OCR ---
|
|
687
|
+
const windows = Window.all();
|
|
688
|
+
const targetWindow = windows.find(w => w.appName().includes('Chrome'));
|
|
689
|
+
if (targetWindow) {
|
|
690
|
+
// 识别窗口中的文字(内部先截图)
|
|
691
|
+
const windowResults = await targetWindow.recognizeText();
|
|
692
|
+
console.log(`Chrome 窗口中有 ${windowResults.length} 条文字`);
|
|
693
|
+
|
|
694
|
+
// 在窗口中查找指定文字
|
|
695
|
+
const windowFound = await targetWindow.findText('搜索');
|
|
696
|
+
if (windowFound) {
|
|
697
|
+
console.log(`在窗口中找到"搜索": (${windowFound.x}, ${windowFound.y})`);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
**TextRecognitionResult 类型:**
|
|
703
|
+
```typescript
|
|
704
|
+
interface TextRecognitionResult {
|
|
705
|
+
text: string; // 识别到的文字
|
|
706
|
+
x: number; // 文字区块左上角 x 坐标(像素)
|
|
707
|
+
y: number; // 文字区块左上角 y 坐标(像素)
|
|
708
|
+
width: number; // 文字区块宽度(像素)
|
|
709
|
+
height: number; // 文字区块高度(像素)
|
|
710
|
+
confidence: number; // 置信度(0.0-1.0)
|
|
711
|
+
}
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
> **注意**: OCR 功能需要预先下载模型文件到 `models/` 目录下。模型文件分别为:
|
|
715
|
+
> - `PP-OCRv5_mobile_det.mnn` — 文字检测模型(4.7M)
|
|
716
|
+
> - `PP-OCRv5_mobile_rec.mnn` — 文字识别模型(16M)
|
|
717
|
+
> - `ppocr_keys_v5.txt` — 字符集
|
|
718
|
+
|
|
719
|
+
|
|
614
720
|
|
|
615
721
|
// 方式 1: 从截图获取图片
|
|
616
722
|
const monitors = Monitor.all();
|
|
@@ -741,13 +847,58 @@ npm test
|
|
|
741
847
|
- [x] 图像保存到文件
|
|
742
848
|
- [x] 剪贴板操作
|
|
743
849
|
- [x] 窗口操作(置顶、移动、调整大小、父进程查询)
|
|
744
|
-
- [
|
|
745
|
-
- [
|
|
746
|
-
- [
|
|
850
|
+
- [x] OCR 文字识别(基于 PP-OCRv5_mobile)
|
|
851
|
+
- [x] 文字位置查找(findText)
|
|
852
|
+
- [x] 等待文字、图标出现(waitText/waitIcon)
|
|
853
|
+
- [x] 点击文字、图标(clickText/clickIcon)
|
|
747
854
|
- [ ] 进程管理
|
|
748
855
|
|
|
749
856
|
## 更新日志
|
|
750
857
|
|
|
858
|
+
### 0.2.0
|
|
859
|
+
|
|
860
|
+
#### 新功能
|
|
861
|
+
|
|
862
|
+
- **OCR 文字识别**: 新增 `recognizeText()` 和 `findText()` 方法
|
|
863
|
+
- 基于 PP-OCRv5_mobile 模型(检测模型 4.7M + 识别模型 16M)
|
|
864
|
+
- 支持中文、英文、日文识别
|
|
865
|
+
- `ImageData.recognizeText()` - 识别图片中的所有文字,返回文字列表及其位置信息
|
|
866
|
+
- `ImageData.findText(text, options?)` - 在图片中查找指定文字,返回匹配文字的位置信息;`options.regions` 可指定查找区域列表以提升性能
|
|
867
|
+
- **Window 类增强**: 新增 `findIcon()`、`recognizeText()`、`findText()`、`waitText()`、`waitIcon()`、`clickText()`、`clickIcon()` 方法
|
|
868
|
+
- 所有方法内部先执行 `captureImage({ autoSize: true })`,然后在截图上执行对应操作
|
|
869
|
+
- `Window.findIcon(template, options?)` - 在窗口截图中查找图标
|
|
870
|
+
- `Window.recognizeText()` - 识别窗口中的文字
|
|
871
|
+
- `Window.findText(text, options?)` - 在窗口中查找指定文字
|
|
872
|
+
- `Window.waitText(text, options?)` - 等待指定文字出现,超时抛出错误
|
|
873
|
+
- `Window.waitIcon(template, options?)` - 等待指定图标出现,超时抛出错误
|
|
874
|
+
- `Window.clickText(text, options?)` - 点击指定文字
|
|
875
|
+
- `Window.clickIcon(template, options?)` - 点击指定图标
|
|
876
|
+
- **Monitor 类增强**: 新增 `findIcon()`、`recognizeText()`、`findText()`、`waitText()`、`waitIcon()`、`clickText()`、`clickIcon()` 方法
|
|
877
|
+
- 所有方法内部先执行 `captureImage({ autoSize: true })`,然后在截图上执行对应操作
|
|
878
|
+
- `Monitor.findIcon(template, options?)` - 在显示器截图中查找图标
|
|
879
|
+
- `Monitor.recognizeText()` - 识别显示器截图中的文字
|
|
880
|
+
- `Monitor.findText(text, options?)` - 在显示器截图中查找指定文字
|
|
881
|
+
- `Monitor.waitText(text, options?)` - 等待指定文字出现,超时抛出错误
|
|
882
|
+
- `Monitor.waitIcon(template, options?)` - 等待指定图标出现,超时抛出错误
|
|
883
|
+
- `Monitor.clickText(text, options?)` - 点击指定文字
|
|
884
|
+
- `Monitor.clickIcon(template, options?)` - 点击指定图标
|
|
885
|
+
|
|
886
|
+
#### BREAKCHANGE
|
|
887
|
+
|
|
888
|
+
- **`captureImage` 的 `autoSize` 默认值改为 `true`**: `Monitor.captureImage()` 和 `Window.captureImage()` 的 `autoSize` 选项默认值从 `false` 改为 `true`
|
|
889
|
+
- 现在默认自动将图像缩放到逻辑像素尺寸,与 `getBounds()`/`getSize()` 返回的坐标一致
|
|
890
|
+
- 如需物理像素图像,需显式传入 `autoSize: false`
|
|
891
|
+
- 影响范围:所有依赖截图尺寸的逻辑(OCR、模板匹配等)现在默认使用逻辑坐标
|
|
892
|
+
|
|
893
|
+
#### 技术详情
|
|
894
|
+
|
|
895
|
+
- 使用 `ocr-rs` crate 提供 PaddleOCR 功能
|
|
896
|
+
- 使用 MNN 推理框架,提供高性能本地推理
|
|
897
|
+
- 支持 GPU 加速(如果可用)
|
|
898
|
+
- 模型文件需要自行下载放置在 `models/` 目录
|
|
899
|
+
|
|
900
|
+
## 更新日志
|
|
901
|
+
|
|
751
902
|
### 0.1.7
|
|
752
903
|
|
|
753
904
|
#### BREAKCHANGE
|
|
@@ -761,6 +912,22 @@ npm test
|
|
|
761
912
|
- 此前 Windows 上正数向上滚动,与 macOS 相反
|
|
762
913
|
- 现在双平台方向一致:正数向下,负数向上
|
|
763
914
|
|
|
915
|
+
#### 功能
|
|
916
|
+
|
|
917
|
+
- **所有输入操作自动添加延迟**: `Mouse` 和 `Keyboard` 类的所有操作方法现在自动在返回前添加 50ms 延迟
|
|
918
|
+
- 包括:`moveTo`, `click`, `doubleClick`, `down`, `up`, `scroll`
|
|
919
|
+
- 包括:`Keyboard.click`, `typeText`, `down`, `up`, `sequence`
|
|
920
|
+
- 确保系统有足够时间处理操作,避免后续操作立即执行导致失败
|
|
921
|
+
- **剪贴板操作自动添加延迟**: `Clipboard` 类的写入和粘贴方法现在自动添加 50ms 延迟
|
|
922
|
+
- 包括:`writeText`, `writeImage`, `writeFile`, `paste`
|
|
923
|
+
- 确保剪贴板内容写入完成后再执行后续操作
|
|
924
|
+
- **窗口操作自动添加延迟**: `Window` 类的状态修改方法现在自动添加 50ms 延迟
|
|
925
|
+
- 包括:`bringToFront`, `setBounds`
|
|
926
|
+
- 确保窗口状态变更完成后再执行后续操作
|
|
927
|
+
- **`clickText`/`clickIcon` 返回点击坐标**: `Window.clickText()` 和 `Window.clickIcon()` 现在返回 `ClickResult` 对象
|
|
928
|
+
- 返回 `{ x, y }` 表示实际点击的屏幕坐标
|
|
929
|
+
- 便于调试和验证点击位置
|
|
930
|
+
|
|
764
931
|
### 0.1.6
|
|
765
932
|
|
|
766
933
|
#### BREAKCHANGE
|