Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PureBasic Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PureBasic) Google Cloud Vision Text Detection

Demonstrates calling the Google Cloud Vision for text detection (performs Optical Character Recognition). "Detects and extracts text within an image with support for a broad range of languages. It also features automatic language identification." See https://cloud.google.com/vision/docs/detecting-text

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkBinData.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; Build the following request:

    ; {
    ;   "requests": [
    ;     {
    ;       "image": {
    ;         "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
    ;       },
    ;       "features": [
    ;         {
    ;           "type": "TEXT_DETECTION"
    ;         }
    ;       ]
    ;     }
    ;   ]
    ; }

    ; Use this online tool to generate the code from sample JSON: 
    ; Generate Code to Create JSON

    ; Load an image file.
    imageData.i = CkBinData::ckCreate()
    If imageData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; This image file contains some text...
    success.i = CkBinData::ckLoadFile(imageData,"qa_data/jpg/text.jpg")
    If success <> 1
        Debug "Failed to load image file."
        CkBinData::ckDispose(imageData)
        ProcedureReturn
    EndIf

    ; Create the above JSON.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateBd(json,"requests[0].image.content","base64",imageData)
    CkJsonObject::ckUpdateString(json,"requests[0].features[0].type","TEXT_DETECTION")

    ; Send the following POST with the HTTP request body containing the above JSON.
    ; POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    url.s = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
    resp.i = CkHttp::ckPostJson3(http,url,"application/json",json)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkBinData::ckDispose(imageData)
        CkJsonObject::ckDispose(json)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    Debug "status = " + Str(CkHttpResponse::ckStatusCode(resp))

    ; A 200 response status indicate success.
    If CkHttpResponse::ckStatusCode(resp) <> 200
        Debug CkHttpResponse::ckBodyStr(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkBinData::ckDispose(imageData)
        CkJsonObject::ckDispose(json)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
    CkStringBuilder::ckWriteFile(sbResponseBody,"qa_output/textDetectResponse.json","utf-8",0)
    CkJsonObject::ckLoadSb(json,sbResponseBody)

    ; The response is a JSON document like this:

    ; Use this online tool to generate parsing code from sample JSON: 
    ; Generate Parsing Code from JSON

    ; {
    ;   "responses": [
    ;     {
    ;       "textAnnotations": [
    ;         {
    ;           "locale": "en",
    ;           "description": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n",
    ;           "boundingPoly": {
    ;             "vertices": [
    ;               {
    ;                 "x": 17,
    ;                 "y": 14
    ;               },
    ; 	...
    ;             ]
    ;           }
    ;         ],
    ;         "text": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n"
    ;       }
    ;     }
    ;   ]
    ; }

    ; The parsing code generated from the online tool:

    i.i
    count_i.i
    fullTextAnnotationText.s
    j.i
    count_j.i
    locale.s
    description.s
    k.i
    count_k.i
    x.i
    y.i
    width.i
    height.i
    languageCode.s
    blockType.s
    i1.i
    count_i1.i
    json1.i
    j1.i
    count_j1.i
    k1.i
    count_k1.i
    text.s
    propertyDetectedBreakType.s
    i2.i
    count_i2.i
    json2.i

    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"responses")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        fullTextAnnotationText = CkJsonObject::ckStringOf(json,"responses[i].fullTextAnnotation.text")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"responses[i].textAnnotations")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            locale = CkJsonObject::ckStringOf(json,"responses[i].textAnnotations[j].locale")
            description = CkJsonObject::ckStringOf(json,"responses[i].textAnnotations[j].description")
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(json,"responses[i].textAnnotations[j].boundingPoly.vertices")
            While k < count_k
                CkJsonObject::setCkK(json, k)
                x = CkJsonObject::ckIntOf(json,"responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
                y = CkJsonObject::ckIntOf(json,"responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
                k = k + 1
            Wend
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"responses[i].fullTextAnnotation.pages")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            width = CkJsonObject::ckIntOf(json,"responses[i].fullTextAnnotation.pages[j].width")
            height = CkJsonObject::ckIntOf(json,"responses[i].fullTextAnnotation.pages[j].height")
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(json,"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
            While k < count_k
                CkJsonObject::setCkK(json, k)
                languageCode = CkJsonObject::ckStringOf(json,"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
                k = k + 1
            Wend
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(json,"responses[i].fullTextAnnotation.pages[j].blocks")
            While k < count_k
                CkJsonObject::setCkK(json, k)
                blockType = CkJsonObject::ckStringOf(json,"responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")

                json1 = CkJsonObject::ckObjectOf(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]")
                i1 = 0
                count_i1 = CkJsonObject::ckSizeOfArray(json1,"property.detectedLanguages")
                While i1 < count_i1
                    CkJsonObject::setCkI(json1, i1)
                    languageCode = CkJsonObject::ckStringOf(json1,"property.detectedLanguages[i].languageCode")
                    i1 = i1 + 1
                Wend
                CkJsonObject::ckDispose(json1)

                json1 = CkJsonObject::ckObjectOf(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]")
                i1 = 0
                count_i1 = CkJsonObject::ckSizeOfArray(json1,"boundingBox.vertices")
                While i1 < count_i1
                    CkJsonObject::setCkI(json1, i1)
                    x = CkJsonObject::ckIntOf(json1,"boundingBox.vertices[i].x")
                    y = CkJsonObject::ckIntOf(json1,"boundingBox.vertices[i].y")
                    i1 = i1 + 1
                Wend
                CkJsonObject::ckDispose(json1)

                json1 = CkJsonObject::ckObjectOf(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]")
                i1 = 0
                count_i1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs")
                While i1 < count_i1
                    CkJsonObject::setCkI(json1, i1)
                    j1 = 0
                    count_j1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].property.detectedLanguages")
                    While j1 < count_j1
                        CkJsonObject::setCkJ(json1, j1)
                        languageCode = CkJsonObject::ckStringOf(json1,"paragraphs[i].property.detectedLanguages[j].languageCode")
                        j1 = j1 + 1
                    Wend
                    j1 = 0
                    count_j1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].boundingBox.vertices")
                    While j1 < count_j1
                        CkJsonObject::setCkJ(json1, j1)
                        x = CkJsonObject::ckIntOf(json1,"paragraphs[i].boundingBox.vertices[j].x")
                        y = CkJsonObject::ckIntOf(json1,"paragraphs[i].boundingBox.vertices[j].y")
                        j1 = j1 + 1
                    Wend
                    j1 = 0
                    count_j1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].words")
                    While j1 < count_j1
                        CkJsonObject::setCkJ(json1, j1)
                        k1 = 0
                        count_k1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].words[j].property.detectedLanguages")
                        While k1 < count_k1
                            CkJsonObject::setCkK(json1, k1)
                            languageCode = CkJsonObject::ckStringOf(json1,"paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
                            k1 = k1 + 1
                        Wend
                        k1 = 0
                        count_k1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].words[j].boundingBox.vertices")
                        While k1 < count_k1
                            CkJsonObject::setCkK(json1, k1)
                            x = CkJsonObject::ckIntOf(json1,"paragraphs[i].words[j].boundingBox.vertices[k].x")
                            y = CkJsonObject::ckIntOf(json1,"paragraphs[i].words[j].boundingBox.vertices[k].y")
                            k1 = k1 + 1
                        Wend
                        k1 = 0
                        count_k1 = CkJsonObject::ckSizeOfArray(json1,"paragraphs[i].words[j].symbols")
                        While k1 < count_k1
                            CkJsonObject::setCkK(json1, k1)
                            text = CkJsonObject::ckStringOf(json1,"paragraphs[i].words[j].symbols[k].text")
                            propertyDetectedBreakType = CkJsonObject::ckStringOf(json1,"paragraphs[i].words[j].symbols[k].property.detectedBreak.type")

                            json2 = CkJsonObject::ckObjectOf(json1,"paragraphs[i].words[j].symbols[k]")
                            i2 = 0
                            count_i2 = CkJsonObject::ckSizeOfArray(json2,"property.detectedLanguages")
                            While i2 < count_i2
                                CkJsonObject::setCkI(json2, i2)
                                languageCode = CkJsonObject::ckStringOf(json2,"property.detectedLanguages[i].languageCode")
                                i2 = i2 + 1
                            Wend
                            CkJsonObject::ckDispose(json2)

                            json2 = CkJsonObject::ckObjectOf(json1,"paragraphs[i].words[j].symbols[k]")
                            i2 = 0
                            count_i2 = CkJsonObject::ckSizeOfArray(json2,"boundingBox.vertices")
                            While i2 < count_i2
                                CkJsonObject::setCkI(json2, i2)
                                x = CkJsonObject::ckIntOf(json2,"boundingBox.vertices[i].x")
                                y = CkJsonObject::ckIntOf(json2,"boundingBox.vertices[i].y")
                                i2 = i2 + 1
                            Wend
                            CkJsonObject::ckDispose(json2)

                            k1 = k1 + 1
                        Wend
                        j1 = j1 + 1
                    Wend
                    i1 = i1 + 1
                Wend
                CkJsonObject::ckDispose(json1)

                k = k + 1
            Wend
            j = j + 1
        Wend
        i = i + 1
    Wend

    CkHttpResponse::ckDispose(resp)

    Debug "Success."


    CkBinData::ckDispose(imageData)
    CkJsonObject::ckDispose(json)
    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sb)
    CkStringBuilder::ckDispose(sbResponseBody)


    ProcedureReturn
EndProcedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.