rust-rpa 0.2.0-beta.7 → 0.2.0-beta.8

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 CHANGED
@@ -211,7 +211,7 @@ const windows = Window.all();
211
211
  - `setBounds(bounds): Promise<void>` - 设置窗口边界(位置和大小)
212
212
  - `toJSON(): WindowToJson` - 转为 JSON 可序列化对象
213
213
  - `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取窗口图像;可选 `options.size` 指定目标宽高并自动缩放;`options.from` 为 `'screen'` 时截取窗口所在显示器整屏;`options.region` 指定仅截取逻辑像素区域;`options.autoSize` 默认为 `true`,在 Windows 高 DPI 下自动缩放到逻辑像素尺寸
214
- - `findIcon(template, options?): Promise<MatchResult>` - 在窗口截图中查找模板图标(先截图 autoSize=true,再执行模板匹配)
214
+ - `findIcon(template, options?): Promise<MatchResult | null>` - 在窗口截图中查找模板图标(先截图 autoSize=true,再执行模板匹配),找到返回 MatchResult,未找到返回 null
215
215
  - `recognizeText(): Promise<TextRecognitionResult[]>` - 识别窗口截图中的文字(先截图 autoSize=true,再执行 OCR)
216
216
  - `findText(text, options?): Promise<TextRecognitionResult | null>` - 在窗口截图中查找指定文字,找到返回位置信息,未找到返回 null(先截图 autoSize=true,再执行 OCR);`options.regions` 可指定查找区域列表以提升性能
217
217
  - `waitText(text, options?): Promise<TextRecognitionResult>` - 等待指定文字出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
@@ -245,7 +245,7 @@ const primary = monitors.find(m => m.isPrimary());
245
245
  - `scaleFactor(): number` - 获取缩放因子
246
246
  - `isPrimary(): boolean` - 是否主显示器
247
247
  - `captureImage(options?: CaptureImageOptions): Promise<ImageData>` - 截取显示器图像;可选 `options.size` 指定目标宽高并自动缩放;`options.autoSize` 默认为 `true`,在 Windows 高 DPI 下自动缩放到逻辑像素尺寸
248
- - `findIcon(template, options?): Promise<MatchResult>` - 在显示器截图中查找模板图标(先截图 autoSize=true,再执行模板匹配)
248
+ - `findIcon(template, options?): Promise<MatchResult | null>` - 在显示器截图中查找模板图标(先截图 autoSize=true,再执行模板匹配),找到返回 MatchResult,未找到返回 null
249
249
  - `recognizeText(): Promise<TextRecognitionResult[]>` - 识别显示器截图中的文字(先截图 autoSize=true,再执行 OCR)
250
250
  - `findText(text, options?): Promise<TextRecognitionResult | null>` - 在显示器截图中查找指定文字,找到返回位置信息,未找到返回 null(先截图 autoSize=true,再执行 OCR);`options.regions` 可指定查找区域列表以提升性能
251
251
  - `waitText(text, options?): Promise<TextRecognitionResult>` - 等待指定文字出现,超时抛出错误;`options.timeout` 指定等待时间(默认 3000ms),`options.regions` 可指定查找区域
@@ -281,7 +281,7 @@ const primary = monitors.find(m => m.isPrimary());
281
281
  - `crop(x, y, width, height): Promise<ImageData>` - 裁剪图像
282
282
  - `resize(width, height): Promise<ImageData>` - 缩放图像
283
283
  - `grayscale(): Promise<ImageData>` - 转换为灰度图
284
- - `findIcon(template, options?): Promise<MatchResult>` - 查找模板图标
284
+ - `findIcon(template, options?): Promise<MatchResult | null>` - 查找模板图标,找到返回 MatchResult,未找到返回 null
285
285
  - `recognizeText(): Promise<TextRecognitionResult[]>` - 识别图片中的文字
286
286
  - `findText(text): Promise<TextRecognitionResult | null>` - 查找指定文字
287
287
 
@@ -315,16 +315,27 @@ const primary = monitors.find(m => m.isPrimary());
315
315
  - 大多数情况使用默认参数即可:`findIcon(template)`
316
316
  - 需要调整灵敏度时指定阈值:`findIcon(template, { threshold: 0.9 })`
317
317
  - 在特定区域查找可提高性能:`findIcon(template, { threshold: 0.8, regions: [...] })`
318
+ - **返回值处理**:未找到时返回 `null`,使用时需检查返回值
319
+ ```javascript
320
+ const result = await image.findIcon(template);
321
+ if (result) {
322
+ console.log(`找到图标: (${result.x}, ${result.y})`);
323
+ } else {
324
+ console.log('未找到图标');
325
+ }
326
+ ```
318
327
 
319
328
  **MatchResult 类型:**
320
329
  ```typescript
