函数:第三方ocr 文字识别
函数名称:第三方ocr
函数功能:调用指定接口将指定区域的图像转换为其显示出的文字
函数方法
text = 第三方ocr(x1, y1, x2, y2,apikey,mode)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
x1 | number | 是 | 欲识别的区域左上角顶点屏幕横坐标 |
y1 | number | 是 | 欲识别的区域左上角顶点屏幕纵坐标 |
x2 | number | 是 | 欲识别的区域右下角顶点屏幕横坐标 |
y2 | number | 是 | 欲识别的区域右下角顶点屏幕纵坐标 |
apikey | string | 是 | 调用接口的 apikey |
mode | number | 否 | 识别模式,空默认为 1,具体介绍详见对应官网 |
返回值 | 类型 | 说明 |
---|---|---|
text | string | 识别结果 |
函数用例
-- 加载所需库
local ts = require("ts")
local sz = require("sz")
require("TSLib")
local http = require("szocket.http")
local ltn12 = require("ltn12")
function 第三方ocr(x1, y1, x2, y2,apikey,mode)
if mode==nil then
mode=1
end
-- 屏幕截图
snapshot("1.png", x1, y1, x2, y2)
-- 转换图片为Base64
local post_data = imageBase64(userPath() .. "/res/1.png")
-- 准备HTTP请求
local response_body = {}
local staus, code, header = http.request{
url = "https://w.duds.cn/api/ocr_proxy.php?apikey="..apikey.."&mode="..mode,
method = "POST",
headers = {
["Content-Type"] = "application/json",
["Content-Length"] = #post_data,
},
source = ltn12.source.string(post_data),
sink = ltn12.sink.table(response_body),
}
-- 处理响应
local text = table.concat(response_body)
if text ~= "" then
nLog(text)
else
-- 所有 URL 都尝试失败后返回"未知"
dialog("未知")
end
end
第三方ocr(14,337,229,415,"123456789123")
注意事项