Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Tcl Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Tcl) 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 Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# 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.
set imageData [new_CkBinData]

# This image file contains some text...
set success [CkBinData_LoadFile $imageData "qa_data/jpg/text.jpg"]
if {$success != 1} then {
    puts "Failed to load image file."
    delete_CkBinData $imageData
    exit
}

# Create the above JSON.
set json [new_CkJsonObject]

CkJsonObject_UpdateBd $json "requests[0].image.content" "base64" $imageData
CkJsonObject_UpdateString $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

set http [new_CkHttp]

set sb [new_CkStringBuilder]

set url "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
# resp is a CkHttpResponse
set resp [CkHttp_PostJson3 $http $url "application/json" $json]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkBinData $imageData
    delete_CkJsonObject $json
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    exit
}

puts "status = [CkHttpResponse_get_StatusCode $resp]"

# A 200 response status indicate success.
if {[CkHttpResponse_get_StatusCode $resp] != 200} then {
    puts [CkHttpResponse_bodyStr $resp]
    puts "Failed."
    delete_CkHttpResponse $resp

    delete_CkBinData $imageData
    delete_CkJsonObject $json
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody
CkStringBuilder_WriteFile $sbResponseBody "qa_output/textDetectResponse.json" "utf-8" 0
CkJsonObject_LoadSb $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:

# json1 is a CkJsonObject

# json2 is a CkJsonObject

set i 0
set count_i [CkJsonObject_SizeOfArray $json "responses"]
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set fullTextAnnotationText [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.text"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set locale [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].locale"]
        set description [CkJsonObject_stringOf $json "responses[i].textAnnotations[j].description"]
        set k 0
        set count_k [CkJsonObject_SizeOfArray $json "responses[i].textAnnotations[j].boundingPoly.vertices"]
        while {$k < $count_k} {
            CkJsonObject_put_K $json $k
            set x [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].x"]
            set y [CkJsonObject_IntOf $json "responses[i].textAnnotations[j].boundingPoly.vertices[k].y"]
            set k [expr $k + 1]
        }
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set width [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].width"]
        set height [CkJsonObject_IntOf $json "responses[i].fullTextAnnotation.pages[j].height"]
        set k 0
        set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"]
        while {$k < $count_k} {
            CkJsonObject_put_K $json $k
            set languageCode [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"]
            set k [expr $k + 1]
        }
        set k 0
        set count_k [CkJsonObject_SizeOfArray $json "responses[i].fullTextAnnotation.pages[j].blocks"]
        while {$k < $count_k} {
            CkJsonObject_put_K $json $k
            set blockType [CkJsonObject_stringOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"]

            set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"]
            set i1 0
            set count_i1 [CkJsonObject_SizeOfArray $json1 "property.detectedLanguages"]
            while {$i1 < $count_i1} {
                CkJsonObject_put_I $json1 $i1
                set languageCode [CkJsonObject_stringOf $json1 "property.detectedLanguages[i].languageCode"]
                set i1 [expr $i1 + 1]
            }
            delete_CkJsonObject $json1

            set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"]
            set i1 0
            set count_i1 [CkJsonObject_SizeOfArray $json1 "boundingBox.vertices"]
            while {$i1 < $count_i1} {
                CkJsonObject_put_I $json1 $i1
                set x [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].x"]
                set y [CkJsonObject_IntOf $json1 "boundingBox.vertices[i].y"]
                set i1 [expr $i1 + 1]
            }
            delete_CkJsonObject $json1

            set json1 [CkJsonObject_ObjectOf $json "responses[i].fullTextAnnotation.pages[j].blocks[k]"]
            set i1 0
            set count_i1 [CkJsonObject_SizeOfArray $json1 "paragraphs"]
            while {$i1 < $count_i1} {
                CkJsonObject_put_I $json1 $i1
                set j1 0
                set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].property.detectedLanguages"]
                while {$j1 < $count_j1} {
                    CkJsonObject_put_J $json1 $j1
                    set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].property.detectedLanguages[j].languageCode"]
                    set j1 [expr $j1 + 1]
                }
                set j1 0
                set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].boundingBox.vertices"]
                while {$j1 < $count_j1} {
                    CkJsonObject_put_J $json1 $j1
                    set x [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].x"]
                    set y [CkJsonObject_IntOf $json1 "paragraphs[i].boundingBox.vertices[j].y"]
                    set j1 [expr $j1 + 1]
                }
                set j1 0
                set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words"]
                while {$j1 < $count_j1} {
                    CkJsonObject_put_J $json1 $j1
                    set k1 0
                    set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].property.detectedLanguages"]
                    while {$k1 < $count_k1} {
                        CkJsonObject_put_K $json1 $k1
                        set languageCode [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].property.detectedLanguages[k].languageCode"]
                        set k1 [expr $k1 + 1]
                    }
                    set k1 0
                    set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].boundingBox.vertices"]
                    while {$k1 < $count_k1} {
                        CkJsonObject_put_K $json1 $k1
                        set x [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].x"]
                        set y [CkJsonObject_IntOf $json1 "paragraphs[i].words[j].boundingBox.vertices[k].y"]
                        set k1 [expr $k1 + 1]
                    }
                    set k1 0
                    set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].symbols"]
                    while {$k1 < $count_k1} {
                        CkJsonObject_put_K $json1 $k1
                        set text [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].text"]
                        set propertyDetectedBreakType [CkJsonObject_stringOf $json1 "paragraphs[i].words[j].symbols[k].property.detectedBreak.type"]

                        set json2 [CkJsonObject_ObjectOf $json1 "paragraphs[i].words[j].symbols[k]"]
                        set i2 0
                        set count_i2 [CkJsonObject_SizeOfArray $json2 "property.detectedLanguages"]
                        while {$i2 < $count_i2} {
                            CkJsonObject_put_I $json2 $i2
                            set languageCode [CkJsonObject_stringOf $json2 "property.detectedLanguages[i].languageCode"]
                            set i2 [expr $i2 + 1]
                        }
                        delete_CkJsonObject $json2

                        set json2 [CkJsonObject_ObjectOf $json1 "paragraphs[i].words[j].symbols[k]"]
                        set i2 0
                        set count_i2 [CkJsonObject_SizeOfArray $json2 "boundingBox.vertices"]
                        while {$i2 < $count_i2} {
                            CkJsonObject_put_I $json2 $i2
                            set x [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].x"]
                            set y [CkJsonObject_IntOf $json2 "boundingBox.vertices[i].y"]
                            set i2 [expr $i2 + 1]
                        }
                        delete_CkJsonObject $json2

                        set k1 [expr $k1 + 1]
                    }
                    set j1 [expr $j1 + 1]
                }
                set i1 [expr $i1 + 1]
            }
            delete_CkJsonObject $json1

            set k [expr $k + 1]
        }
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

delete_CkHttpResponse $resp

puts "Success."

delete_CkBinData $imageData
delete_CkJsonObject $json
delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkStringBuilder $sbResponseBody

 

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