|  | 
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
| (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_CkBinData.h> #include <C_CkJsonObject.h> #include <C_CkHttp.h> #include <C_CkStringBuilder.h> #include <C_CkHttpResponse.h> void ChilkatSample(void) { BOOL success; HCkBinData imageData; HCkJsonObject json; HCkHttp http; HCkStringBuilder sb; const char *url; HCkHttpResponse resp; HCkStringBuilder sbResponseBody; int i; int count_i; const char *fullTextAnnotationText; int j; int count_j; const char *locale; const char *description; int k; int count_k; int x; int y; int width; int height; const char *languageCode; const char *blockType; int i1; int count_i1; int j1; int count_j1; int k1; int count_k1; const char *text; const char *propertyDetectedBreakType; int i2; int count_i2; HCkJsonObject json1; HCkJsonObject 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 = CkBinData_Create(); // This image file contains some text... success = CkBinData_LoadFile(imageData,"qa_data/jpg/text.jpg"); if (success != TRUE) { printf("Failed to load image file.\n"); CkBinData_Dispose(imageData); return; } // Create the above JSON. json = CkJsonObject_Create(); 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 http = CkHttp_Create(); CkHttp_putSessionLogFilename(http,"c:/aaworkarea/sessionLog.txt"); sb = CkStringBuilder_Create(); url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"; resp = CkHttpResponse_Create(); success = CkHttp_HttpJson(http,"POST",url,json,"application/json",resp); if (success == FALSE) { printf("%s\n",CkHttp_lastErrorText(http)); CkBinData_Dispose(imageData); CkJsonObject_Dispose(json); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkHttpResponse_Dispose(resp); return; } printf("status = %d\n",CkHttpResponse_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) != 200) { printf("%s\n",CkHttpResponse_bodyStr(resp)); printf("Failed.\n"); CkBinData_Dispose(imageData); CkJsonObject_Dispose(json); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkHttpResponse_Dispose(resp); return; } sbResponseBody = CkStringBuilder_Create(); CkHttpResponse_GetBodySb(resp,sbResponseBody); CkStringBuilder_WriteFile(sbResponseBody,"qa_output/textDetectResponse.json","utf-8",FALSE); 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 = CkJsonObject_Create(); json2 = CkJsonObject_Create(); i = 0; count_i = CkJsonObject_SizeOfArray(json,"responses"); while (i < count_i) { CkJsonObject_putI(json,i); fullTextAnnotationText = CkJsonObject_stringOf(json,"responses[i].fullTextAnnotation.text"); j = 0; count_j = CkJsonObject_SizeOfArray(json,"responses[i].textAnnotations"); while (j < count_j) { CkJsonObject_putJ(json,j); locale = CkJsonObject_stringOf(json,"responses[i].textAnnotations[j].locale"); description = CkJsonObject_stringOf(json,"responses[i].textAnnotations[j].description"); k = 0; count_k = CkJsonObject_SizeOfArray(json,"responses[i].textAnnotations[j].boundingPoly.vertices"); while (k < count_k) { CkJsonObject_putK(json,k); x = CkJsonObject_IntOf(json,"responses[i].textAnnotations[j].boundingPoly.vertices[k].x"); y = CkJsonObject_IntOf(json,"responses[i].textAnnotations[j].boundingPoly.vertices[k].y"); k = k + 1; } j = j + 1; } j = 0; count_j = CkJsonObject_SizeOfArray(json,"responses[i].fullTextAnnotation.pages"); while (j < count_j) { CkJsonObject_putJ(json,j); width = CkJsonObject_IntOf(json,"responses[i].fullTextAnnotation.pages[j].width"); height = CkJsonObject_IntOf(json,"responses[i].fullTextAnnotation.pages[j].height"); k = 0; count_k = CkJsonObject_SizeOfArray(json,"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"); while (k < count_k) { CkJsonObject_putK(json,k); languageCode = CkJsonObject_stringOf(json,"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"); k = k + 1; } k = 0; count_k = CkJsonObject_SizeOfArray(json,"responses[i].fullTextAnnotation.pages[j].blocks"); while (k < count_k) { CkJsonObject_putK(json,k); blockType = CkJsonObject_stringOf(json,"responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"); CkJsonObject_ObjectOf2(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObject_SizeOfArray(json1,"property.detectedLanguages"); while (i1 < count_i1) { CkJsonObject_putI(json1,i1); languageCode = CkJsonObject_stringOf(json1,"property.detectedLanguages[i].languageCode"); i1 = i1 + 1; } CkJsonObject_ObjectOf2(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObject_SizeOfArray(json1,"boundingBox.vertices"); while (i1 < count_i1) { CkJsonObject_putI(json1,i1); x = CkJsonObject_IntOf(json1,"boundingBox.vertices[i].x"); y = CkJsonObject_IntOf(json1,"boundingBox.vertices[i].y"); i1 = i1 + 1; } CkJsonObject_ObjectOf2(json,"responses[i].fullTextAnnotation.pages[j].blocks[k]",json1); i1 = 0; count_i1 = CkJsonObject_SizeOfArray(json1,"paragraphs"); while (i1 < count_i1) { CkJsonObject_putI(json1,i1); j1 = 0; count_j1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].property.detectedLanguages"); while (j1 < count_j1) { CkJsonObject_putJ(json1,j1); languageCode = CkJsonObject_stringOf(json1,"paragraphs[i].property.detectedLanguages[j].languageCode"); j1 = j1 + 1; } j1 = 0; count_j1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].boundingBox.vertices"); while (j1 < count_j1) { CkJsonObject_putJ(json1,j1); x = CkJsonObject_IntOf(json1,"paragraphs[i].boundingBox.vertices[j].x"); y = CkJsonObject_IntOf(json1,"paragraphs[i].boundingBox.vertices[j].y"); j1 = j1 + 1; } j1 = 0; count_j1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].words"); while (j1 < count_j1) { CkJsonObject_putJ(json1,j1); k1 = 0; count_k1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].words[j].property.detectedLanguages"); while (k1 < count_k1) { CkJsonObject_putK(json1,k1); languageCode = CkJsonObject_stringOf(json1,"paragraphs[i].words[j].property.detectedLanguages[k].languageCode"); k1 = k1 + 1; } k1 = 0; count_k1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].words[j].boundingBox.vertices"); while (k1 < count_k1) { CkJsonObject_putK(json1,k1); x = CkJsonObject_IntOf(json1,"paragraphs[i].words[j].boundingBox.vertices[k].x"); y = CkJsonObject_IntOf(json1,"paragraphs[i].words[j].boundingBox.vertices[k].y"); k1 = k1 + 1; } k1 = 0; count_k1 = CkJsonObject_SizeOfArray(json1,"paragraphs[i].words[j].symbols"); while (k1 < count_k1) { CkJsonObject_putK(json1,k1); text = CkJsonObject_stringOf(json1,"paragraphs[i].words[j].symbols[k].text"); propertyDetectedBreakType = CkJsonObject_stringOf(json1,"paragraphs[i].words[j].symbols[k].property.detectedBreak.type"); CkJsonObject_ObjectOf2(json1,"paragraphs[i].words[j].symbols[k]",json2); i2 = 0; count_i2 = CkJsonObject_SizeOfArray(json2,"property.detectedLanguages"); while (i2 < count_i2) { CkJsonObject_putI(json2,i2); languageCode = CkJsonObject_stringOf(json2,"property.detectedLanguages[i].languageCode"); i2 = i2 + 1; } CkJsonObject_ObjectOf2(json1,"paragraphs[i].words[j].symbols[k]",json2); i2 = 0; count_i2 = CkJsonObject_SizeOfArray(json2,"boundingBox.vertices"); while (i2 < count_i2) { CkJsonObject_putI(json2,i2); x = CkJsonObject_IntOf(json2,"boundingBox.vertices[i].x"); y = CkJsonObject_IntOf(json2,"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; } printf("Success.\n"); CkBinData_Dispose(imageData); CkJsonObject_Dispose(json); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkHttpResponse_Dispose(resp); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(json1); CkJsonObject_Dispose(json2); } | ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.