Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invoke web service
#3
I found someone wrote the VBS code.
 
Code:
Copy      Help
 
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


Messages In This Thread
How to invoke web service - by win - 04-30-2019, 06:41 AM
RE: How to invoke web service - by win - 04-30-2019, 11:27 PM
RE: How to invoke web service - by win - 05-01-2019, 04:34 AM
RE: How to invoke web service - by redbull2k - 05-01-2019, 04:43 PM
RE: How to invoke web service - by win - 05-01-2019, 11:38 PM
RE: How to invoke web service - by win - 05-02-2019, 01:45 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 02:30 AM
RE: How to invoke web service - by win - 05-02-2019, 02:34 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 02:55 AM
RE: How to invoke web service - by win - 05-02-2019, 03:07 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 03:08 AM
RE: How to invoke web service - by win - 05-02-2019, 03:11 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 03:55 AM
RE: How to invoke web service - by win - 05-02-2019, 04:04 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 04:16 AM
RE: How to invoke web service - by win - 05-02-2019, 05:24 AM
RE: How to invoke web service - by Kevin - 05-02-2019, 05:42 AM
RE: How to invoke web service - by win - 05-02-2019, 06:50 AM
RE: How to invoke web service - by win - 05-02-2019, 11:20 AM
RE: How to invoke web service - by win - 05-03-2019, 03:47 AM
RE: How to invoke web service - by redbull2k - 05-03-2019, 02:21 PM
RE: How to invoke web service - by redbull2k - 05-03-2019, 03:26 PM
RE: How to invoke web service - by win - 05-03-2019, 10:10 PM
RE: How to invoke web service - by Kevin - 05-04-2019, 01:27 AM
RE: How to invoke web service - by win - 05-04-2019, 01:57 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 02:32 AM
RE: How to invoke web service - by win - 05-04-2019, 02:56 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 03:04 AM
RE: How to invoke web service - by win - 05-04-2019, 03:13 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 03:17 AM
RE: How to invoke web service - by win - 05-04-2019, 03:27 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 03:33 AM
RE: How to invoke web service - by win - 05-04-2019, 03:40 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 04:19 AM
RE: How to invoke web service - by win - 05-04-2019, 04:29 AM
RE: How to invoke web service - by Kevin - 05-04-2019, 04:36 AM
RE: How to invoke web service - by redbull2k - 05-04-2019, 10:14 PM
RE: How to invoke web service - by win - 05-04-2019, 11:05 PM
RE: How to invoke web service - by redbull2k - 05-04-2019, 11:50 PM
RE: How to invoke web service - by win - 05-05-2019, 02:25 AM
RE: How to invoke web service - by win - 05-05-2019, 07:39 AM
RE: How to invoke web service - by win - 05-05-2019, 10:51 AM
RE: How to invoke web service - by redbull2k - 05-05-2019, 01:02 PM
RE: How to invoke web service - by win - 05-05-2019, 01:18 PM
RE: How to invoke web service - by redbull2k - 05-05-2019, 01:22 PM
RE: How to invoke web service - by win - 05-05-2019, 01:24 PM
RE: How to invoke web service - by redbull2k - 05-05-2019, 02:01 PM
RE: How to invoke web service - by win - 05-05-2019, 03:15 PM
RE: How to invoke web service - by Kevin - 05-05-2019, 08:36 PM
RE: How to invoke web service - by win - 05-05-2019, 09:15 PM
RE: How to invoke web service - by Kevin - 05-05-2019, 09:39 PM
RE: How to invoke web service - by win - 05-05-2019, 11:54 PM
RE: How to invoke web service - by win - 05-06-2019, 01:22 AM
RE: How to invoke web service - by Kevin - 05-06-2019, 02:19 AM
RE: How to invoke web service - by win - 05-06-2019, 02:33 AM
RE: How to invoke web service - by Kevin - 05-06-2019, 02:44 AM
RE: How to invoke web service - by win - 05-06-2019, 02:50 AM
RE: How to invoke web service - by Kevin - 05-06-2019, 03:03 AM
RE: How to invoke web service - by win - 05-06-2019, 03:11 AM
RE: How to invoke web service - by win - 05-06-2019, 02:05 PM
RE: How to invoke web service - by win - 05-06-2019, 10:00 PM
RE: How to invoke web service - by win - 05-06-2019, 11:03 PM
RE: How to invoke web service - by redbull2k - 05-06-2019, 11:31 PM
RE: How to invoke web service - by Kevin - 05-06-2019, 11:40 PM
RE: How to invoke web service - by redbull2k - 05-06-2019, 11:44 PM
RE: How to invoke web service - by win - 05-07-2019, 12:07 AM
RE: How to invoke web service - by Kevin - 05-07-2019, 12:31 AM
RE: How to invoke web service - by redbull2k - 05-07-2019, 12:33 AM
RE: How to invoke web service - by win - 05-07-2019, 12:36 AM
RE: How to invoke web service - by Kevin - 05-07-2019, 01:06 AM
RE: How to invoke web service - by win - 05-07-2019, 01:11 AM
RE: How to invoke web service - by Kevin - 05-07-2019, 02:42 AM
RE: How to invoke web service - by win - 05-07-2019, 03:00 AM
RE: How to invoke web service - by win - 05-07-2019, 01:31 PM
RE: How to invoke web service - by win - 05-07-2019, 11:52 PM
RE: How to invoke web service - by redbull2k - 05-08-2019, 12:45 AM
RE: How to invoke web service - by win - 05-08-2019, 12:49 AM
RE: How to invoke web service - by redbull2k - 05-08-2019, 12:55 AM
RE: How to invoke web service - by win - 05-08-2019, 01:01 AM
RE: How to invoke web service - by redbull2k - 05-08-2019, 01:30 AM
RE: How to invoke web service - by win - 05-08-2019, 01:42 AM
RE: How to invoke web service - by redbull2k - 05-08-2019, 01:56 AM
RE: How to invoke web service - by win - 05-08-2019, 02:10 AM
RE: How to invoke web service - by win - 05-09-2019, 01:40 AM

Forum Jump:


Users browsing this thread: 30 Guest(s)