文字识别 OCR
对截图或指定区域做 OCR。结果对象含 text、rect、confidence。截图与找图见 图片 Images,权限见 概述。
findText / findTextFirst / findTextCenter / findTextPosition / findTextRegionsInRegion / waitText / waitTextGone 的 mode 都是同一套(匹配 OCR 整行文字,不是切单字)。可省略,默认 contains。
mode | 含义 | 第二参数 |
|---|---|---|
"contains"(默认) | 行文本包含关键字 | 普通字符串,如 "登录" |
"exact" | 整行文字完全相等 | 必须和 OCR 那一行一字不差 |
"regex" | Java 正则,行内 find | 如 "\\d+"、"^下一.?$";非法正则当不匹配 |
getTextAndRegion(imageFile)
返回
{text, rect, confidence}[]
let screen = Images.capture();
let lines = Images.getTextAndRegion(screen);
for (let i = 0; i < lines.length; i++) {
let item = lines[i];
console.log(item.text, item.confidence, item.rect.left, item.rect.top);
}getTextAndRegionInRegion(imageFile, left, top, width, height)
区域内识别,同样返回带坐标结果
let lines = Images.getTextAndRegionInRegion(Images.capture(), 0, 0, 1080, 600);
for (let i = 0; i < lines.length; i++) {
console.log(lines[i].text, lines[i].rect.centerX(), lines[i].rect.centerY());
}getText(imageFile)
返回拼接后的整段文字(行间换行)
let all = Images.getText(Images.capture());
console.log(all);getTextInRegion(imageFile, left, top, width, height)
区域内拼接文字
let part = Images.getTextInRegion(Images.capture(), 0, 100, 1080, 400);
console.log(part);ocr()
内部截屏后全图识别
let lines = Images.ocr();
console.log(lines.length, lines[0] && lines[0].text);ocr(left, top, width, height)
截屏后对区域识别
let lines = Images.ocr(0, 0, 1080, 500);
console.log(lines);hasText(imageFile, keyword)
是否包含关键字(contains)
let screen = Images.capture();
if (Images.hasText(screen, "登录")) {
console.log("找到登录");
}hasTextExact(imageFile, keyword)
是否存在整行精确匹配
if (Images.hasTextExact(Images.capture(), "确定")) {
console.log("存在精确「确定」");
}hasTextMatches(imageFile, regex)
是否匹配正则(行内 find)
if (Images.hasTextMatches(Images.capture(), "\\d{4,}")) {
console.log("存在至少4位数字");
}findText(imageFile, keyword, mode?)
返回
TextAndRegion[]。第三参数mode见上文表格,默认contains
let hits = Images.findText(Images.capture(), "关注");
for (let i = 0; i < hits.length; i++) {
console.log(hits[i].text, hits[i].confidence);
}
// 指定模式
let regHits = Images.findText(Images.capture(), "\\d+", "regex");findTextExact(imageFile, keyword)
精确匹配文字行
let hits = Images.findTextExact(Images.capture(), "下一步");
console.log(hits.length);findTextMatches(imageFile, regex)
正则匹配
let hits = Images.findTextMatches(Images.capture(), "¥\\d+(\\.\\d+)?");
console.log(hits);findTextFirst(imageFile, keyword, mode?)
返回第一个匹配,或
null。第三参数mode同上表,默认contains
let first = Images.findTextFirst(Images.capture(), "搜索");
if (first) {
console.log(first.text, first.rect.left, first.rect.top);
}
let exactFirst = Images.findTextFirst(Images.capture(), "确定", "exact");findTextCenter(imageFile, keyword, mode?)
返回匹配文字中心点
Point,或null。第三参数mode同上表,默认contains
let p = Images.findTextCenter(Images.capture(), "登录");
if (p) {
Hid.tap(p.x, p.y);
}findTextPosition(imageFile, keyword, mode?)
返回匹配行的
Rect[](兼容旧用法)。第三参数mode:contains(默认)/exact/regex,见上文表格。不传第三参数等同"contains"。
let screen = Images.capture();
// 默认 contains:行里包含「百度一下」
let rects = Images.findTextPosition(screen, "百度一下");
for (let i = 0; i < rects.length; i++) {
console.log(rects[i].left, rects[i].top, rects[i].width(), rects[i].height());
}
Images.findTextPosition(screen, "确定", "exact");
Images.findTextPosition(screen, "\\d{4,}", "regex");findTextInRegion(imageFile, left, top, width, height)
区域内识别到的文字字符串,返回
string[](只有文字,没有坐标)。按关键字找坐标用findTextRegionsInRegion。
let arr = Images.findTextInRegion(Images.capture(), 0, 0, 1080, 1920);
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}findTextRegionsInRegion(imageFile, keyword, left, top, width, height)
区域内按关键字查找,返回
TextAndRegion[]。mode默认contains。
let hits = Images.findTextRegionsInRegion(Images.capture(), "关注", 0, 200, 1080, 800);
if (hits.length > 0) {
console.log(hits[0].text, hits[0].rect.centerX(), hits[0].rect.centerY());
}findTextRegionsInRegion(imageFile, keyword, mode, left, top, width, height)
带匹配模式的区域查找
let hits = Images.findTextRegionsInRegion(
Images.capture(),
"^下一.?$",
"regex",
0, 0, 1080, 400
);
console.log(hits);waitText(keyword, timeoutMs?, intervalMs?, mode?)
截屏轮询直到文字出现;超时返回
null
let hit = Images.waitText("加载完成", 10000);
if (hit) {
console.log("出现:", hit.text);
}
// 精确匹配 + 自定义间隔
Images.waitText("完成", 8000, 300, "exact");waitTextGone(keyword, timeoutMs?, intervalMs?, mode?)
等到文字消失,返回
boolean
if (Images.waitTextGone("加载中", 8000)) {
console.log("加载文案已消失");
}与 Hid 配合
Hid.ensureConnected();
Hid.clickText("登录", 5000);
Hid.waitText("完成", 8000);详见 Hid 方法。
