app.ocr - 文字识别 API
基于 Vision 框架的光学字符识别(OCR)。
需要 macOS 10.15+
方法
app.ocr.recognize(imagePath, opts?)
识别图片中的文字。
参数:
imagePath(string) - 图片文件路径opts(table, 可选):language(string|array) - 识别语言(如"zh-Hans",{"zh-Hans", "en"})level(string) - 识别级别:"accurate"(默认) 或"fast"rect(table) - 识别区域(像素坐标):{x, y, w, h}
返回值: array<table>, error - 每项包含:
text(string) - 识别的文字confidence(number) - 置信度(0.0-1.0)
-- 识别整张图片
local results, err = app.ocr.recognize("/path/to/image.png")
if results then
for _, item in ipairs(results) do
app.log.info(item.text .. " (" .. string.format("%.0f%%", item.confidence * 100) .. ")")
end
end
-- 指定语言和区域
local results = app.ocr.recognize("/path/to/image.png", {
language = {"zh-Hans", "en"},
level = "accurate",
rect = {x = 100, y = 50, w = 500, h = 200}
})
app.ocr.recognizeFromScreen(opts?)
截取屏幕并识别文字。
参数:
opts(table, 可选):displayId(number) - 显示器 IDrect(table) - 截取区域:{x, y, w, h}language(string|array) - 识别语言level(string) - 识别级别
返回值: array<table>, error - 同 recognize()
-- 截取整个屏幕并识别
local results, err = app.ocr.recognizeFromScreen()
-- 截取指定区域
local results = app.ocr.recognizeFromScreen({
rect = {x = 0, y = 0, w = 800, h = 600},
language = "en"
})
app.ocr.languages()
获取系统支持的 OCR 语言列表。
返回值: array<string>, error
local langs = app.ocr.languages()
-- {"en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant", ...}
示例
提取图片中的文字
function MyPlugin:handleOCR(context)
local file = context.selectedFiles[1]
app.progress.show("识别中", {message = "正在识别文字..."})
local results, err = app.ocr.recognize(file, {
language = {"zh-Hans", "en"},
level = "accurate"
})
app.progress.hide()
if not results then
app.dialog.alert("错误", err or "识别失败")
return
end
if #results == 0 then
app.dialog.alert("结果", "未识别到文字")
return
end
local text = ""
for _, item in ipairs(results) do
text = text .. item.text .. "\n"
end
app.clipboard.setText(text)
app.notification.show("完成", "识别到 " .. #results .. " 段文字,已复制到剪贴板")
end