跳到主要内容
版本:3.0.2

屏幕文字提取

如果屏幕上的文字清晰且具备一定的对比度,那么可以使用光学字符识别(OCR)技术,精准提取和定位屏幕上的文字。

XXTouchNG 以桥接 Apple 提供的 Vision 框架的方式提供了 Vision OCR。在快速(Fast)模式下,采用字符检测、字符识别技术,识别结果较为模糊,但速度较快;在精准(Accurate)模式下,则采用基于神经网络引擎的文本检测、识别技术,识别结果较为准确,但速度较慢。Vision OCR 在 iOS 13 上仅支持英文,iOS 14 以上可以支持中文。

XXTouchNG 还提供了 Tessaract OCR,但是识别效果不如 Vision OCR,本节中并不会展开介绍。

试一试!Vision OCR

在 VSCode 工作区新建 vision-ocr.lua 脚本,输入以下内容:

local function tapWord(word)
local txts, details =
screen.ocr_text {
languages = {"en-US"},
words = {word},
confidence = 0.8
}
local tapped = false
for i, v in ipairs(txts) do
if v == word then
touch.tap(details[i].center[1], details[i].center[2])
tapped = true
end
end
return tapped
end

nLog("Open “Settings”…")
app.run("com.apple.Preferences") -- 打开 “设置” 应用
sys.sleep(2) -- 延迟 2 秒

nLog("Test gestures…")
touch.show_pose(true) -- 显示手指位置
while not tapWord("App Store") do
touch.on(375, 1100)
:move(375, 275)
:step_delay(20) -- 每一步的延迟
:step_len(1) -- 每一步的长度
:move(375, 255)
:off()
sys.sleep(1)
end

运行脚本,可以看到脚本会自动打开 “设置” 应用。随后,持续多次向下划动 “设置” 列表,直到找到 “App Store” 文字并点击它为止。

信息

如果划动始终没有停止,可以点击 VSCode 窗口右上角的 “远程停止” 以强行终止脚本。