计算机键盘图片及功能百度云,电脑版能不能百度云识别

本文介绍如何使用VBS脚本调用百度OCR API进行图片的文字识别,包括获取访问令牌、图片转Base64编码及发送POST请求等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dim bs64

Dim tokenid

Dim path

path=  "C:\Users\Administrator\Desktop\1.PNG"   //图片路径自选

tokenid =  gettokenid()

bs64 = BToBase64(path)

bs64 = URLEncode(bs64)

TracePrint postret

Import "C:\vbsjson.vbs"

Set json = New vbsJson

Set o = json.Decode(postret)

//返回 = o("words_result")(0)("words")

//If 返回 > "" Then

//TracePrint 返回

//End If

Function Postret()

Dim BodyTXT

Set WinHttp = CreateObject("Microsoft.XMLHTTP")

WinHttp.Open "POST", "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token="&tokenid, False

WinHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"

WinHttp.SetRequestHeader "Content-Length", len(bs64)

WinHttp.Send("&image=" & bs64  )

// 超时设置 60秒 默认

j = 0

i = Now

Do

Delay 100

j = DateDiff("s", i, Now)

Loop Until (WinHttp.ReadyState = 4 Or j > 60)

BodyTXT = winhttp.responsetext

If Len(BodyTXT) = 0 Then

Postret = ""

Exit Function

End If

Postret = BodyTXT

End Function

Function BToBase64(path)

Dim ADS,MSD, node

Set ADS = CreateObject("ADODB.Stream")

ADS.Type = 1

ADS.Open

ADS.LoadFromFile path'加载图片,path为图片路径

Set MSD = CreateObject("Msxml2.DOMDocument")

Set node = MSD.CreateElement("Base64Data")

node.DataType = "bin.base64"

node.NodeTypedValue =  ADS.Read()

BToBase64 = node.Text

Set node = Nothing

Set MSD = Nothing

Set ADS = Nothing

End Function

Function URLEncode(EncodeStr)

Dim i

Dim erg

erg = EncodeStr

' *** First replace '%' chr

erg = Replace(erg, "%", Chr(1))

' *** then '+' chr

erg = Replace(erg, "+", Chr(2))

For i = 0 To 255

If i=1 or i =37 or i = 43  or (i >=48 and i <= 57) or (i>=65 and  i <=90 ) or (i>=97 and i <=122)

Delay 1

ElseIf i = 2

erg = Replace(erg, Chr(i), "%2B")

ElseIf i = 32

erg = Replace(erg, Chr(i), "+")

Elseif (i>=3  and  i <=15 )

erg = Replace(erg, Chr(i), "%0" & Hex(i))

Else

erg = Replace(erg, Chr(i), "%" & Hex(i))

End If

Next

URLEncode = erg

End Function

Function gettokenid()

Dim sendstr

Dim Bodytext

dim tokedid

Dim arr, brr

//client_id与client_secret的值需替换成你自己百度ocr的

sendstr = "&grant_type=client_credentials&client_id=你的ID&client_secret=你的ID"

Set WinHttp = CreateObject("Microsoft.XMLHTTP")

WinHttp.Open "POST", "https://aip.baidubce.com/oauth/2.0/token", False

WinHttp.SetRequestHeader "Content-Type", "text/html"

WinHttp.SetRequestHeader "Content-Length", len(sendstr)

WinHttp.Send( sendstr )

// 超时设置 60秒 默认

j = 0

i = Now

Do

Delay 100

j = DateDiff("s", i, Now)

Loop Until (WinHttp.ReadyState = 4 Or j > 60)

//    TracePrint winhttp.responsetext

arr = Split(winhttp.responsetext, "access_token")

brr = Split(arr(1), chr(34))

gettokenid = brr(2)

End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值