Xbase++ Requires Chilkat v11.0.0+
Xbase++
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-textChilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImageData
LOCAL oJson
LOCAL oHttp
LOCAL oSb
LOCAL cUrl
LOCAL oResp
LOCAL oSbResponseBody
LOCAL i
LOCAL nCount_i
LOCAL cFullTextAnnotationText
LOCAL j
LOCAL nCount_j
LOCAL cLocale
LOCAL cDescription
LOCAL k
LOCAL nCount_k
LOCAL x
LOCAL y
LOCAL nWidth
LOCAL nHeight
LOCAL cLanguageCode
LOCAL cBlockType
LOCAL nI1
LOCAL nCount_i1
LOCAL nJ1
LOCAL nCount_j1
LOCAL nK1
LOCAL nCount_k1
LOCAL cText
LOCAL cPropertyDetectedBreakType
LOCAL nI2
LOCAL nCount_i2
LOCAL oJson1
LOCAL oJson2
nSuccess := 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.
oImageData := CreateObject("Chilkat.BinData")
// This image file contains some text...
nSuccess := oImageData:LoadFile("qa_data/jpg/text.jpg")
IF (nSuccess != 1)
? "Failed to load image file."
oImageData:destroy()
RETURN
ENDIF
// Create the above JSON.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateBd("requests[0].image.content", "base64", oImageData)
oJson: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
oHttp := CreateObject("Chilkat.Http")
oHttp:SessionLogFilename := "c:/aaworkarea/sessionLog.txt"
oSb := CreateObject("Chilkat.StringBuilder")
cUrl := "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", cUrl, oJson, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oImageData:destroy()
oJson:destroy()
oHttp:destroy()
oSb:destroy()
oResp:destroy()
RETURN
ENDIF
? "status = " + Str(oResp:StatusCode)
// A 200 response status indicate success.
IF (oResp:StatusCode != 200)
? oResp:BodyStr
? "Failed."
oImageData:destroy()
oJson:destroy()
oHttp:destroy()
oSb:destroy()
oResp:destroy()
RETURN
ENDIF
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oSbResponseBody:WriteFile("qa_output/textDetectResponse.json", "utf-8", 0)
oJson:LoadSb(oSbResponseBody)
// 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:
oJson1 := CreateObject("Chilkat.JsonObject")
oJson2 := CreateObject("Chilkat.JsonObject")
i := 0
nCount_i := oJson:SizeOfArray("responses")
DO WHILE i < nCount_i
oJson:I := i
cFullTextAnnotationText := oJson:StringOf("responses[i].fullTextAnnotation.text")
j := 0
nCount_j := oJson:SizeOfArray("responses[i].textAnnotations")
DO WHILE j < nCount_j
oJson:J := j
cLocale := oJson:StringOf("responses[i].textAnnotations[j].locale")
cDescription := oJson:StringOf("responses[i].textAnnotations[j].description")
k := 0
nCount_k := oJson:SizeOfArray("responses[i].textAnnotations[j].boundingPoly.vertices")
DO WHILE k < nCount_k
oJson:K := k
x := oJson:IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].x")
y := oJson:IntOf("responses[i].textAnnotations[j].boundingPoly.vertices[k].y")
k := k + 1
ENDDO
j := j + 1
ENDDO
j := 0
nCount_j := oJson:SizeOfArray("responses[i].fullTextAnnotation.pages")
DO WHILE j < nCount_j
oJson:J := j
nWidth := oJson:IntOf("responses[i].fullTextAnnotation.pages[j].width")
nHeight := oJson:IntOf("responses[i].fullTextAnnotation.pages[j].height")
k := 0
nCount_k := oJson:SizeOfArray("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages")
DO WHILE k < nCount_k
oJson:K := k
cLanguageCode := oJson:StringOf("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
k := k + 1
ENDDO
k := 0
nCount_k := oJson:SizeOfArray("responses[i].fullTextAnnotation.pages[j].blocks")
DO WHILE k < nCount_k
oJson:K := k
cBlockType := oJson:StringOf("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
oJson:ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]", oJson1)
nI1 := 0
nCount_i1 := oJson1:SizeOfArray("property.detectedLanguages")
DO WHILE nI1 < nCount_i1
oJson1:I := nI1
cLanguageCode := oJson1:StringOf("property.detectedLanguages[i].languageCode")
nI1 := nI1 + 1
ENDDO
oJson:ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]", oJson1)
nI1 := 0
nCount_i1 := oJson1:SizeOfArray("boundingBox.vertices")
DO WHILE nI1 < nCount_i1
oJson1:I := nI1
x := oJson1:IntOf("boundingBox.vertices[i].x")
y := oJson1:IntOf("boundingBox.vertices[i].y")
nI1 := nI1 + 1
ENDDO
oJson:ObjectOf2("responses[i].fullTextAnnotation.pages[j].blocks[k]", oJson1)
nI1 := 0
nCount_i1 := oJson1:SizeOfArray("paragraphs")
DO WHILE nI1 < nCount_i1
oJson1:I := nI1
nJ1 := 0
nCount_j1 := oJson1:SizeOfArray("paragraphs[i].property.detectedLanguages")
DO WHILE nJ1 < nCount_j1
oJson1:J := nJ1
cLanguageCode := oJson1:StringOf("paragraphs[i].property.detectedLanguages[j].languageCode")
nJ1 := nJ1 + 1
ENDDO
nJ1 := 0
nCount_j1 := oJson1:SizeOfArray("paragraphs[i].boundingBox.vertices")
DO WHILE nJ1 < nCount_j1
oJson1:J := nJ1
x := oJson1:IntOf("paragraphs[i].boundingBox.vertices[j].x")
y := oJson1:IntOf("paragraphs[i].boundingBox.vertices[j].y")
nJ1 := nJ1 + 1
ENDDO
nJ1 := 0
nCount_j1 := oJson1:SizeOfArray("paragraphs[i].words")
DO WHILE nJ1 < nCount_j1
oJson1:J := nJ1
nK1 := 0
nCount_k1 := oJson1:SizeOfArray("paragraphs[i].words[j].property.detectedLanguages")
DO WHILE nK1 < nCount_k1
oJson1:K := nK1
cLanguageCode := oJson1:StringOf("paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
nK1 := nK1 + 1
ENDDO
nK1 := 0
nCount_k1 := oJson1:SizeOfArray("paragraphs[i].words[j].boundingBox.vertices")
DO WHILE nK1 < nCount_k1
oJson1:K := nK1
x := oJson1:IntOf("paragraphs[i].words[j].boundingBox.vertices[k].x")
y := oJson1:IntOf("paragraphs[i].words[j].boundingBox.vertices[k].y")
nK1 := nK1 + 1
ENDDO
nK1 := 0
nCount_k1 := oJson1:SizeOfArray("paragraphs[i].words[j].symbols")
DO WHILE nK1 < nCount_k1
oJson1:K := nK1
cText := oJson1:StringOf("paragraphs[i].words[j].symbols[k].text")
cPropertyDetectedBreakType := oJson1:StringOf("paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
oJson1:ObjectOf2("paragraphs[i].words[j].symbols[k]", oJson2)
nI2 := 0
nCount_i2 := oJson2:SizeOfArray("property.detectedLanguages")
DO WHILE nI2 < nCount_i2
oJson2:I := nI2
cLanguageCode := oJson2:StringOf("property.detectedLanguages[i].languageCode")
nI2 := nI2 + 1
ENDDO
oJson1:ObjectOf2("paragraphs[i].words[j].symbols[k]", oJson2)
nI2 := 0
nCount_i2 := oJson2:SizeOfArray("boundingBox.vertices")
DO WHILE nI2 < nCount_i2
oJson2:I := nI2
x := oJson2:IntOf("boundingBox.vertices[i].x")
y := oJson2:IntOf("boundingBox.vertices[i].y")
nI2 := nI2 + 1
ENDDO
nK1 := nK1 + 1
ENDDO
nJ1 := nJ1 + 1
ENDDO
nI1 := nI1 + 1
ENDDO
k := k + 1
ENDDO
j := j + 1
ENDDO
i := i + 1
ENDDO
? "Success."
oImageData:destroy()
oJson:destroy()
oHttp:destroy()
oSb:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJson1:destroy()
oJson2:destroy()