【按鍵精靈】50. OCR文字識別

Import "D:\按鍵精靈\按鍵精靈2014\plugin\vbsjson.vbs"
Set json = New vbsJson
Call 獲取access_token
//TracePrint ImagesToBase64("D:\微信收款二維碼.png")
//Call 網(wǎng)絡圖片OCR
//Call 本地圖片OCR
//Call 用戶選擇文件
Call 區(qū)域截圖OCR
Sub 獲取access_token
// 從ini配置文件讀取 API_KEY 和 SECRET_KEY
API_KEY = Plugin.File.ReadINI("key", "API_KEY", "D:\百度OCR.ini")
SECRET_KEY = Plugin.File.ReadINI("key", "SECRET_KEY", "D:\百度OCR.ini")
// 獲取access_token
Set tokenPost = CreateObject("Msxml2.ServerXMLHTTP.3.0")
tokenPost.Open "Post", "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" & API_KEY & "&client_secret=" & SECRET_KEY, False
tokenPost.Send?
If tokenPost.readyState = 4 Then?
// TracePrint tokenPost.responsetext
Set obj = json.Decode(tokenPost.responsetext)
// TracePrint obj("access_token")
access_token = obj("access_token")
End If
tokenPost.abort
Set tokenPost = Nothing
End Sub
Sub 網(wǎng)絡圖片OCR
Set xPost = CreateObject("Msxml2.ServerXMLHTTP.3.0")
xPost.Open "Post", "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" & access_token, False
xPost.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
xPost.Send ("url=http://www.kuwwz.com/img/%E5%BE%AE%E4%BF%A1%E6%94%B6%E6%AC%BE%E4%BA%8C%E7%BB%B4%E7%A0%81.png")
If xPost.readyState=4 Then
?? TracePrint xPost.responsetext
End If
xPost.abort
Set xPost = Nothing
End Sub
Sub 區(qū)域截圖OCR
Call Plugin.Pic.PrintScreen(0, 0, 1000, 100, "D:\OCR測試截圖.bmp")
Set xPost = CreateObject("Msxml2.ServerXMLHTTP.3.0")
xPost.Open "Post", "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" & access_token, False
xPost.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
base64Param = "image=" & ImagesToBase64("D:\OCR測試截圖.bmp")
xPost.Send (base64Param)
If xPost.readyState=4 Then
?? TracePrint xPost.responsetext
?? Set obj = json.Decode(xPost.responsetext)
?? wordsArray = obj("words_result")
?? For i = 0 To UBound(wordsArray)
?? TracePrint wordsArray(i)("words")
?? Next
End If
xPost.abort
Set xPost = Nothing
End Sub
Sub 用戶選擇文件
filepath = Plugin.File.SelectFile()
Set xPost = CreateObject("Msxml2.ServerXMLHTTP.3.0")
xPost.Open "Post", "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" & access_token, False
xPost.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
base64Param = "image=" & ImagesToBase64(filepath)
xPost.Send (base64Param)
If xPost.readyState=4 Then
?? TracePrint xPost.responsetext
?? Set obj = json.Decode(xPost.responsetext)
?? wordsArray = obj("words_result")
?? For i = 0 To UBound(wordsArray)
?? TracePrint wordsArray(i)("words")
?? Next
End If
xPost.abort
Set xPost = Nothing
End Sub
Sub 本地圖片OCR
Set xPost = CreateObject("Msxml2.ServerXMLHTTP.3.0")
xPost.Open "Post", "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" & access_token, False
xPost.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
base64Param = "image=" & ImagesToBase64("D:\微信收款二維碼.png")
xPost.Send (base64Param)
If xPost.readyState=4 Then
?? TracePrint xPost.responsetext
?? Set obj = json.Decode(xPost.responsetext)
?? wordsArray = obj("words_result")
?? For i = 0 To UBound(wordsArray)
?? TracePrint wordsArray(i)("words")
?? Next
End If
xPost.abort
Set xPost = Nothing
End Sub
// 圖片轉(zhuǎn)Base64
Function ImagesToBase64(FilePath)
Dim xml
Dim root
Dim fs
Dim objStream
Dim objXMLDoc
Dim Base64
Set objXMLDoc = CreateObject("Microsoft.XMLDOM") // 可以訪問和操作XML文檔
objXMLDoc.loadXML "<?xml version='1.0' ?><data></data>" // 導入指定字符串的XML文檔
Set fs = createObject("Scripting.FileSystemObject") // 可以操作磁盤、文件夾或文本文件
If fs.FileExists(FilePath) Then // 判斷文件是否存在
'用 stream 來讀取數(shù)據(jù)
Set objStream = CreateObject("ADODB.Stream") // 可以存取二進制數(shù)據(jù)或者文本流
objStream.Type = 1 // 表示二進制數(shù)據(jù)
objStream.Open // 打開objStream
objStream.LoadFromFile FilePath // 加載文件數(shù)據(jù)(下載圖片用SaveToFile)
objXMLDoc.documentElement.dataType = "bin.base64" // 設置節(jié)點數(shù)據(jù)類型
objXMLDoc.documentElement.nodeTypedvalue = objStream.Read // 從objStream讀取,再存儲到根節(jié)點(objXMLDoc.documentElement代表XML文檔的根節(jié)點)
'數(shù)據(jù)流讀取結(jié)束.得到了值 objXMLDoc
'創(chuàng)建XML文件
Set xml = CreateObject("Microsoft.XMLDOM")
xml.load objXMLDoc // 導入指定位置的XML文檔
If xml.ReadyState > 2 Then // 0:未初始化;1:載入;2:載入完成;3:交互;4:完成
Set root = xml.getElementsByTagName("data")// 返回指定名字的節(jié)點集合(可能會有多個重名節(jié)點)
Base64 = root(0).Text
// Base64 = Replace(Base64, vbLf, "") // 去除換行(vbLf相當于chr(10))(可以不去除)
Base64 = Replace(Base64,"+","%2B") // 替換加號(文檔未說明,但需要此操作,而且不要進行urlencode)
Else
Base64 = ""
End If
Set xml = Nothing
Set objStream = Nothing
Else // 文件不存在
Base64 = ""
End If
Set fs = Nothing
Set objXMLDoc = Nothing
ImagesToBase64 = Base64
//TracePrint ImagesToBase64
End Function