unit Unit1;
interface
uses
Windows, SysUtils, Classes, Forms, Dialogs, Controls, StdCtrls, InvokeRegistry,
Rio, SOAPHTTPClient, ShellAPI, Types, IMiSOCRServer1;
type
TForm1 = class(TForm)
HTTPRIO1: THTTPRIO;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
IMiSOCRServerWebservice: IMiSOCRServer;
Image: TByteDynArray;
Token: string;
AParams: AnalysisParams;
AReply: AnalysisReply;
ProgressObj: ProgressDetails;
RParams: RecognitionParams;
RReply: RecognitionReply;
Langs: Languages;
EParams: ExportParams;
EReply: ExportReply;
EResult: ExportResult;
FileName: string;
begin
try
IMiSOCRServerWebservice := HTTPRIO1 as IMiSOCRServer;
(*** 1. PREPARE DOCUMENT STAGE ***)
// Read a document into byte array
with TFileStream.Create('C:\test.tif', fmOpenRead) do
try
SetLength(Image, Size);
Read(Image[0], Size);
finally
Free;
end;
// Get IMiS/OCR Server job token
Token := IMiSOCRServerWebservice.PrepareDocument(Image);
(*** 2. ANALYZE DOCUMENT STAGE ***)
// Create and set AnalysisParams object
AParams := AnalysisParams.Create;
AParams.LayoutType := LayoutType.Autodetect;
AParams.DeskewImage := True;
AParams.RemoveNoise := True;
// Create and set AnalysisReply object
AReply := AnalysisReply.Create;
// Send AnalyzeDocument request
IMiSOCRServerWebservice.AnalyzeDocument(Token, AParams, AReply);
(*** 3. WAIT FOR ANALYZE STAGE TO COMPLETE ***)
// Check analysis progress details - finished when 100 (success) or -1 (error)
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
while ((ProgressObj.Stage <> RequestStage.Analysis) or
((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
Sleep(100); // Give IMiS/OCR Server time to process request
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
end;
(*** 4. RECOGNIZE DOCUMENT STAGE ***)
// Create and set RecognitionParams object
RParams := RecognitionParams.Create;
RParams.ErrorMarkingLevel := ErrorMarkingLevel.None;
SetLength(Langs, 2);
Langs[0] := Language.English;
Langs[1] := Language.Slovenian;
RParams.Languages := Langs;
RParams.TextType := TextType.Normal;
// Create and set RecognitionReply object
RReply := RecognitionReply.Create;
// Send RecognizeDocument request
IMiSOCRServerWebservice.RecognizeDocument(Token, RParams, RReply);
(*** 5. WAIT FOR RECOGNIZE STAGE TO COMPLETE ***)
// Check analysis progress details - finished when 100 (success) or -1 (error)
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
while ((ProgressObj.Stage <> RequestStage.Recognition) or
((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
Sleep(100); // Give IMiS/OCR Server time to process request
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
end;
(*** 6. EXPORT DOCUMENT STAGE ***)
// Create and set ExportParams object
EParams := ExportParams.Create;
EParams.FileFormat := FileFormat.PDF;
EParams.PDFParams := PDFParams.Create;
EParams.PDFParams.SaveMode := PDFMode.ImageOnText;
EParams.PDFParams.UncertainWordsAsImage := False;
EParams.PDFParams.KeepColor := False;
EParams.PDFParams.PictureQuality := 50;
EParams.PDFParams.PictureResolution := 200;
// Create and set ExportReply object
EReply := ExportReply.Create;
// Send ExportDocument request
IMiSOCRServerWebservice.ExportDocument(Token, EParams, EReply);
(*** 7. WAIT FOR EXPORT STAGE TO COMPLETE ***)
// Check export progress details - finished when 100 (success) or -1 (error)
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
while ((ProgressObj.Stage <> RequestStage.Export) or
((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
Sleep(100); // Give IMiS/OCR Server time to process request
ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
end;
(*** 8. RETRIEVE EXPORTED DOCUMENT ***)
// Get ExportResult object
EResult := IMiSOCRServerWebservice.GetExportResult(Token);
// Set output file name
FileName := ChangeFileExt(Application.ExeName, '.pdf');
// Read byte array into output file
with TFileStream.Create(FileName, fmCreate) do
try
Write(EResult.OutputFile[0], Length(EResult.OutputFile));
finally
Free;
end;
// Execute output file with associated application
ShellExecute(0, PChar('OPEN'), PChar(FileName), nil, nil, SW_SHOWNORMAL)
except
on E: Exception do
ShowMessageFmt('Error: %s: %s', [E.ClassName, E.Message]);
end;
end;
end.
|