Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, BinData, StringBuilder, HttpResponse, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
imageData: HCkBinData;
json: HCkJsonObject;
http: HCkHttp;
sb: HCkStringBuilder;
url: PWideChar;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
i: Integer;
count_i: Integer;
fullTextAnnotationText: PWideChar;
j: Integer;
count_j: Integer;
locale: PWideChar;
description: PWideChar;
k: Integer;
count_k: Integer;
x: Integer;
y: Integer;
width: Integer;
height: Integer;
languageCode: PWideChar;
blockType: PWideChar;
i1: Integer;
count_i1: Integer;
j1: Integer;
count_j1: Integer;
k1: Integer;
count_k1: Integer;
text: PWideChar;
propertyDetectedBreakType: PWideChar;
i2: Integer;
count_i2: Integer;
json1: HCkJsonObject;
json2: HCkJsonObject;
begin
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) then
begin
Memo1.Lines.Add('Failed to load image file.');
Exit;
end;
// 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) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
// A 200 response status indicate success.
if (CkHttpResponse_getStatusCode(resp) <> 200) then
begin
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));
Memo1.Lines.Add('Failed.');
Exit;
end;
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 do
begin
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 do
begin
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 do
begin
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;
end;
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(json,'responses[i].fullTextAnnotation.pages');
while j < count_j do
begin
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 do
begin
CkJsonObject_putK(json,k);
languageCode := CkJsonObject__stringOf(json,'responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode');
k := k + 1;
end;
k := 0;
count_k := CkJsonObject_SizeOfArray(json,'responses[i].fullTextAnnotation.pages[j].blocks');
while k < count_k do
begin
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 do
begin
CkJsonObject_putI(json1,i1);
languageCode := CkJsonObject__stringOf(json1,'property.detectedLanguages[i].languageCode');
i1 := i1 + 1;
end;
CkJsonObject_ObjectOf2(json,'responses[i].fullTextAnnotation.pages[j].blocks[k]',json1);
i1 := 0;
count_i1 := CkJsonObject_SizeOfArray(json1,'boundingBox.vertices');
while i1 < count_i1 do
begin
CkJsonObject_putI(json1,i1);
x := CkJsonObject_IntOf(json1,'boundingBox.vertices[i].x');
y := CkJsonObject_IntOf(json1,'boundingBox.vertices[i].y');
i1 := i1 + 1;
end;
CkJsonObject_ObjectOf2(json,'responses[i].fullTextAnnotation.pages[j].blocks[k]',json1);
i1 := 0;
count_i1 := CkJsonObject_SizeOfArray(json1,'paragraphs');
while i1 < count_i1 do
begin
CkJsonObject_putI(json1,i1);
j1 := 0;
count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].property.detectedLanguages');
while j1 < count_j1 do
begin
CkJsonObject_putJ(json1,j1);
languageCode := CkJsonObject__stringOf(json1,'paragraphs[i].property.detectedLanguages[j].languageCode');
j1 := j1 + 1;
end;
j1 := 0;
count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].boundingBox.vertices');
while j1 < count_j1 do
begin
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;
end;
j1 := 0;
count_j1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words');
while j1 < count_j1 do
begin
CkJsonObject_putJ(json1,j1);
k1 := 0;
count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].property.detectedLanguages');
while k1 < count_k1 do
begin
CkJsonObject_putK(json1,k1);
languageCode := CkJsonObject__stringOf(json1,'paragraphs[i].words[j].property.detectedLanguages[k].languageCode');
k1 := k1 + 1;
end;
k1 := 0;
count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].boundingBox.vertices');
while k1 < count_k1 do
begin
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;
end;
k1 := 0;
count_k1 := CkJsonObject_SizeOfArray(json1,'paragraphs[i].words[j].symbols');
while k1 < count_k1 do
begin
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 do
begin
CkJsonObject_putI(json2,i2);
languageCode := CkJsonObject__stringOf(json2,'property.detectedLanguages[i].languageCode');
i2 := i2 + 1;
end;
CkJsonObject_ObjectOf2(json1,'paragraphs[i].words[j].symbols[k]',json2);
i2 := 0;
count_i2 := CkJsonObject_SizeOfArray(json2,'boundingBox.vertices');
while i2 < count_i2 do
begin
CkJsonObject_putI(json2,i2);
x := CkJsonObject_IntOf(json2,'boundingBox.vertices[i].x');
y := CkJsonObject_IntOf(json2,'boundingBox.vertices[i].y');
i2 := i2 + 1;
end;
k1 := k1 + 1;
end;
j1 := j1 + 1;
end;
i1 := i1 + 1;
end;
k := k + 1;
end;
j := j + 1;
end;
i := i + 1;
end;
Memo1.Lines.Add('Success.');
CkBinData_Dispose(imageData);
CkJsonObject_Dispose(json);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(json1);
CkJsonObject_Dispose(json2);
end;