321
330
  interface MatchResult {
322
331
  found: boolean; // 是否找到匹配
323
332
  score: number; // 相似度分数 (0.0-1.0)
324
- x: number; // 匹配位置 x 坐标
325
- y: number; // 匹配位置 y 坐标
333
+ x: number; // 匹配位置 x 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo)
334
+ y: number; // 匹配位置 y 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo)
326
335
  width: number; // 模板宽度
327
336
  height: number; // 模板高度
337
+ relX: number; // 相对于当前窗口/屏幕左上角的 x 坐标
338
+ relY: number; // 相对于当前窗口/屏幕左上角的 y 坐标
328
339
  }
329
340
  ```
330
341
 
@@ -698,11 +709,13 @@ if (targetWindow) {
698
709
  ```typescript
699
710
  interface TextRecognitionResult {
700
711
  text: string; // 识别到的文字
701
- x: number; // 文字区块左上角 x 坐标(像素)
702
- y: number; // 文字区块左上角 y 坐标(像素)
712
+ x: number; // 文字区块左上角 x 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo)
713
+ y: number; // 文字区块左上角 y 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo)
703
714
  width: number; // 文字区块宽度(像素)
704
715
  height: number; // 文字区块高度(像素)
705
716
  confidence: number; // 置信度(0.0-1.0)
717
+ relX: number; // 相对于当前窗口/屏幕左上角的 x 坐标
718
+ relY: number; // 相对于当前窗口/屏幕左上角的 y 坐标
706
719
  }
707
720
  ```
708
721
 
@@ -796,11 +809,21 @@ interface TextRecognitionResult {
796
809
  - 影响范围:所有依赖截图尺寸的逻辑(OCR、模板匹配等)现在默认使用逻辑坐标
797
810
  - rpa部分命令简化
798
811
 
799
- #### 技术详情
800
-
801
- - 使用 MNN 推理框架,提供高性能本地推理
802
- - 支持 GPU 加速(如果可用)
803
- - OCR 模型内置于 npm 包中,开箱即用
812
+ #### API 变更
813
+
814
+ - **`findIcon` 返回值调整**: `findIcon()` 方法在未找到匹配时现在返回 `null` 而非 `{ found: false, ... }` 对象
815
+ - 找到匹配: 返回 `MatchResult` 对象 `{ found: true, score, x, y, width, height }`
816
+ - 未找到匹配: 返回 `null`
817
+ - 影响范围: `ImageData.findIcon()`, `Window.findIcon()`, `Monitor.findIcon()`
818
+ - 迁移指南: 使用 `if (result) { ... }` 或 `if (result !== null) { ... }` 检查返回值
819
+ ```javascript
820
+ const result = await image.findIcon(template);
821
+ if (result) {
822
+ console.log(`找到图标: (${result.x}, ${result.y}), 置信度: ${result.score}`);
823
+ } else {
824
+ console.log('未找到图标');
825
+ }
826
+ ```
804
827
 
805
828
  ### 0.1.7
806
829
 
package/bin/rpa.js CHANGED
@@ -361,14 +361,13 @@ image.command('find-icon').description('在图片中查找图标')
361
361
  options.regions = [{ x, y, width: w, height: h }];
362
362
  }
363
363
  const result = await img.findIcon(tmpl, options);
364
- if (result.found) {
364
+ if (result) {
365
365
  console.log(c.green(`找到图标`));
366
366
  console.log(`位置: (${result.x}, ${result.y})`);
367
367
  console.log(`大小: ${result.width}x${result.height}`);
368
368
  console.log(`相似度: ${(result.score * 100).toFixed(2)}%`);
369
369
  } else {
370
370
  console.log(c.yellow('未找到图标'));
371
- console.log(`最高相似度: ${(result.score * 100).toFixed(2)}%`);
372
371
  process.exit(1);
373
372
  }
374
373
  });
@@ -448,7 +447,7 @@ monitor.command('find-icon').description('在显示器截图中查找图标')
448
447
  options.regions = [{ x, y, width: w, height: h }];
449
448
  }
450
449
  const result = await mon.findIcon(tmpl, options);
451
- if (result.found) {
450
+ if (result) {
452
451
  console.log(c.green(`找到图标`));
453
452
  console.log(`屏幕位置: (${result.x}, ${result.y})`);
454
453
  console.log(`大小: ${result.width}x${result.height}`);
@@ -624,7 +623,7 @@ window.command('find-icon').description('在窗口截图中查找图标')
624
623
  options.regions = [{ x, y, width: w, height: h }];
625
624
  }
626
625
  const result = await win.findIcon(tmpl, options);
627
- if (result.found) {
626
+ if (result) {
628
627
  console.log(c.green(`找到图标`));
629
628
  console.log(`窗口内位置: (${result.x}, ${result.y})`);
630
629
  console.log(`大小: ${result.width}x${result.height}`);
package/index.d.ts CHANGED
@@ -59,22 +59,26 @@ export interface MatchResult {
59
59
  found: boolean
60
60
  /** 相似度分数 (0.0-1.0) */
61
61
  score: number
62
- /** 匹配位置 x 坐标 */
62
+ /** 匹配位置 x 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo) */
63
63
  x: number
64
- /** 匹配位置 y 坐标 */
64
+ /** 匹配位置 y 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo) */
65
65
  y: number
66
66
  /** 模板宽度 */
67
67
  width: number
68
68
  /** 模板高度 */
69
69
  height: number
70
+ /** 相对于当前窗口/屏幕左上角的 x 坐标 */
71
+ relX: number
72
+ /** 相对于当前窗口/屏幕左上角的 y 坐标 */
73
+ relY: number
70
74
  }
71
75
  /** 文字识别结果 */
72
76
  export interface TextRecognitionResult {
73
77
  /** 识别出的文字内容 */
74
78
  text: string
75
- /** 文字区域左上角 x 坐标 */
79
+ /** 文字区域左上角 x 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo) */
76
80
  x: number
77
- /** 文字区域左上角 y 坐标 */
81
+ /** 文字区域左上角 y 坐标(逻辑屏幕坐标,可直接用于 Mouse.moveTo) */
78
82
  y: number
79
83
  /** 文字区域宽度 */
80
84
  width: number
@@ -82,6 +86,10 @@ export interface TextRecognitionResult {
82
86
  height: number
83
87
  /** 识别置信度 (0.0-1.0) */
84
88
  confidence: number
89
+ /** 相对于当前窗口/屏幕左上角的 x 坐标 */
90
+ relX: number
91
+ /** 相对于当前窗口/屏幕左上角的 y 坐标 */
92
+ relY: number
85
93
  }
86
94
  /** 截图时可选的目标尺寸,配置后会将截取到的图像缩放到指定宽高 */
87
95
  export interface CaptureSize {
@@ -107,6 +115,10 @@ export interface ClickResult {
107
115
  x: number
108
116
  /** 点击的 y 坐标(屏幕坐标) */
109
117
  y: number
118
+ /** 相对于当前窗口/屏幕左上角的 x 坐标 */
119
+ relX: number
120
+ /** 相对于当前窗口/屏幕左上角的 y 坐标 */
121
+ relY: number
110
122
  }
111
123
  /** captureImage 的选项对象,便于后续扩展更多参数 */
112
124
  export interface CaptureImageOptions {
@@ -424,7 +436,7 @@ export declare class ImageData {
424
436
  /** 缩放图片(异步) */
425
437
  resize(newWidth: number, newHeight: number): Promise<ImageData>
426
438
  /** 在图片中查找模板图标(异步) */
427
- findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult>
439
+ findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult | null>
428
440
  /**
429
441
  * 识别图片中的文字(异步)
430
442
  *
@@ -549,12 +561,12 @@ export declare class Monitor {
549
561
  * const monitors = Monitor.all();
550
562
  * const template = await ImageData.fromFile('button.png');
551
563
  * const result = await monitors[0].findIcon(template);
552
- * if (result.found) {
564
+ * if (result) {
553
565
  * console.log(`Found at: (${result.x}, ${result.y})`);
554
566
  * }
555
567
  * ```
