-
Notifications
You must be signed in to change notification settings - Fork 15
/
ocromni.cpp
209 lines (170 loc) · 5.45 KB
/
ocromni.cpp
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
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, [email protected]
.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <QDataStream>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QImage>
#include <QStringList>
#include "err.h"
#include "ocromni.h"
#include "utils.h"
#ifdef CONFIG_use_omnipage
#include "KernelApi.h"
#define SID 0
#include "../nuance_ocr/sample/sample.h"
Ocromni::Ocromni (void)
{
_engine = OCRE_omnipage;
}
Ocromni::~Ocromni ()
{
}
err_info *Ocromni::init (void)
{
void *lib = dlopen ("libxocr.so", RTLD_LAZY);
// try some other locations, so the user doesn't need LD_LIBRARY_PATH
if (!lib)
{
// first try to find the directory
QStringList dirlist = QString ("/usr/local/lib/,/usr/lib").split (',');
QString fname;
for (int i = 0; i < dirlist.size (); i++)
{
QDir dir (dirlist [i]);
dir.setFilter (QDir::Dirs);
const QString filter = "nuance-omnipage-csdk*";
dir.setNameFilter (filter);
// qDebug () << dir.path () << dir.count ();
QStringList list = dir.entryList ();
foreach (QString path, list)
{
fname = dir.path () + "/" + path + "/libxocr.so";
QFile file (fname);
if (file.exists (fname))
break;
fname.clear ();
}
// qDebug () << list.size ();
}
if (!fname.isEmpty ())
lib = dlopen (fname.latin1 (), RTLD_LAZY);
}
if (!lib)
return err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", dlerror ());
return omni_init ();
}
err_info *Ocromni::checkerr (char *operation, int rc)
{
if (rc != REC_OK)
return err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", QString ("Op '%1', error code %2").arg (operation).arg (rc).latin1 ());
return NULL;
}
err_info *Ocromni::omni_init (void)
{
int brightness = 45; // Automatic conversion mode (default=50)
HPAGE hPage;
IMG_CONVERSION conversion; // Conversion mode
RECERR rc;
err_info *err;
#if (USE_OEM_LICENSE)
rc = kRecSetLicense(LICENSE_PATH, LICENSE_KEY);
if (rc != REC_OK)
{
err = err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", QString ("Error code %1").arg (rc).latin1 ());
kRecQuit();
return err;
}
#endif
rc = kRecInit("SampleCompany", "SampleProduct"); // use your company and product name here
if ((rc != REC_OK) && (rc != API_INIT_WARN))
{
err = err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", QString ("Error code %1").arg (rc).latin1 ());
kRecQuit();
return err;
}
if (rc == API_INIT_WARN)
{
qDebug () << "Module error - For more information, see kRecGetModulesInfo()";
}
rc = kRecSettingSetToDefault(SID, NULL, true);
if (rc != REC_OK)
{
err = err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", QString ("Error code %1").arg (rc).latin1 ());
kRecQuit();
return err;
}
return NULL;
}
err_info *Ocromni::imageToText (QImage &image, QString &text)
{
// unfortunately Omnipage's API has hungarian disease, probably
// an infection from Seattle. It also suffers from Windows
// 'far' disease. Oh dear.
tagIMG_INFO img;
int rc;
HPAGE page;
err_info *err;
img.Size.cx = image.width ();
img.Size.cy = image.height ();
img.DPI.cx = 300;
img.DPI.cy = 300;
img.BytesPerLine = image.bytesPerLine ();
img.IsPalette = 0;
img.BitsPerPixel = image.depth ();
img.DummyS = NULL;
rc = kRecLoadImgM (SID, image.bits (), &img, &page);
if (rc != REC_OK)
{
err = err_make (ERRFN, ERR_ocr_engine_not_present_or_broken2,
"omnipage", QString ("Error code %1").arg (rc).latin1 ());
return err;
}
rc = kRecPreprocessImg(SID, page);
rc = kRecSetCodePage(SID, "Windows ANSI");
rc = kRecRecognize(SID, page, NULL);
rc = kRecSetDTXTFormat(SID, DTXT_TXTF);
// in a bizarre twist, this function doesn't support memory output
char fname [PATH_MAX + 1];
CALL (util_get_tmp (fname));
rc = kRecConvert2DTXT(SID, &page, 1, fname);
QFile file (fname);
if (!file.open (QIODevice::ReadOnly))
return err_make (ERRFN, ERR_cannot_open_file1, fname);
QByteArray ba = file.readAll ();
text = ba.constData ();
file.remove ();
kRecFreeImg (page);
return NULL;
}
#endif