Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... 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
#include <CkBinDataW.h> #include <CkJsonObjectW.h> #include <CkHttpW.h> #include <CkStringBuilderW.h> #include <CkHttpResponseW.h> void ChilkatSample(void) { // 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. CkBinDataW imageData; // This image file contains some text... bool success = imageData.LoadFile(L"qa_data/jpg/text.jpg"); if (success != true) { wprintf(L"Failed to load image file.\n"); return; } // Create the above JSON. CkJsonObjectW json; json.UpdateBd(L"requests[0].image.content",L"base64",imageData); json.UpdateString(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 CkHttpW http; CkStringBuilderW sb; const wchar_t *url = L"https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"; CkHttpResponseW *resp = http.PostJson3(url,L"application/json",json); if (http.get_LastMethodSuccess() != true) { wprintf(L"%s\n",http.lastErrorText()); return; } wprintf(L"status = %d\n",resp->get_StatusCode()); // A 200 response status indicate success. if (resp->get_StatusCode() != 200) { wprintf(L"%s\n",resp->bodyStr()); wprintf(L"Failed.\n"); delete resp; return; } CkStringBuilderW sbResponseBody; resp->GetBodySb(sbResponseBody); sbResponseBody.WriteFile(L"qa_output/textDetectResponse.json",L"utf-8",false); json.LoadSb(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: int i; int count_i; const wchar_t *fullTextAnnotationText = 0; int j; int count_j; const wchar_t *locale = 0; const wchar_t *description = 0; int k; int count_k; int x; int y; int width; int height; const wchar_t *languageCode = 0; const wchar_t *blockType = 0; int i1; int count_i1; CkJsonObjectW *json1 = 0; int j1; int count_j1; int k1; int count_k1; const wchar_t *text = 0; const wchar_t *propertyDetectedBreakType = 0; int i2; int count_i2; CkJsonObjectW *json2 = 0; i = 0; count_i = json.SizeOfArray(L"responses"); while (i < count_i) { json.put_I(i); fullTextAnnotationText = json.stringOf(L"responses[i].fullTextAnnotation.text"); j = 0; count_j = json.SizeOfArray(L"responses[i].textAnnotations"); while (j < count_j) { json.put_J(j); locale = json.stringOf(L"responses[i].textAnnotations[j].locale"); description = json.stringOf(L"responses[i].textAnnotations[j].description"); k = 0; count_k = json.SizeOfArray(L"responses[i].textAnnotations[j].boundingPoly.vertices"); while (k < count_k) { json.put_K(k); x = json.IntOf(L"responses[i].textAnnotations[j].boundingPoly.vertices[k].x"); y = json.IntOf(L"responses[i].textAnnotations[j].boundingPoly.vertices[k].y"); k = k + 1; } j = j + 1; } j = 0; count_j = json.SizeOfArray(L"responses[i].fullTextAnnotation.pages"); while (j < count_j) { json.put_J(j); width = json.IntOf(L"responses[i].fullTextAnnotation.pages[j].width"); height = json.IntOf(L"responses[i].fullTextAnnotation.pages[j].height"); k = 0; count_k = json.SizeOfArray(L"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"); while (k < count_k) { json.put_K(k); languageCode = json.stringOf(L"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"); k = k + 1; } k = 0; count_k = json.SizeOfArray(L"responses[i].fullTextAnnotation.pages[j].blocks"); while (k < count_k) { json.put_K(k); blockType = json.stringOf(L"responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"); json1 = json.ObjectOf(L"responses[i].fullTextAnnotation.pages[j].blocks[k]"); i1 = 0; count_i1 = json1->SizeOfArray(L"property.detectedLanguages"); while (i1 < count_i1) { json1->put_I(i1); languageCode = json1->stringOf(L"property.detectedLanguages[i].languageCode"); i1 = i1 + 1; } delete json1; json1 = json.ObjectOf(L"responses[i].fullTextAnnotation.pages[j].blocks[k]"); i1 = 0; count_i1 = json1->SizeOfArray(L"boundingBox.vertices"); while (i1 < count_i1) { json1->put_I(i1); x = json1->IntOf(L"boundingBox.vertices[i].x"); y = json1->IntOf(L"boundingBox.vertices[i].y"); i1 = i1 + 1; } delete json1; json1 = json.ObjectOf(L"responses[i].fullTextAnnotation.pages[j].blocks[k]"); i1 = 0; count_i1 = json1->SizeOfArray(L"paragraphs"); while (i1 < count_i1) { json1->put_I(i1); j1 = 0; count_j1 = json1->SizeOfArray(L"paragraphs[i].property.detectedLanguages"); while (j1 < count_j1) { json1->put_J(j1); languageCode = json1->stringOf(L"paragraphs[i].property.detectedLanguages[j].languageCode"); j1 = j1 + 1; } j1 = 0; count_j1 = json1->SizeOfArray(L"paragraphs[i].boundingBox.vertices"); while (j1 < count_j1) { json1->put_J(j1); x = json1->IntOf(L"paragraphs[i].boundingBox.vertices[j].x"); y = json1->IntOf(L"paragraphs[i].boundingBox.vertices[j].y"); j1 = j1 + 1; } j1 = 0; count_j1 = json1->SizeOfArray(L"paragraphs[i].words"); while (j1 < count_j1) { json1->put_J(j1); k1 = 0; count_k1 = json1->SizeOfArray(L"paragraphs[i].words[j].property.detectedLanguages"); while (k1 < count_k1) { json1->put_K(k1); languageCode = json1->stringOf(L"paragraphs[i].words[j].property.detectedLanguages[k].languageCode"); k1 = k1 + 1; } k1 = 0; count_k1 = json1->SizeOfArray(L"paragraphs[i].words[j].boundingBox.vertices"); while (k1 < count_k1) { json1->put_K(k1); x = json1->IntOf(L"paragraphs[i].words[j].boundingBox.vertices[k].x"); y = json1->IntOf(L"paragraphs[i].words[j].boundingBox.vertices[k].y"); k1 = k1 + 1; } k1 = 0; count_k1 = json1->SizeOfArray(L"paragraphs[i].words[j].symbols"); while (k1 < count_k1) { json1->put_K(k1); text = json1->stringOf(L"paragraphs[i].words[j].symbols[k].text"); propertyDetectedBreakType = json1->stringOf(L"paragraphs[i].words[j].symbols[k].property.detectedBreak.type"); json2 = json1->ObjectOf(L"paragraphs[i].words[j].symbols[k]"); i2 = 0; count_i2 = json2->SizeOfArray(L"property.detectedLanguages"); while (i2 < count_i2) { json2->put_I(i2); languageCode = json2->stringOf(L"property.detectedLanguages[i].languageCode"); i2 = i2 + 1; } delete json2; json2 = json1->ObjectOf(L"paragraphs[i].words[j].symbols[k]"); i2 = 0; count_i2 = json2->SizeOfArray(L"boundingBox.vertices"); while (i2 < count_i2) { json2->put_I(i2); x = json2->IntOf(L"boundingBox.vertices[i].x"); y = json2->IntOf(L"boundingBox.vertices[i].y"); i2 = i2 + 1; } delete json2; k1 = k1 + 1; } j1 = j1 + 1; } i1 = i1 + 1; } delete json1; k = k + 1; } j = j + 1; } i = i + 1; } delete resp; wprintf(L"Success.\n"); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.