React Native Requires Chilkat v11.0.0+
React Native
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 React Native Downloads
import { BinData, Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// 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.
const imageData = new BinData();
// This image file contains some text...
try {
imageData.loadFile('qa_data/jpg/text.jpg');
} catch {
console.log('Failed to load image file.');
return;
}
// Create the above JSON.
const json = new JsonObject();
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
const http = new Http();
http.sessionLogFilename = 'c:/aaworkarea/sessionLog.txt';
const sb = new StringBuilder();
const url = 'https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY';
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', url, json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
console.log(`status = ${resp.statusCode}`);
// A 200 response status indicate success.
if (resp.statusCode !== 200) {
console.log(resp.bodyStr);
console.log('Failed.');
return;
}
const sbResponseBody = new StringBuilder();
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:
let i = 0;
let fullTextAnnotationText = '';
let j = 0;
let countJ = 0;
let locale = '';
let description = '';
let k = 0;
let countK = 0;
let x = 0;
let y = 0;
let width = 0;
let height = 0;
let languageCode = '';
let blockType = '';
let i1 = 0;
let countI1 = 0;
let j1 = 0;
let countJ1 = 0;
let k1 = 0;
let countK1 = 0;
let text = '';
let propertyDetectedBreakType = '';
let i2 = 0;
let countI2 = 0;
const json1 = new JsonObject();
const json2 = new JsonObject();
i = 0;
const 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++;
}
console.log('Success.');
}