05-01-2019, 04:34 AM
I found someone wrote the VBS code.
Rem CODE BY OLDLIU
Option Explicit
Randomize
Dim ExitCode
ExitCode = Main()
WSH.Quit ExitCode
Function Main()
Const OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
If WSH.Arguments.Count = 3 Then
Dim fso,APIKey,SecretKey,Start_Time
Start_Time = Timer
Set fso = CreateObject("Scripting.FileSystemObject")
APIKey = WSH.Arguments(0)
SecretKey = WSH.Arguments(1)
If fso.FileExists(WSH.Arguments(2)) Then
Dim ImageFilePath,Token
ImageFilePath = fso.GetFile(WSH.Arguments(2)).Path 'Processing relative paths
Token = GetToken(APIKey,SecretKey)
If Token <> "ERROR" Then
With CreateObject("HTMLFILE")
.ParentWindow.execScript _
"var ImageBase64String_URLEnc = encodeURIComponent('" & FileBase64Enc(ImageFilePath) & "');"
If Len(.ParentWindow.ImageBase64String_URLEnc) <= 4*1024*1024 Then
.ParentWindow.execScript _
"var JSON = " & _
HTTP_POST( _
OCR_URL & "?access_token=" & Token, _
"image="& .ParentWindow.ImageBase64String_URLEnc & _
"&language_type=CHN_ENG" & _
"&detect_direction=false" & _
"&detect_language=false" _
)
On Error Resume Next
Dim Error_Code
Error_Code = .ParentWindow.JSON.error_code
If Error_Code = Empty Then
On Error Goto 0
Dim i
For i = 0 To .ParentWindow.JSON.words_result_num - 1
WSH.Echo Eval(".ParentWindow.JSON.words_result.[" & i & "].words")
Next
Main = 0
WSH.StdErr.WriteLine "Success, time " & Timer - Start_Time & " sec。"
Else
On Error Goto 0
Main = 5
WSH.StdErr.WriteLine "Error! Code:" & Error_Code & ", msg:" & .ParentWindow.JSON.error_msg
End If
Else
Main = 4
WSH.StdErr.WriteLine "The file is too large."
End If
End With
Else
Main = 3
WSH.StdErr.WriteLine "The APIKey or SecretKey is incorrect."
End If
Else
Main = 2
WSH.StdErr.WriteLine "file not found."
End If
Else
Main = 1
WSH.StdErr.WriteLine "Image Cloud OCR Tool (Baidu Cloud Edition, support Chinese and English mixed)" & vbNewLine & vbNewLine & _
"usage:" & vbNewLine & _
"CSCRIPT -NOLOGO OCRImage.VBS <APIKey> <SecretKey> <ImageFilePath>" & vbNewLine & vbNewLine & _
"APIKey The APIKey that comes with the Baidu Cloud Artificial Intelligence app you created." & vbNewLine & _
"SecretKey The SecretKey that comes with the Baidu Cloud Artificial Intelligence app you created." & vbNewLine & _
"ImageFilePath Path to image file (support relative path)"& vbNewLine & vbNewLine & _
"Baidu Cloud Artificial Intelligence OCR: https://cloud.baidu.com/product/ocr" & vbNewLine & vbNewLine & _
"Output information:" & vbNewLine & _
"Handle 1 OCR results" & vbNewLine & _
"Handle 2 Help, error message, execution time" & vbNewLine & vbNewLine & _
"note:" & vbNewLine & _
"First, does not support XP system." & vbNewLine & _
"Second, the picture size must be less than 3M, it is recommended to be less than 1M. Otherwise it will be very slow (VBS pot, next time Vb.Net), and the recognition effect is very poor or even unrecognizable"
End If
End Function
Function HTTP_POST(ByVal Address,ByVal Args)
With CreateObject("MSXML2.XMLHTTP")
.Open "POST",Address, False
.SetRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
.Send Args
HTTP_POST = .ResponseText
End With
End Function
Function GetToken(ByVal APIKey,ByVal SecretKey)
Const GET_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token"
On Error Resume Next
GetToken = _
Split( _
HTTP_POST( _
GET_TOKEN_URL, _
"grant_type=client_credentials" & _
"&client_id="&APIKey & _
"&client_secret=" & SecretKey _
) _
,"""")(3)
If Err.Number <> 0 Then GetToken = "ERROR"
On Error Goto 0
End Function
Function FileBase64Enc(ByVal strFilePath)
Dim fso,WshShell,TmpFilePath,oReadFile
Const ForReading = 1
Const MyBirthDay = 1228
Const WINDOW_HIDE = 0
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
TmpFilePath = fso.GetSpecialFolder(2) & "\" & Fix(Rnd * MyBirthDay) & ".TMP"
WshShell.Run "Certutil.EXE -ENCODE """ & strFilePath & """ """ & TmpFilePath & """" ,WINDOW_HIDE ,True
Set oReadFile = fso.OpenTextFile(TmpFilePath,ForReading)
oReadFile.ReadLine 'Skip the first line”-----BEGIN CERTIFICATE-----“Sign
Dim Str
While Str <> "-----END CERTIFICATE-----"
FileBase64Enc = FileBase64Enc & Str
Str = oReadFile.ReadLine
Wend
oReadFile.Close
fso.DeleteFile TmpFilePath
End Function