-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWorking with an existing FlexiCapture project.cs
413 lines (332 loc) · 15.3 KB
/
Working with an existing FlexiCapture project.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// © ABBYY. 2012.
// SAMPLES code is property of ABBYY, exclusive rights are reserved.
// DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it
// under the terms of License Agreement between ABBYY and DEVELOPER.
// Product: ABBYY FlexiCapture Engine 10
// Description: Working with an existing FlexiCapture project
using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using FCEngine;
namespace Sample
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Working with an existing FlexiCapture project
class WorkingWithFlexiCaptureProject : FlexiCaptureEngineSnippets
{
// USE CASE: Using a configured FlexiCapture project to recognize image files
public static void Using_a_configured_FlexiCapture_project_to_recognize_image_files( IEngine engine )
{
trace( "Open the sample project..." );
IProject project = engine.OpenProject("D:\\Users\\seral\\Desktop\\SampleImages\\SampleProject\\DocScan1CProject\\DocScan1CProject.fcproj");
try {
trace( "Add a new batch..." );
IBatch batch = project.Batches.AddNew("DocScan1C");
trace( "Open the batch..." );
batch.Open();
try {
IStringsCollection filePaths = engine.CreateStringsCollection();
filePaths.Add(SamplesFolder + "1134.pdf");
filePaths.Add(SamplesFolder + "Scan_07_èç_06.bmp");
//filePaths.Add(SamplesFolder + "Scan_02_èç_06.jpg");
// filePaths.Add(SamplesFolder + "Scan_08_èç_06.jpg");*/
IStringsCollection badFiles = engine.CreateStringsCollection(); ;
ILongsCollection docIds = engine.CreateLongsCollection();
trace("Add image files to the batch...");
batch.AddImages(filePaths, -1, null, null, badFiles, docIds);
trace( "Recognize all images in the batch..." );
batch.Recognize( null, RecognitionModeEnum.RM_ReRecognizeMinimal, null );
trace( "Export the results..." );
IExportParams exportParams = engine.CreateExportParams(ExportDestinationTypeEnum.EDT_File);
exportParams.IgnoreRuleErrors = true;
exportParams.FileExportParams.RootPath = "D:\\Users\\seral\\Desktop\\FCEExport";
exportParams.FileExportParams.FileFormat = FileExportFormatEnum.FEF_XML;
exportParams.FileExportParams.FileNamePattern = "<DocumentDefinition>";
exportParams.FileExportParams.CreateSeparateFilesForRepeatableItems = true;
exportParams.FileExportParams.FileOverwriteRule = FileOverwriteRuleEnum.FOR_Rename;
batch.Export(null, exportParams);
trace( "Close and delete the batch..." );
} finally {
batch.Close(); // Before the batch could be deleted, it has to be closed
project.Batches.DeleteAll();
}
trace( "Close the project..." );
} finally {
project.Close();
}
}
// USE CASE: Verifying recognized documents
public static void Verifying_recognized_documents( IEngine engine )
{
trace( "Open the sample project..." );
IProject project = engine.OpenProject( SamplesFolder + "\\SampleProject\\Invoices_eng.fcproj" );
try {
trace( "Prepare a new batch for verification..." );
IBatch batch = PrepareNewRecognizedBatch( engine, project );
try {
traceBegin( "Run verification..." );
trace( "Start verification session (all documents in the batch)..." );
IVerificationSession verificationSession = batch.StartVerification( null );
try {
trace( "Change verification options if required..." );
IVerificationOptions verificationOptions = verificationSession.Options;
verificationOptions.VerifyExtraSymbols = true;
trace( "Open a set of documents (a work set) and collect objects that need to be verified..." );
IVerificationWorkSet verificationWorkSet = verificationSession.NextWorkSet();
while( verificationWorkSet != null ) {
trace( "For each group of objects show the objects to the verification operator for confirmation..." );
IVerificationGroup verificationGroup = verificationWorkSet.NextGroup();
while( verificationGroup != null ) {
trace( "Verification Group: " + verificationGroup.Description + " (confirm all)" );
for( int i = 0; i < verificationGroup.Count; i++ ) {
IVerificationObject verificationObject = verificationGroup.Item( i );
if( verificationObject.Type == VerificationObjectTypeEnum.VOT_Group ) {
verificationObject.State = VerificationObjectStateEnum.VOS_Confirmed;
} else {
IContextVerificationObject contextVerificationObject = verificationObject.AsContextVerificationObject();
// If field value is modified during verification you should recheck rules for
// the corresponding field
contextVerificationObject.CheckRules();
contextVerificationObject.Field.Value.SetVerified();
}
}
verificationGroup = verificationWorkSet.NextGroup();
}
trace( "Save verification results (all documents in the set)..." );
verificationWorkSet.Commit();
trace( "Open the next set of documents..." );
verificationWorkSet = verificationSession.NextWorkSet();
}
trace( "Close the session..." );
} finally {
// Verification consumes considerable system resources (many simultaniously
// open and loaded documents and images). So it is VERY important that
// these resources should be released in timely manner and not left for
// garbage collector to manage.
verificationSession.Close();
}
trace( "Check that the documents do not need verification now..." );
IDocuments batchDocuments = batch.Documents;
for( int i = 0; i < batchDocuments.Count; i++ ) {
IDocument document = batchDocuments.Item( i );
document.Open( false );
try {
recursiveCheckVerified( engine, document.Sections );
} finally {
document.Close( false );
}
}
traceEnd( "OK" );
} finally {
batch.Close();
project.Batches.DeleteAll();
}
} finally {
project.Close();
}
traceEnd( "OK" );
}
// USE CASE: Using batch processing events
public static void Using_batch_processing_events( IEngine engine )
{
trace( "Open a project..." );
IProject project = engine.OpenProject( SamplesFolder + "\\SampleProject\\Invoices_eng.fcproj" );
try {
trace( "Add a new batch..." );
IBatch batch = project.Batches.AddNew( "TestBatch" );
trace( "Open the new batch..." );
batch.Open();
// Here we create an events sink which will capture batch processing events and
// show progress and tracing messages in the status bar. This will also make UI
// more responsive to user input. See definition of the sink class for details
trace( "Start listening to batch processing events..." );
SampleBatchEventsSink eventsSink = new SampleBatchEventsSink( batch );
try {
trace( "Add the image files to the batch..." );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_1.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_2.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_3.tif" );
trace( "Recognize the batch..." );
batch.Recognize( null, RecognitionModeEnum.RM_ReRecognizeMinimal, null );
trace( "Export the results..." );
batch.Export( null, null );
trace( "Close and delete the batch..." );
} finally {
eventsSink.Dispose();
batch.Close();
project.Batches.DeleteAll();
}
trace( "Close the project..." );
} finally {
project.Close();
}
}
// USE CASE: Using collection events
public static void Using_collection_events( IEngine engine )
{
// Collection events are mainly intended for use in creating UI views for FlexiCapture Engine objects.
// By listening to collection events listeners can duly react to changes in the model.
trace( "Open the project..." );
IProject project = engine.OpenProject( SamplesFolder + "\\SampleProject\\Invoices_eng.fcproj" );
// Start listening to project.Batches events. In a real-life usage scenario the listener
// would be some kind of a visual control, showing the list of batches and redrawing
// itself when the collection changes
SampleCollectionEventsSink batchesEvents = new SampleCollectionEventsSink( project.Batches, "project.Batches" );
try {
trace( "Add a new batch..." );
IBatch batch = project.Batches.AddNew( "TestBatch" );
trace( "Open the new batch..." );
batch.Open();
// Start listening to batch.Documents events. In a real-life usage scenario the listener
// would be some kind of a visual control, showing the list of documents and redrawing
// itself when the collection changes
SampleCollectionEventsSink documentsEvents = new SampleCollectionEventsSink( batch.Documents, "batch.Documents" );
try {
trace( "Add the image files to the batch..." );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_1.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_2.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_3.tif" );
trace( "Recognize the batch..." );
batch.Recognize( null, RecognitionModeEnum.RM_ReRecognizeMinimal, null );
trace( "Export the results..." );
batch.Export( null, null );
trace( "Close and delete the batch..." );
} finally {
documentsEvents.Dispose();
batch.Close();
project.Batches.DeleteAll();
}
trace( "Close the project..." );
} finally {
batchesEvents.Dispose();
project.Close();
}
}
#region Sample Auxiliary Classes and Methods
// Sample Event Sinks ///////////////////////////////////////////////////////////
class SampleBatchEventsSink
{
public SampleBatchEventsSink( object source )
{
// Check that the source object is a IBatchEvents source (projects and batches are).
// Using the helper class generated by the framework which maps events source methods to delegates
batchEvents = source as IBatchEvents_Event;
assert( batchEvents != null );
batchEvents.OnBatchProgress += new IBatchEvents_OnBatchProgressEventHandler( batchEvents_OnBatchProgress );
batchEvents.OnProcessMessages += new IBatchEvents_OnProcessMessagesEventHandler(batchEvents_OnProcessMessages);
batchEvents.OnRecognizerTip +=new IBatchEvents_OnRecognizerTipEventHandler(batchEvents_OnRecognizerTip);
}
public void Dispose()
{
batchEvents.OnBatchProgress -= new IBatchEvents_OnBatchProgressEventHandler( batchEvents_OnBatchProgress );
batchEvents.OnProcessMessages -= new IBatchEvents_OnProcessMessagesEventHandler(batchEvents_OnProcessMessages);
batchEvents.OnRecognizerTip -=new IBatchEvents_OnRecognizerTipEventHandler(batchEvents_OnRecognizerTip);
batchEvents = null;
}
#region IMPLEMENTATION
IBatchEvents_Event batchEvents;
private void batchEvents_OnBatchProgress( IBatch batch, int processedItemsCount, int remainingItemsCount )
{
traceProgress( ( processedItemsCount * 100 ) / ( processedItemsCount + remainingItemsCount ) );
}
private void batchEvents_OnProcessMessages( ref bool ShouldTerminate )
{
Application.DoEvents();
}
private void batchEvents_OnRecognizerTip( IPage Page, string RecognizerTip )
{
traceDetail( RecognizerTip );
}
#endregion
};
class SampleCollectionEventsSink
{
public SampleCollectionEventsSink( object source, string _name )
{
// Check that the source object is a ICollectionEvents source (most collections are).
// Using the helper class generated by the framework which maps event source methods to delegates
collectionEvents = source as ICollectionEvents_Event;
assert( collectionEvents != null );
name = _name;
collectionEvents.OnCollectionChanged += new ICollectionEvents_OnCollectionChangedEventHandler( collectionEvents_OnCollectionChanged );
}
public void Dispose()
{
collectionEvents.OnCollectionChanged -= new ICollectionEvents_OnCollectionChangedEventHandler( collectionEvents_OnCollectionChanged );
collectionEvents = null;
}
#region IMPLEMENTATION
ICollectionEvents_Event collectionEvents;
string name;
void collectionEvents_OnCollectionChanged( bool reloaded,
ILongsCollection added, ILongsCollection deleted, ILongsCollection modified )
{
try {
if( reloaded ) {
trace( name + ": RESET" );
} else {
string addedTxt = added != null ? " ADDED:" + added.Count : "";
string deletedTxt = deleted != null ? " DELETED:" + deleted.Count : "";
string modifiedTxt = modified != null ? " MODIFIED:" + modified.Count : "";
trace( name + ":" + addedTxt + deletedTxt + modifiedTxt );
}
} catch( Exception e ) {
trace( e.Message );
}
}
#endregion
};
// Auxiliary Tools ///////////////////////////////////////////////////////////
static IBatch PrepareNewRecognizedBatch( IEngine engine, IProject project )
{
IBatch batch = project.Batches.AddNew( "TestBatch" );
batch.Open();
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_1.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_2.tif" );
batch.AddImage( SamplesFolder + "\\SampleImages\\Invoices_3.tif" );
batch.Recognize( null, RecognitionModeEnum.RM_ReRecognizeMinimal, null );
return batch;
}
///////////////////////////////////////////////////////////////////////////////
static void recursiveCheckVerified( IEngine engine, IField field )
{
if( field.Value != null ) {
assert( !field.Value.NeedsVerification );
if( field.Value.Type == FieldValueTypeEnum.FVT_Text ) {
IText text = field.Value.AsText;
ICharParams p = engine.CreateCharParams();
for( int i = 0; i < text.Length; i++ ) {
text.GetCharParams( i, p );
assert( !p.NeedsVerification );
}
}
}
recursiveCheckVerified( engine, field.Instances );
recursiveCheckVerified( engine, field.Children );
}
static void recursiveCheckVerified( IEngine engine, IFields fields )
{
if( fields != null ) {
for( int i = 0; i < fields.Count; i++ ) {
recursiveCheckVerified( engine, fields[i] );
}
}
}
static void recursiveCheckVerified( IEngine engine, IFieldInstances fields )
{
if( fields != null ) {
for( int i = 0; i < fields.Count; i++ ) {
recursiveCheckVerified( engine, fields[i] );
}
}
}
///////////////////////////////////////////////////////////////////////////////
#endregion
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
}