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

Tcl Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
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)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(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 the 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 {[expr $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 {[expr [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 {[expr [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 the 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 {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $i1 < $count_i1]} {
                CkJsonObject_put_I $json1 $i1
                set j1 0
                set count_j1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].property.detectedLanguages"]
                while {[expr $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 {[expr $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 {[expr $j1 < $count_j1]} {
                    CkJsonObject_put_J $json1 $j1
                    set k1 0
                    set count_k1 [CkJsonObject_SizeOfArray $json1 "paragraphs[i].words[j].property.detectedLanguages"]
                    while {[expr $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 {[expr $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 {[expr $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 {[expr $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 {[expr $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-2016 Chilkat Software, Inc. All Rights Reserved.