556
568
  */
557
- findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult>
569
+ findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult | null>
558
570
  /**
559
571
  * 识别显示器截图中的文字(异步)
560
572
  *
@@ -933,7 +945,7 @@ export declare class Window {
933
945
  *
934
946
  * # Returns
935
947
  *
936
- * 返回 Promise<MatchResult>
948
+ * 返回 Promise<MatchResult | null>,找到返回结果,未找到返回 null
937
949
  *
938
950
  * # Example
939
951
  *
@@ -944,12 +956,12 @@ export declare class Window {
944
956
  * const window = windows.find(w => w.appName().includes('Chrome'));
945
957
  * const template = await ImageData.fromFile('button.png');
946
958
  * const result = await window.findIcon(template);
947
- * if (result.found) {
959
+ * if (result) {
948
960
  * console.log(`Found at: (${result.x}, ${result.y})`);
949
961
  * }
950
962
  * ```
951
963
  */
952
- findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult>
964
+ findIcon(template: ImageData, options?: MatchOptionsJs | undefined | null): Promise<MatchResult | null>
953
965
  /**
954
966
  * 识别窗口中的文字(异步)
955
967
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rust-rpa",
3
- "version": "0.2.0-beta.7",
3
+ "version": "0.2.0-beta.8",
4
4
  "description": "Rust-based RPA automation library for Node.js",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
@@ -69,9 +69,9 @@
69
69
  "commander": "^14.0.3"
70
70
  },
71
71
  "optionalDependencies": {
72
- "@alibot/rust-rpa-win32-x64-msvc": "0.2.0-beta.7",
73
- "@alibot/rust-rpa-win32-ia32-msvc": "0.2.0-beta.7",
74
- "@alibot/rust-rpa-darwin-x64": "0.2.0-beta.7",
75
- "@alibot/rust-rpa-darwin-arm64": "0.2.0-beta.7"
72
+ "@alibot/rust-rpa-win32-x64-msvc": "0.2.0-beta.8",
73
+ "@alibot/rust-rpa-win32-ia32-msvc": "0.2.0-beta.8",
74
+ "@alibot/rust-rpa-darwin-x64": "0.2.0-beta.8",
75
+ "@alibot/rust-rpa-darwin-arm64": "0.2.0-beta.8"
76
76
  }
77
77
  }