Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// 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.
let image_data = chilkat::BinData::new();
// This image file contains some text...
if image_data.load_file("qa_data/jpg/text.jpg").is_err() {
println!("Failed to load image file.");
return;
}
// Create the above JSON.
let json = chilkat::JsonObject::new();
let _ = json.update_bd("requests[0].image.content", "base64", &image_data);
let _ = json.update_string("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
let http = chilkat::Http::new();
http.set_session_log_filename("c:/aaworkarea/sessionLog.txt");
let sb = chilkat::StringBuilder::new();
let url = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("status = {}", resp.status_code());
// A 200 response status indicate success.
if resp.status_code() != 200 {
println!("{}", resp.body_str());
println!("Failed.");
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let _ = sb_response_body.write_file("qa_output/textDetectResponse.json", "utf-8", false);
let _ = json.load_sb(&sb_response_body);
// 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:
let mut i: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut k: i32 = 0;
let mut count_k: i32 = 0;
let mut i1: i32 = 0;
let mut count_i1: i32 = 0;
let mut j1: i32 = 0;
let mut count_j1: i32 = 0;
let mut k1: i32 = 0;
let mut count_k1: i32 = 0;
let mut i2: i32 = 0;
let mut count_i2: i32 = 0;
let json1 = chilkat::JsonObject::new();
let json2 = chilkat::JsonObject::new();
i = 0;
let count_i = json.size_of_array("responses");
while i < count_i {
json.set_i(i);
let _ = json.string_of("responses[i].fullTextAnnotation.text").unwrap_or_default();
j = 0;
count_j = json.size_of_array("responses[i].textAnnotations");
while j < count_j {
json.set_j(j);
let _ = json.string_of("responses[i].textAnnotations[j].locale").unwrap_or_default();
let _ = json.string_of("responses[i].textAnnotations[j].description").unwrap_or_default();
k = 0;
count_k = json.size_of_array("responses[i].textAnnotations[j].boundingPoly.vertices");
while k < count_k {
json.set_k(k);
let _ = json.int_of("responses[i].textAnnotations[j].boundingPoly.vertices[k].x");
let _ = json.int_of("responses[i].textAnnotations[j].boundingPoly.vertices[k].y");
k = k + 1;
}
j = j + 1;
}
j = 0;
count_j = json.size_of_array("responses[i].fullTextAnnotation.pages");
while j < count_j {
json.set_j(j);
let _ = json.int_of("responses[i].fullTextAnnotation.pages[j].width");
let _ = json.int_of("responses[i].fullTextAnnotation.pages[j].height");
k = 0;
count_k = json.size_of_array("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages");
while k < count_k {
json.set_k(k);
let _ = json.string_of("responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode").unwrap_or_default();
k = k + 1;
}
k = 0;
count_k = json.size_of_array("responses[i].fullTextAnnotation.pages[j].blocks");
while k < count_k {
json.set_k(k);
let _ = json.string_of("responses[i].fullTextAnnotation.pages[j].blocks[k].blockType").unwrap_or_default();
let _ = json.object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", &json1);
i1 = 0;
count_i1 = json1.size_of_array("property.detectedLanguages");
while i1 < count_i1 {
json1.set_i(i1);
let _ = json1.string_of("property.detectedLanguages[i].languageCode").unwrap_or_default();
i1 = i1 + 1;
}
let _ = json.object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", &json1);
i1 = 0;
count_i1 = json1.size_of_array("boundingBox.vertices");
while i1 < count_i1 {
json1.set_i(i1);
let _ = json1.int_of("boundingBox.vertices[i].x");
let _ = json1.int_of("boundingBox.vertices[i].y");
i1 = i1 + 1;
}
let _ = json.object_of2("responses[i].fullTextAnnotation.pages[j].blocks[k]", &json1);
i1 = 0;
count_i1 = json1.size_of_array("paragraphs");
while i1 < count_i1 {
json1.set_i(i1);
j1 = 0;
count_j1 = json1.size_of_array("paragraphs[i].property.detectedLanguages");
while j1 < count_j1 {
json1.set_j(j1);
let _ = json1.string_of("paragraphs[i].property.detectedLanguages[j].languageCode").unwrap_or_default();
j1 = j1 + 1;
}
j1 = 0;
count_j1 = json1.size_of_array("paragraphs[i].boundingBox.vertices");
while j1 < count_j1 {
json1.set_j(j1);
let _ = json1.int_of("paragraphs[i].boundingBox.vertices[j].x");
let _ = json1.int_of("paragraphs[i].boundingBox.vertices[j].y");
j1 = j1 + 1;
}
j1 = 0;
count_j1 = json1.size_of_array("paragraphs[i].words");
while j1 < count_j1 {
json1.set_j(j1);
k1 = 0;
count_k1 = json1.size_of_array("paragraphs[i].words[j].property.detectedLanguages");
while k1 < count_k1 {
json1.set_k(k1);
let _ = json1.string_of("paragraphs[i].words[j].property.detectedLanguages[k].languageCode").unwrap_or_default();
k1 = k1 + 1;
}
k1 = 0;
count_k1 = json1.size_of_array("paragraphs[i].words[j].boundingBox.vertices");
while k1 < count_k1 {
json1.set_k(k1);
let _ = json1.int_of("paragraphs[i].words[j].boundingBox.vertices[k].x");
let _ = json1.int_of("paragraphs[i].words[j].boundingBox.vertices[k].y");
k1 = k1 + 1;
}
k1 = 0;
count_k1 = json1.size_of_array("paragraphs[i].words[j].symbols");
while k1 < count_k1 {
json1.set_k(k1);
let _ = json1.string_of("paragraphs[i].words[j].symbols[k].text").unwrap_or_default();
let _ = json1.string_of("paragraphs[i].words[j].symbols[k].property.detectedBreak.type").unwrap_or_default();
let _ = json1.object_of2("paragraphs[i].words[j].symbols[k]", &json2);
i2 = 0;
count_i2 = json2.size_of_array("property.detectedLanguages");
while i2 < count_i2 {
json2.set_i(i2);
let _ = json2.string_of("property.detectedLanguages[i].languageCode").unwrap_or_default();
i2 = i2 + 1;
}
let _ = json1.object_of2("paragraphs[i].words[j].symbols[k]", &json2);
i2 = 0;
count_i2 = json2.size_of_array("boundingBox.vertices");
while i2 < count_i2 {
json2.set_i(i2);
let _ = json2.int_of("boundingBox.vertices[i].x");
let _ = json2.int_of("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;
}
println!("Success.");