|  | 
Chilkat  HOME  Android™  AutoIt  C  C#  C++  Chilkat2-Python  CkPython  Classic ASP  DataFlex  Delphi DLL  Go  Java  Node.js  Objective-C  PHP Extension  Perl  PowerBuilder  PowerShell  PureBasic  Ruby  SQL Server  Swift  Tcl  Unicode C  Unicode C++  VB.NET  VBScript  Visual Basic 6.0  Visual FoxPro  Xojo Plugin
| (Unicode C) Google Cloud Vision Text DetectionDemonstrates 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 Note: This example requires Chilkat v11.0.0 or greater. 
 #include <C_CkBinDataW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpW.h> #include <C_CkStringBuilderW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { BOOL success; HCkBinDataW imageData; HCkJsonObjectW json; HCkHttpW http; HCkStringBuilderW sb; const wchar_t *url; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; int i; int count_i; const wchar_t *fullTextAnnotationText; int j; int count_j; const wchar_t *locale; const wchar_t *description; int k; int count_k; int x; int y; int width; int height; const wchar_t *languageCode; const wchar_t *blockType; int i1; int count_i1; int j1; int count_j1; int k1; int count_k1; const wchar_t *text; const wchar_t *propertyDetectedBreakType; int i2; int count_i2; HCkJsonObjectW json1; HCkJsonObjectW json2; success = FALSE; // 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 = CkBinDataW_Create(); // This image file contains some text... success = CkBinDataW_LoadFile(imageData,L"qa_data/jpg/text.jpg"); if (success != TRUE) { wprintf(L"Failed to load image file.\n"); CkBinDataW_Dispose(imageData); return; } // Create the above JSON. json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateBd(json,L"requests[0].image.content",L"base64",imageData); CkJsonObjectW_UpdateString(json,L"requests[0].features[0].type",L"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 = CkHttpW_Create(); CkHttpW_putSessionLogFilename(http,L"c:/aaworkarea/sessionLog.txt"); sb = CkStringBuilderW_Create(); url = L"https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"; resp = CkHttpResponseW_Create(); success = CkHttpW_HttpJson(http,L"POST",url,json,L"application/json",resp); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkBinDataW_Dispose(imageData); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sb); CkHttpResponseW_Dispose(resp); return; } wprintf(L"status = %d\n",CkHttpResponseW_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); wprintf(L"Failed.\n"); CkBinDataW_Dispose(imageData); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sb); CkHttpResponseW_Dispose(resp); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); CkStringBuilderW_WriteFile(sbResponseBody,L"qa_output/textDetectResponse.json",L"utf-8",FALSE); CkJsonObjectW_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 = CkJsonObjectW_Create(); json2 = CkJsonObjectW_Create(); i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"responses"); while (i < count_i) { CkJsonObjectW_putI(json,i); fullTextAnnotationText = CkJsonObjectW_stringOf(json,L"responses[i].fullTextAnnotation.text"); j = 0; count_j = CkJsonObjectW_SizeOfArray(json,L"responses[i].textAnnotations"); while (j < count_j) { CkJsonObjectW_putJ(json,j); locale = CkJsonObjectW_stringOf(json,L"responses[i].textAnnotations[j].locale"); description = CkJsonObjectW_stringOf(json,L"responses[i].textAnnotations[j].description"); k = 0; count_k = CkJsonObjectW_SizeOfArray(json,L"responses[i].textAnnotations[j].boundingPoly.vertices"); while (k < count_k) { CkJsonObjectW_putK(json,k); x = CkJsonObjectW_IntOf(json,L"responses[i].textAnnotations[j].boundingPoly.vertices[k].x"); y = CkJsonObjectW_IntOf(json,L"responses[i].textAnnotations[j].boundingPoly.vertices[k].y"); k = k + 1; } j = j + 1; } j = 0; count_j = CkJsonObjectW_SizeOfArray(json,L"responses[i].fullTextAnnotation.pages"); while (j < count_j) { CkJsonObjectW_putJ(json,j); width = CkJsonObjectW_IntOf(json,L"responses[i].fullTextAnnotation.pages[j].width"); height = CkJsonObjectW_IntOf(json,L"responses[i].fullTextAnnotation.pages[j].height"); k = 0; count_k = CkJsonObjectW_SizeOfArray(json,L"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"); while (k < count_k) { CkJsonObjectW_putK(json,k); languageCode = CkJsonObjectW_stringOf(json,L"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"); k = k + 1; } k = 0; count_k = CkJsonObjectW_SizeOfArray(json,L"responses[i].fullTextAnnotation.pages[j].blocks"); while (k < count_k) { CkJsonObjectW_putK(json,k); blockType = CkJsonObjectW_stringOf(json,L"responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"); CkJsonObjectW_ObjectOf2(json,L"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObjectW_SizeOfArray(json1,L"property.detectedLanguages"); while (i1 < count_i1) { CkJsonObjectW_putI(json1,i1); languageCode = CkJsonObjectW_stringOf(json1,L"property.detectedLanguages[i].languageCode"); i1 = i1 + 1; } CkJsonObjectW_ObjectOf2(json,L"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObjectW_SizeOfArray(json1,L"boundingBox.vertices"); while (i1 < count_i1) { CkJsonObjectW_putI(json1,i1); x = CkJsonObjectW_IntOf(json1,L"boundingBox.vertices[i].x"); y = CkJsonObjectW_IntOf(json1,L"boundingBox.vertices[i].y"); i1 = i1 + 1; } CkJsonObjectW_ObjectOf2(json,L"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs"); while (i1 < count_i1) { CkJsonObjectW_putI(json1,i1); j1 = 0; count_j1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].property.detectedLanguages"); while (j1 < count_j1) { CkJsonObjectW_putJ(json1,j1); languageCode = CkJsonObjectW_stringOf(json1,L"paragraphs[i].property.detectedLanguages[j].languageCode"); j1 = j1 + 1; } j1 = 0; count_j1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].boundingBox.vertices"); while (j1 < count_j1) { CkJsonObjectW_putJ(json1,j1); x = CkJsonObjectW_IntOf(json1,L"paragraphs[i].boundingBox.vertices[j].x"); y = CkJsonObjectW_IntOf(json1,L"paragraphs[i].boundingBox.vertices[j].y"); j1 = j1 + 1; } j1 = 0; count_j1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].words"); while (j1 < count_j1) { CkJsonObjectW_putJ(json1,j1); k1 = 0; count_k1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].words[j].property.detectedLanguages"); while (k1 < count_k1) { CkJsonObjectW_putK(json1,k1); languageCode = CkJsonObjectW_stringOf(json1,L"paragraphs[i].words[j].property.detectedLanguages[k].languageCode"); k1 = k1 + 1; } k1 = 0; count_k1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].words[j].boundingBox.vertices"); while (k1 < count_k1) { CkJsonObjectW_putK(json1,k1); x = CkJsonObjectW_IntOf(json1,L"paragraphs[i].words[j].boundingBox.vertices[k].x"); y = CkJsonObjectW_IntOf(json1,L"paragraphs[i].words[j].boundingBox.vertices[k].y"); k1 = k1 + 1; } k1 = 0; count_k1 = CkJsonObjectW_SizeOfArray(json1,L"paragraphs[i].words[j].symbols"); while (k1 < count_k1) { CkJsonObjectW_putK(json1,k1); text = CkJsonObjectW_stringOf(json1,L"paragraphs[i].words[j].symbols[k].text"); propertyDetectedBreakType = CkJsonObjectW_stringOf(json1,L"paragraphs[i].words[j].symbols[k].property.detectedBreak.type"); CkJsonObjectW_ObjectOf2(json1,L"paragraphs[i].words[j].symbols[k]",json2); i2 = 0; count_i2 = CkJsonObjectW_SizeOfArray(json2,L"property.detectedLanguages"); while (i2 < count_i2) { CkJsonObjectW_putI(json2,i2); languageCode = CkJsonObjectW_stringOf(json2,L"property.detectedLanguages[i].languageCode"); i2 = i2 + 1; } CkJsonObjectW_ObjectOf2(json1,L"paragraphs[i].words[j].symbols[k]",json2); i2 = 0; count_i2 = CkJsonObjectW_SizeOfArray(json2,L"boundingBox.vertices"); while (i2 < count_i2) { CkJsonObjectW_putI(json2,i2); x = CkJsonObjectW_IntOf(json2,L"boundingBox.vertices[i].x"); y = CkJsonObjectW_IntOf(json2,L"boundingBox.vertices[i].y"); i2 = i2 + 1; } k1 = k1 + 1; } j1 = j1 + 1; } i1 = i1 + 1; } k = k + 1; } j = j + 1; } i = i + 1; } wprintf(L"Success.\n"); CkBinDataW_Dispose(imageData); CkJsonObjectW_Dispose(json); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sb); CkHttpResponseW_Dispose(resp); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(json1); CkJsonObjectW_Dispose(json2); } | ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.