Sample code for 30+ languages & platforms
Visual FoxPro

Google Cloud Vision Text Detection

See more HTTP Misc Examples

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImageData
LOCAL loJson
LOCAL loHttp
LOCAL loSb
LOCAL lcUrl
LOCAL loResp
LOCAL loSbResponseBody
LOCAL i
LOCAL lnCount_i
LOCAL lcFullTextAnnotationText
LOCAL j
LOCAL lnCount_j
LOCAL lcLocale
LOCAL lcDescription
LOCAL k
LOCAL lnCount_k
LOCAL x
LOCAL y
LOCAL lnWidth
LOCAL lnHeight
LOCAL lcLanguageCode
LOCAL lcBlockType
LOCAL lnI1
LOCAL lnCount_i1
LOCAL lnJ1
LOCAL lnCount_j1
LOCAL lnK1
LOCAL lnCount_k1
LOCAL lcText
LOCAL lcPropertyDetectedBreakType
LOCAL lnI2
LOCAL lnCount_i2
LOCAL loJson1
LOCAL loJson2

lnSuccess = 0

* 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.
loImageData = CreateObject('Chilkat.BinData')
* This image file contains some text...
lnSuccess = loImageData.LoadFile("qa_data/jpg/text.jpg")
IF (lnSuccess <> 1) THEN
    ? "Failed to load image file."
    RELEASE loImageData
    CANCEL
ENDIF

