1: #if WFA
2: using System;
3: using System.Collections.Generic;
4: using System.Text;
5: using System.Drawing;
6: using System.Threading;
7:
8: namespace Ia.Cl.Model
9: {
10: /// <summary publish="true">
11: /// Handles OCR operations.
12: /// </summary>
13: /// <value>
14: /// See http://code.google.com/p/tesseract-ocr/downloads/list. Include tessnet2.dll and tessdata folder
15: /// </value>
16: /// <remarks>
17: /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
18: ///
19: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
20: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
21: ///
22: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24: ///
25: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
26: ///
27: /// Copyright notice: This notice may not be removed or altered from any source distribution.
28: /// </remarks>
29: public class Ocr
30: {
31: StringBuilder sb;
32: List<tessnet2.Word> result;
33: tessnet2.Tesseract ocr;
34: ManualResetEvent m_event;
35:
36: ////////////////////////////////////////////////////////////////////////////
37:
38: /// <summary>
39: ///
40: /// </summary>
41: public Ocr()
42: {
43: ocr = new tessnet2.Tesseract();
44: ocr.Init("eng", false);
45: }
46:
47: ////////////////////////////////////////////////////////////////////////////
48:
49: /// <summary>
50: ///
51: /// </summary>
52: public void Dispose()
53: {
54: ocr.Dispose();
55: sb.Length = 0;
56: }
57:
58: ////////////////////////////////////////////////////////////////////////////
59:
60: /// <summary>
61: ///
62: /// </summary>
63: public void DumpResult(List<tessnet2.Word> result)
64: {
65: foreach (tessnet2.Word word in result) Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
66: }
67:
68: ////////////////////////////////////////////////////////////////////////////
69:
70: /// <summary>
71: ///
72: /// </summary>
73: public string DoOCRNormal(Bitmap image)
74: {
75: result = ocr.DoOCR(image, Rectangle.Empty);
76:
77: sb = new StringBuilder(result.Count);
78:
79: foreach (tessnet2.Word word in result)
80: {
81: sb.Append(word.Text + " ");
82: }
83:
84: return sb.ToString();
85: }
86:
87: ////////////////////////////////////////////////////////////////////////////
88:
89: /// <summary>
90: ///
91: /// </summary>
92: public List<tessnet2.Word> DoOCRNormal_Word(Bitmap image)
93: {
94: result = ocr.DoOCR(image, Rectangle.Empty);
95:
96: return result;
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: public void DoOCRMultiThred(Bitmap image, string lang)
105: {
106: tessnet2.Tesseract ocr = new tessnet2.Tesseract();
107: ocr.Init(lang, false);
108: // If the OcrDone delegate is not null then this'll be the multithreaded version
109: ocr.OcrDone = new tessnet2.Tesseract.OcrDoneHandler(Finished);
110: // For event to work, must use the multithreaded version
111: ocr.ProgressEvent += new tessnet2.Tesseract.ProgressHandler(ocr_ProgressEvent);
112: m_event = new ManualResetEvent(false);
113: ocr.DoOCR(image, Rectangle.Empty);
114: // Wait here it's finished
115: m_event.WaitOne();
116: }
117:
118: ////////////////////////////////////////////////////////////////////////////
119:
120: /// <summary>
121: ///
122: /// </summary>
123: public void Finished(List<tessnet2.Word> result)
124: {
125: //DumpResult(result);
126: m_event.Set();
127: }
128:
129: ////////////////////////////////////////////////////////////////////////////
130:
131: /// <summary>
132: ///
133: /// </summary>
134: void ocr_ProgressEvent(int percent)
135: {
136: //Console.WriteLine("{0}% progression", percent);
137: }
138:
139: ////////////////////////////////////////////////////////////////////////////
140: ////////////////////////////////////////////////////////////////////////////
141: }
142: }
143: #endif