Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// 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.
final imageData = CkBinData();
// This image file contains some text...
try {
imageData.loadFile('qa_data/jpg/text.jpg');
} on ChilkatException {
print('Failed to load image file.');
return;
}
// Create the above JSON.
final json = CkJsonObject();
json.updateBd('requests[0].image.content', 'base64', imageData);
json.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
final http = CkHttp();
http.sessionLogFilename = 'c:/aaworkarea/sessionLog.txt';
final sb = CkStringBuilder();
final url = 'https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY';
final resp = CkHttpResponse();
try {
http.httpJson('POST', url, json, 'application/json', resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('status = ${resp.statusCode}');
// A 200 response status indicate success.
if (resp.statusCode != 200) {
print(resp.bodyStr);
print('Failed.');
return;
}
final sbResponseBody = CkStringBuilder();
resp.getBodySb(sbResponseBody);
sbResponseBody.writeFile('qa_output/textDetectResponse.json', '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:
var i = 0;
var fullTextAnnotationText = '';
var j = 0;
var countJ = 0;
var locale = '';
var description = '';
var k = 0;
var countK = 0;
var x = 0;
var y = 0;
var width = 0;
var height = 0;
var languageCode = '';
var blockType = '';
var i1 = 0;
var countI1 = 0;
var j1 = 0;
var countJ1 = 0;
var k1 = 0;
var countK1 = 0;
var text = '';
var propertyDetectedBreakType = '';
var i2 = 0;
var countI2 = 0;
final json1 = CkJsonObject();
final json2 = CkJsonObject();
i = 0;
final countI = json.sizeOfArray('responses');
while (i < countI) {
json.i = i;
fullTextAnnotationText = json.stringOf('responses[i].fullTextAnnotation.text');
j = 0;
countJ = json.sizeOfArray('responses[i].textAnnotations');
while (j < countJ) {
json.j = j;
locale = json.stringOf('responses[i].textAnnotations[j].locale');
description = json.stringOf('responses[i].textAnnotations[j].description');
k = 0;
countK = json.sizeOfArray('responses[i].textAnnotations[j].boundingPoly.vertices');
while (k < countK) {
json.k = k;
x = json.intOf('responses[i].textAnnotations[j].boundingPoly.vertices[k].x');
y = json.intOf('responses[i].textAnnotations[j].boundingPoly.vertices[k].y');
k++;
}
j++;
}
j = 0;
countJ = json.sizeOfArray('responses[i].fullTextAnnotation.pages');
while (j < countJ) {
json.j = j;
width = json.intOf('responses[i].fullTextAnnotation.pages[j].width');
height = json.intOf('responses[i].fullTextAnnotation.pages[j].height');
k = 0;
countK = json.sizeOfArray('responses[i].fullTextAnnotation.pages[j].property.detectedLanguages');
while (k < countK) {
json.k = k;
languageCode = json.stringOf('responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode');
k++;
}
k = 0;
countK = json.sizeOfArray('responses[i].fullTextAnnotation.pages[j].blocks');
while (k < countK) {
json.k = k;
blockType = json.stringOf('responses[i].fullTextAnnotation.pages[j].blocks[k].blockType');
json.objectOf2('responses[i].fullTextAnnotation.pages[j].blocks[k]', json1);
i1 = 0;
countI1 = json1.sizeOfArray('property.detectedLanguages');
while (i1 < countI1) {
json1.i = i1;
languageCode = json1.stringOf('property.detectedLanguages[i].languageCode');
i1++;
}
json.objectOf2('responses[i].fullTextAnnotation.pages[j].blocks[k]', json1);
i1 = 0;
countI1 = json1.sizeOfArray('boundingBox.vertices');
while (i1 < countI1) {
json1.i = i1;
x = json1.intOf('boundingBox.vertices[i].x');
y = json1.intOf('boundingBox.vertices[i].y');
i1++;
}
json.objectOf2('responses[i].fullTextAnnotation.pages[j].blocks[k]', json1);
i1 = 0;
countI1 = json1.sizeOfArray('paragraphs');
while (i1 < countI1) {
json1.i = i1;
j1 = 0;
countJ1 = json1.sizeOfArray('paragraphs[i].property.detectedLanguages');
while (j1 < countJ1) {
json1.j = j1;
languageCode = json1.stringOf('paragraphs[i].property.detectedLanguages[j].languageCode');
j1++;
}
j1 = 0;
countJ1 = json1.sizeOfArray('paragraphs[i].boundingBox.vertices');
while (j1 < countJ1) {
json1.j = j1;
x = json1.intOf('paragraphs[i].boundingBox.vertices[j].x');
y = json1.intOf('paragraphs[i].boundingBox.vertices[j].y');
j1++;
}
j1 = 0;
countJ1 = json1.sizeOfArray('paragraphs[i].words');
while (j1 < countJ1) {
json1.j = j1;
k1 = 0;
countK1 = json1.sizeOfArray('paragraphs[i].words[j].property.detectedLanguages');
while (k1 < countK1) {
json1.k = k1;
languageCode = json1.stringOf('paragraphs[i].words[j].property.detectedLanguages[k].languageCode');
k1++;
}
k1 = 0;
countK1 = json1.sizeOfArray('paragraphs[i].words[j].boundingBox.vertices');
while (k1 < countK1) {
json1.k = k1;
x = json1.intOf('paragraphs[i].words[j].boundingBox.vertices[k].x');
y = json1.intOf('paragraphs[i].words[j].boundingBox.vertices[k].y');
k1++;
}
k1 = 0;
countK1 = json1.sizeOfArray('paragraphs[i].words[j].symbols');
while (k1 < countK1) {
json1.k = k1;
text = json1.stringOf('paragraphs[i].words[j].symbols[k].text');
propertyDetectedBreakType = json1.stringOf('paragraphs[i].words[j].symbols[k].property.detectedBreak.type');
json1.objectOf2('paragraphs[i].words[j].symbols[k]', json2);
i2 = 0;
countI2 = json2.sizeOfArray('property.detectedLanguages');
while (i2 < countI2) {
json2.i = i2;
languageCode = json2.stringOf('property.detectedLanguages[i].languageCode');
i2++;
}
json1.objectOf2('paragraphs[i].words[j].symbols[k]', json2);
i2 = 0;
countI2 = json2.sizeOfArray('boundingBox.vertices');
while (i2 < countI2) {
json2.i = i2;
x = json2.intOf('boundingBox.vertices[i].x');
y = json2.intOf('boundingBox.vertices[i].y');
i2++;
}
k1++;
}
j1++;
}
i1++;
}
k++;
}
j++;
}
i++;
}
print('Success.');
}