在使用封装函数前先说一下,怎么获取必要的api和Secret:
准备工作做好就可以使用我的函数了。
Import "shanhai.lua" Dim 路径="/sdcard/pictures/tu.jpg" SnapShot(路径) Dim arr=BaiDuOCR("7h4Y7Ejb21kUrXYpxhmQRfnt","zgGwEcIcXFCWrsqmALYLGzrnGq2qaMjy",路径,1) TracePrint arr Function BaiDuOCR(api, Secret, path, n) TracePrint "文字识别中,请耐心等待..." Dim ret_arr() Dim token=url.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="&api&"&client_secret="&secret) dim mytoken=Encode.JsonToTable(token) Dim base64=ShanHai.ReadFileBase(path) Dim 图片=shanhai.CharToUrl(base64) Dim post内容="access_token="&mytoken["access_token"]&"&Content-Type=application/x-www-form-urlencoded&image="&图片&"&detect_direction=true" Dim myjson=url.post("https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic",post内容) myjson = Encode.JsonToTable(myjson) For i = 0 To UBOUND(myjson["words_result"]) ret_arr(i)=myjson["words_result"][i+1]["words"] Next If n = 0 Then BaiDuOCR = ret_arr ElseIf n = 1 Then BaiDuOCR=join(ret_arr,"") End If End Function
在代码第4行是调用函数,这里我直接留了我自己的百度云识别密钥,如果是测试的话,可以直接使用,如果是你正式的脚本记得使用自己的密钥。下面介绍一下这个函数的参数如何填写:
BaiDuOCR(api, Secret, path,n)共有4个参数:
参数1:api key的值
参数2:Secret key的值
参数3:要识别图片
参数4:返回值的类型,0代表数组,1代表字符串(注:识别多行内容可存入数组)