度大师OCR

多编程语言API函数

度大师OCR图像文字识别

通过屏幕截图区域识别文字内容,集成度大师OCR高精度文字识别API

使用示例

以下是不同平台/语言调用度大师OCR API的示例代码,所有示例均遵循"截图→Base64编码→API请求→解析结果"的流程,可根据实际开发环境选择对应版本。

Lua 触动精灵
Lua 懒人精灵
易语言
Lua 触动精灵调用示例
require("TSLib")
-- 度大师OCR函数定义
function 度大师ocr(x1, y1, x2, y2)
    -- 1. 截取屏幕指定区域并保存
    snapshot("1.png", x1, y1, x2, y2)
    
    -- 2. 加载所需库
    local sz = require("sz")
    local ts = require("ts")
    local http = require("szocket.http")
    local ltn12 = require("ltn12")
    
    -- 3. 将图片转换为Base64编码
    local post_data = imageBase64(userPath() .. "/res/1.png")
    
    -- 4. 发送HTTP请求到OCR API
    local response_body = {}
    local staus, code, header = http.request{
        url = "https://w.duds.cn/api/ocr_proxy.php?apikey=e7f0ed01c3ccf84a6ee25db8da89ffc61&mode=1",
        method = "POST",
        headers = {
            ["Content-Type"] = "application/json",
            ["Content-Length"] = #post_data,
        },
        source = ltn12.source.string(post_data),
        sink = ltn12.sink.table(response_body),
    }
    
    -- 5. 处理并返回识别结果
    local result = table.concat(response_body)
    if result ~= "" then
        return result  
    else
        return "未知"  -- 请求失败时返回默认值
    end
end

-- 调用示例:识别屏幕(100,200)到(500,350)区域的文字
local result = 度大师ocr(100, 200, 500, 350)
nLog("识别结果: " .. result)
Lua 懒人精灵调用示例
-- 度大师OCR函数定义
function 度大师OCR(x1, y1, x2, y2)
    -- 1. 截取屏幕指定区域并保存
    snapShot("/mnt/sdcard/test.png", x1, y1, x2, y2)
    
    -- 2. 加载所需库
    local https = require("ssl.https")
    local ltn12 = require("ltn12")
    
    -- 3. 将图片转换为Base64编码
    local post_data = getFileBase64("/mnt/sdcard/test.png")
    
    -- 4. 发送HTTP请求到OCR API
    local response_body = {}
    local headers = {
        ["Content-Type"] = "application/json",
        ["Content-Length"] = tostring(#post_data)
    }
    
    local res, code, response_headers = https.request{
        url = "https://w.duds.cn/api/ocr_proxy.php?apikey=3dbded15a9d5b35f550b50480a93bf1e&mode=1",
        method = "POST",
        headers = headers,
        source = ltn12.source.string(post_data),
        sink = ltn12.sink.table(response_body)
    }
    
    -- 5. 处理并返回识别结果
    local result = table.concat(response_body)
    if result ~= "" then
        return result
    else
        return "未知"  -- 请求失败时返回默认值
    end
end

-- 调用示例:识别屏幕(100,200)到(500,350)区域的文字
local result = 度大师OCR(100, 200, 500, 350)
print("识别结果: " .. result, 0)
易语言调用示例
.版本 2
.支持库 spec

.子程序 度大师OCR, 文本型, 公开
.参数 文件路径, 文本型, , 待识别的图片路径
.局部变量 网址, 文本型
.局部变量 方式, 整数型
.局部变量 结果, 文本型
.局部变量 提交数据, 文本型
.局部变量 协议头, 文本型
.局部变量 body_base64, 文本型

' 1. 将图片文件转换为Base64编码
body_base64 = 编码_BASE64编码 (读入文件 (文件路径))

' 2. 配置API请求参数
网址 = “https://w.duds.cn/api/ocr_proxy.php?apikey=3ad5df2297231478f4c251f90d3741cd&mode=1”
方式 = 1  ' 1表示POST请求
协议头 = “Content-Type: application/json”
提交数据 = body_base64

' 3. 发送请求并获取结果
结果 = UTF8到文本 (网页_访问 (网址, 方式, 提交数据, , , 协议头, ))

' 4. 返回处理结果
.如果 (结果 ≠ “”)
    调试输出 (结果)  ' 调试模式下输出结果
    返回 (结果)
.否则
    返回 (“没有识别内容”)
.如果结束

' 调用示例(在其他子程序中):
' 局部变量 识别结果, 文本型
' 识别结果 = 度大师OCR (“C:\test.png”)
' 信息框 (识别结果, 0, )