* Create the above JSON.
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateBd("requests[0].image.content","base64",loImageData)
loJson.UpdateString("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

loHttp = CreateObject('Chilkat.Http')
loHttp.SessionLogFilename = "c:/aaworkarea/sessionLog.txt"

loSb = CreateObject('Chilkat.StringBuilder')
lcUrl = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpJson("POST",lcUrl,loJson,"application/json",loResp)
IF (lnSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loImageData
    RELEASE loJson
    RELEASE loHttp
    RELEASE loSb
    RELEASE loResp
    CANCEL
ENDIF

? "status = " + STR(loResp.StatusCode)

* A 200 response status indicate success.
IF (loResp.StatusCode <> 200) THEN
    ? loResp.BodyStr
    ? "Failed."
    RELEASE loImageData
    RELEASE loJson
    RELEASE loHttp
    RELEASE loSb
    RELEASE loResp
    CANCEL
ENDIF

loSbResponseBody = CreateObject('Chilkat.StringBuilder')
loResp.GetBodySb(loSbResponseBody)
loSbResponseBody.WriteFile("qa_output/textDetectResponse.json","utf-8",0)
loJson.LoadSb(loSbResponseBody)

* 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:

loJson1 = CreateObject('Chilkat.JsonObject')
loJson2 = CreateObject('Chilkat.JsonObject')

i = 0
lnCount_i = loJson.SizeOfArray("responses")
DO WHILE i < lnCount_i
    loJson.I = i
    lcFullTextAnnotationText = loJson.StringOf("responses[i].fullTextAnnotation.text")
    j = 0
    lnCount_j = loJson.SizeOfArray("responses[i].textAnnotations")
    DO WHILE j < lnCount_j
        loJson.J = j
        lcLocale = loJson.StringOf("responses[i].textAnnotations[j].locale")
        lcDescription = loJson.StringOf("responses[i].textAnnotations[j].description")
        k = 0
        lnCount_k = loJson.SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices")
        DO WHILE k < lnCount_k
            loJson.K = k
            x = loJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
            y = loJson.IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
            k = k + 1
        ENDDO
        j = j + 1
    ENDDO
    j = 0
    lnCount_j = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages")
    DO WHILE j < lnCount_j
        loJson.J = j
        lnWidth = loJson.IntOf("responses[i].fullTextAnnotation.pages[j].width")
        lnHeight = loJson.IntOf("responses[i].fullTextAnnotation.pages[j].height")
        k = 0
        lnCount_k = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
        DO WHILE k < lnCount_k
            loJson.K = k
            lcLanguageCode = loJson.StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
            k = k + 1
        ENDDO
        k = 0
        lnCount_k = loJson.SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks")
        DO WHILE k < lnCount_k
            loJson.K = k
            lcBlockType = loJson.StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
            loJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loJson1)

            lnI1 = 0
            lnCount_i1 = loJson1.SizeOfArray("property.detectedLanguages")
            DO WHILE lnI1 < lnCount_i1
                loJson1.I = lnI1
                lcLanguageCode = loJson1.StringOf("property.detectedLanguages[i].languageCode")
                lnI1 = lnI1 + 1
            ENDDO
            loJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loJson1)

            lnI1 = 0
            lnCount_i1 = loJson1.SizeOfArray("boundingBox.vertices")
            DO WHILE lnI1 < lnCount_i1
                loJson1.I = lnI1
                x = loJson1.IntOf("boundingBox.vertices[i].x")
                y = loJson1.IntOf("boundingBox.vertices[i].y")
                lnI1 = lnI1 + 1
            ENDDO
            loJson.ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]",loJson1)

            lnI1 = 0
            lnCount_i1 = loJson1.SizeOfArray("paragraphs")
            DO WHILE lnI1 < lnCount_i1
                loJson1.I = lnI1
                lnJ1 = 0
                lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].property.detectedLanguages")
                DO WHILE lnJ1 < lnCount_j1
                    loJson1.J = lnJ1
                    lcLanguageCode = loJson1.StringOf("paragraphs[i].property.detectedLanguages[j].languageCode")
                    lnJ1 = lnJ1 + 1
                ENDDO
                lnJ1 = 0
                lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].boundingBox.vertices")
                DO WHILE lnJ1 < lnCount_j1
                    loJson1.J = lnJ1
                    x = loJson1.IntOf("paragraphs[i].boundingBox.vertices[j].x")
                    y = loJson1.IntOf("paragraphs[i].boundingBox.vertices[j].y")
                    lnJ1 = lnJ1 + 1
                ENDDO
                lnJ1 = 0
                lnCount_j1 = loJson1.SizeOfArray("paragraphs[i].words")
                DO WHILE lnJ1 < lnCount_j1
                    loJson1.J = lnJ1
                    lnK1 = 0
                    lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].property.detectedLanguages")
                    DO WHILE lnK1 < lnCount_k1
                        loJson1.K = lnK1
                        lcLanguageCode = loJson1.StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
                        lnK1 = lnK1 + 1
                    ENDDO
                    lnK1 = 0
                    lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].boundingBox.vertices")
                    DO WHILE lnK1 < lnCount_k1
                        loJson1.K = lnK1
                        x = loJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x")
                        y = loJson1.IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y")
                        lnK1 = lnK1 + 1
                    ENDDO
                    lnK1 = 0
                    lnCount_k1 = loJson1.SizeOfArray("paragraphs[i].words[j].symbols")
                    DO WHILE lnK1 < lnCount_k1
                        loJson1.K = lnK1
                        lcText = loJson1.StringOf("paragraphs[i].words[j].symbols[k].text")
                        lcPropertyDetectedBreakType = loJson1.StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
                        loJson1.ObjectOf2("paragraphs[i].words[j].symbols[k]",loJson2)

                        lnI2 = 0
                        lnCount_i2 = loJson2.SizeOfArray("property.detectedLanguages")
                        DO WHILE lnI2 < lnCount_i2
                            loJson2.I = lnI2
                            lcLanguageCode = loJson2.StringOf("property.detectedLanguages[i].languageCode")
                            lnI2 = lnI2 + 1
                        ENDDO
                        loJson1.ObjectOf2("paragraphs[i].words[j].symbols[k]",loJson2)

                        lnI2 = 0
                        lnCount_i2 = loJson2.SizeOfArray("boundingBox.vertices")
                        DO WHILE lnI2 < lnCount_i2
                            loJson2.I = lnI2
                            x = loJson2.IntOf("boundingBox.vertices[i].x")
                            y = loJson2.IntOf("boundingBox.vertices[i].y")
                            lnI2 = lnI2 + 1
                        ENDDO
                        lnK1 = lnK1 + 1
                    ENDDO
                    lnJ1 = lnJ1 + 1
                ENDDO
                lnI1 = lnI1 + 1
            ENDDO
            k = k + 1
        ENDDO
        j = j + 1
    ENDDO
    i = i + 1
ENDDO

? "Success."

RELEASE loImageData
RELEASE loJson
RELEASE loHttp
RELEASE loSb
RELEASE loResp
RELEASE loSbResponseBody
RELEASE loJson1
RELEASE loJson2