-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourCube.h
517 lines (460 loc) · 24.5 KB
/
FourCube.h
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// FourCube.h
#pragma once
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
// ------------------------------------------------------------------------------------------------------------------------------------------
// Global Defines
// ------------------------------------------------------------------------------------------------------------------------------------------
// Default timeout for "Process" operations
#define DEFULT_TIMEOUT 60000
namespace FourCube {
// ------------------------------------------------------------------------------------------------------------------------------------------
// Enumeration: FourCube.ExtractType
// ------------------------------------------------------------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Represents different types of extract.
// ------------------------------------------------------------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// ------------------------------------------------------------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// ------------------------------------------------------------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// ------------------------------------------------------------------------------------------------------------------------------------------
public enum class ExtractType
{
Block,
Paragraph,
Symbol,
Line,
Word
};
// ------------------------------------------------------------------------------------------------------------------------------------------
// Class: FourCube.Extract
// ------------------------------------------------------------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Represents an extracted text part, it's bound ( ... bounding rect), confidence level and other important details.
// ------------------------------------------------------------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// ------------------------------------------------------------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// ------------------------------------------------------------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// ------------------------------------------------------------------------------------------------------------------------------------------
public ref class Extract
{
public:
// --------------------------------------------------------------------------------------
// Property: Type
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the type of the extracted text part.
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property ExtractType Type;
// --------------------------------------------------------------------------------------
// Property: Text
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the extracted text part.
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property String ^ Text;
// --------------------------------------------------------------------------------------
// Property: Bound
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the bounding rect of this piece of text.
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property System::Drawing::Rectangle ^ Bound;
// --------------------------------------------------------------------------------------
// Property: Confidence
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the confidence level for the extracted text.
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/19/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property float ^ Confidence;
};
// ------------------------------------------------------------------------------------------------------------------------------------------
// Class: FourCube.OCR
// ------------------------------------------------------------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Performs Optical Charachter Recognition (OCR) using the Tesseract engine.
// ------------------------------------------------------------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// ------------------------------------------------------------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// ------------------------------------------------------------------------------------------------------------------------------------------
// 12/19/2013 | DMV |
// 12/17/2013 | DMV | Process(Pix) function.
// 12/16/2013 | DMV | Process(Bitmap) function, some image helper functions, Bound.
// 12/15/2013 | DMV | ProcessAll() and Process(STRING) functions, constructor, destructor, Timeout property.
// 12/14/2013 | DMV | Initial version and Create function.
// ------------------------------------------------------------------------------------------------------------------------------------------
public ref class OCR
{
private:
// The reference to the Tesseract API
tesseract::TessBaseAPI * _pAPI;
String ^ _dataPath;
String ^ _languageCode;
System::Drawing::Rectangle ^ _bound;
// --------------------------------------------------------------------------------------
// Method: BitmapToBitmap32BPP
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Convert any Bitmap to a 32BPP Bitmap.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - bitmap = The bitmap to convert.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: Bitmap @ 32BBP
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/16/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
Bitmap ^ BitmapToBitmap32BPP(System::Drawing::Bitmap ^ bitmap);
// --------------------------------------------------------------------------------------
// Method: BitmapToPIX
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Converts the specified System::Drawing::Bitmap to a comparable Leptonica PIX.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - bitmap = The bitmap to be converted.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A new Leptonica PIX instance.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/16/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
Pix * BitmapToPix(System::Drawing::Bitmap ^ bitmap);
// --------------------------------------------------------------------------------------
// Method: Process (PIX img)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified Leptonica PIX and return the resultant text.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - pPix = The Leptonica PIX to be OCR processed.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A String containing the recognized text.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/17/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
String ^ Process(Pix * pPix);
// --------------------------------------------------------------------------------------
// Method: Extract (PIX img)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified image and return the resultant extractions.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - pPix = The Leptonica PIX to be OCR processed.
// - type = The type of extract to be returned.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A List of Extracts that were recognized from the image.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 01/08/2014 | DMV | Initial version.
// --------------------------------------------------------------------------------------
List<FourCube::Extract ^>^ Extract(Pix * pPix, FourCube::ExtractType type);
public:
// --------------------------------------------------------------------------------------
// Property: Timeout
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the Timeout value used to specify the maximum time (in
// milliseconds) that Tesseract will take when processing a file/image.
// DEFAULT: 60000 (1 minute)
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property int Timeout;
// --------------------------------------------------------------------------------------
// Property: Bound
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Gets and Sets the Bound value used to specify the rectangular region to
// be processed for all subsequent file/image recognitions.
// DEFAULT: Rectangle::Empty (process the whole page/image)
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/16/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
property System::Drawing::Rectangle ^ Bound {
// The "Get" decleration
System::Drawing::Rectangle^ get();
// The "Set" declaration
void set(System::Drawing::Rectangle^ value);
}
// --------------------------------------------------------------------------------------
// Method: Create
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Create and initialize a new instance of the OCR system
// --------------------------------------------------------------------------------------
// Parameters:
//
// - dataPath = (Optional) The full path to the Tesseract data directory containing the language
// data files. If NULL or "empty string" is specified, a default value is formulated
// as {AppDomain::BaseDirectory}\data\
//
// Example: C:\Program Files\Program\data
//
// \tessdata
// - eng.DangAmbigs
// - eng.freq-dawg
// - eng.inttemp
// - eng.normproto
// - eng.pffmtable
// - eng.unicharset
// - eng.user-words
// - eng.word-dawg
//
// - languageCode = (Optional) The desired Tesseract language code. If NULL or "empty string"
// is specified, the default value is "eng".
//
// Example: "eng"
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A new OCR instance
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/14/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
static OCR ^ Create(String ^ dataPath, String ^ languageCode);
// --------------------------------------------------------------------------------------
// Method: Constructor
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Constructs a new OCR instance and initializes the default state.
// --------------------------------------------------------------------------------------
// Parameters: NONE
// --------------------------------------------------------------------------------------
// Returns: NONE
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
OCR();
// --------------------------------------------------------------------------------------
// Method: Destructor
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: Destructs an existing OCR instance freeing all resources.
// --------------------------------------------------------------------------------------
// Parameters: NONE
// --------------------------------------------------------------------------------------
// Returns: NONE
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
~OCR();
// --------------------------------------------------------------------------------------
// Method: ProcessAll (STRING filename)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process all pages (complete page too... Bound igonred) of the specified
// file and return the resultant text.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - filename = The full path to the file to be OCR processed.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A String containing ALL recognized text.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
String ^ ProcessAll(String ^ filenanme);
// --------------------------------------------------------------------------------------
// Method: Process (STRING filename)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified file and return the resultant text.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - filename = The full path to the file to be OCR processed.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A String containing the recognized text.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
String ^ Process(String ^ filename);
// --------------------------------------------------------------------------------------
// Method: Process (BITMAP image)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified image and return the resultant text.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - image = The image to be OCR processed.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A String containing the recognized text.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/16/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
String ^ Process(Bitmap ^ image);
// --------------------------------------------------------------------------------------
// Method: Extract (STRING filename)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified image file and return the resultant extractions.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - filename = The full path to the file to be OCR processed.
// - type = The type of extract to be returned.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A List of Extracts that were recognized from the image.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/15/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
List<FourCube::Extract ^>^ Extract(String ^ filename, ExtractType type);
// --------------------------------------------------------------------------------------
// Method: Extract (BITMAP image)
// --------------------------------------------------------------------------------------
// Author: David Vaccaro
// Description: OCR process the specified image and return the resultant extractions.
// --------------------------------------------------------------------------------------
// Parameters:
//
// - image = The image to be OCR processed.
// - type = The type of extract to be returned.
//
// --------------------------------------------------------------------------------------
// Returns:
//
// - Success: A List of Extracts that were recognized from the image.
// - Failure: NULL
//
// --------------------------------------------------------------------------------------
// Revision History (latest at the top!)
// --------------------------------------------------------------------------------------
// WHEN | WHO | WHAT
// --------------------------------------------------------------------------------------
// 12/16/2013 | DMV | Initial version.
// --------------------------------------------------------------------------------------
List<FourCube::Extract ^>^ Extract(Bitmap ^ image, ExtractType type);
};
}