GlobeEngine
asprise_ocr_api.h
Go to the documentation of this file.
1 #ifndef ASPRISE_OCR_API_H
2 #define ASPRISE_OCR_API_H
3 
18 #ifdef _WIN32
19 #define EXPORT_API __declspec(dllexport)
20 #define CALL_CONVENTION __stdcall
21 #include <Windows.h>
22 #else
23 #define EXPORT_API
24 #define CALL_CONVENTION
25 #include <stdio.h>
26 #include <string.h>
27 #include <dlfcn.h>
28 #endif
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define OCR_SPEED_FASTEST "fastest"
35 #define OCR_SPEED_FAST "fast"
36 #define OCR_SPEED_SLOW "slow"
37 
38 #define OCR_RECOGNIZE_TYPE_TEXT "text"
39 #define OCR_RECOGNIZE_TYPE_BARCODE "barcode"
40 #define OCR_RECOGNIZE_TYPE_ALL "all"
41 
42 #define OCR_OUTPUT_FORMAT_PLAINTEXT "text"
43 #define OCR_OUTPUT_FORMAT_XML "xml"
44 #define OCR_OUTPUT_FORMAT_PDF "pdf"
45 #define OCR_OUTPUT_FORMAT_RTF "rtf"
46 
47 //#define OCR_PROP_INCLUDE_EMPTY_BLOCK "PROP_INCLUDE_EMPTY_BLOCK"
48 
49 #define OCR_PROP_LIMIT_TO_CHARSET "PROP_LIMIT_TO_CHARSET"
50 
51 #define OCR_PROP_PAGE_TYPE "PROP_PAGE_TYPE"
52 #define OCR_PROP_PAGE_TYPE_AUTO_DETECT "auto"
53 #define OCR_PROP_PAGE_TYPE_SINGLE_BLOCK "single_block"
54 #define OCR_PROP_PAGE_TYPE_SINGLE_COLUMN "single_column"
55 #define OCR_PROP_PAGE_TYPE_SINGLE_LINE "single_line"
56 #define OCR_PROP_PAGE_TYPE_SINGLE_WORD "single_word"
57 #define OCR_PROP_PAGE_TYPE_SINGLE_CHARACTOR "single_char"
58 #define OCR_PROP_PAGE_TYPE_SCATTERED "scattered"
59 
60 #define OCR_PROP_OUTPUT_SEPARATE_WORDS "PROP_OUTPUT_SEPARATE_WORDS"
61 
63 #define OCR_PROP_INPUT_PDF_DPI "PROP_INPUT_PDF_DPI"
64 
65 // -------- Dictionary related ---------
67 #define OCR_START_PROP_DICT_SKIP_BUILT_IN_DEFAULT "START_PROP_DICT_SKIP_BUILT_IN_DEFAULT"
68 
69 #define OCR_START_PROP_DICT_SKIP_BUILT_IN_ALL "START_PROP_DICT_SKIP_BUILT_IN_ALL"
70 
71 #define OCR_START_PROP_DICT_CUSTOM_DICT_FILE "START_PROP_DICT_CUSTOM_DICT_FILE"
72 
73 #define OCR_START_PROP_DICT_CUSTOM_TEMPLATES_FILE "START_PROP_DICT_CUSTOM_TEMPLATES_FILE"
74 
76 #define OCR_PROP_DICT_DICT_IMPORTANCE "PROP_DICT_DICT_IMPORTANCE"
77 
78 // -------- PDF output related ---------
79 
80 #define OCR_PROP_PDF_OUTPUT_FILE "PROP_PDF_OUTPUT_FILE"
81 #define OCR_PROP_PDF_OUTPUT_IMAGE_DPI "PROP_PDF_OUTPUT_IMAGE_DPI"
82 #define OCR_PROP_PDF_OUTPUT_FONT "PROP_PDF_OUTPUT_FONT"
83 #define OCR_PROP_PDF_OUTPUT_TEXT_VISIBLE "PROP_PDF_OUTPUT_TEXT_VISIBLE"
84 #define OCR_PROP_PDF_OUTPUT_IMAGE_FORCE_BW "PROP_PDF_OUTPUT_IMAGE_FORCE_BW"
85 
86 #define OCR_PROP_PDF_OUTPUT_PDFA "PROP_PDF_OUTPUT_PDFA"
87 #define OCR_PROP_PDF_OUTPUT_PDFA_FONT_FILE "PROP_PDF_OUTPUT_PDFA_FONT_FILE"
88 
90 #define OCR_PROP_PDF_OUTPUT_CONF_THRESHOLD "PROP_PDF_OUTPUT_CONF_THRESHOLD"
91 
93 #define OCR_PROP_PDF_OUTPUT_RETURN_TEXT "PROP_PDF_OUTPUT_RETURN_TEXT"
94 
95 // -------- RTF output related ---------
96 
97 #define OCR_PROP_RTF_OUTPUT_FILE "PROP_RTF_OUTPUT_FILE"
98 
100 #define OCR_PROP_RTF_OUTPUT_RETURN_TEXT "PROP_RTF_OUTPUT_RETURN_TEXT"
101 
103 #define OCR_PROP_RTF_PAPER_SIZE "PROP_RTF_PAPER_SIZE"
104 
106 #define OCR_PROP_RTF_SHOW_TABLE_CELL_BORDER "PROP_RTF_SHOW_TABLE_CELL_BORDER"
107 
108 // -------- image processing related ---------
109 
111 #define OCR_PROP_IMG_PREPROCESS_TYPE "PROP_IMG_PREPROCESS_TYPE"
112 #define OCR_PROP_IMG_PREPROCESS_TYPE_DEFAULT "default"
113 #define OCR_PROP_IMG_PREPROCESS_TYPE_DEFAULT_WITH_ORIENTATION_DETECTION "default_with_orientation_detection"
114 #define OCR_PROP_IMG_PREPROCESS_TYPE_CUSTOM "custom"
115 
117 #define OCR_PROP_IMG_PREPROCESS_CUSTOM_CMDS "PROP_IMG_PREPROCESS_CUSTOM_CMDS"
118 
119 // -------- table related ---------
121 #define OCR_PROP_TABLE_SKIP_DETECTION "PROP_TABLE_SKIP_DETECTION"
122 
124 #define OCR_PROP_TABLE_MIN_SIDE_LENGTH "PROP_TABLE_MIN_SIDE_LENGTH"
125 
127 #define OCR_PROP_SAVE_INTERMEDIATE_IMAGES_TO_DIR "PROP_SAVE_INTERMEDIATE_IMAGES_TO_DIR"
128 
130 #define OCR_PROP_INPUT_PDF_SINGLE_IMG_PER_PAGE "PROP_INPUT_PDF_SINGLE_IMG_PER_PAGE"
131 
132 // ------------------------- Functions exposed -------------------------
135 
138 
141 
143 EXPORT_API long long CALL_CONVENTION com_asprise_ocr_start(const char * lang, const char * speed, const char * propSpec, const char * propSeparator, const char * propKeyValueSpeparator);
144 
147 (long long handle, const char * imgFiles, int pageIndex, int startX, int startY, int width, int height, const char * recognizeType, const char * outputFormat, const char * propSpec, const char * propSeparator, const char * propKeyValueSpeparator);
148 
150 EXPORT_API void CALL_CONVENTION com_asprise_ocr_stop(long long handle);
151 
153 EXPORT_API void CALL_CONVENTION com_asprise_ocr_input_license(const char * licenseeName, const char * licenseCode);
154 
156 EXPORT_API void CALL_CONVENTION com_asprise_ocr_util_delete(long long handle, bool isArray);
157 
158 // pyd
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 // ------------------------- Run-time Dynamic Linking -------------------------
168 #ifndef SKIP_AOCR_DYNAMIC
169 typedef void(*FUNC_TYPE)(void *);
170 #ifdef _WIN32
171  typedef HMODULE LIBRARY_HANDLE;
172  #define snprintf _snprintf
173 #else
174  typedef void * LIBRARY_HANDLE;
175 #endif
176 
177 #if defined(_WIN32) && !defined(_WIN64)
178 #define FUNC_NAME_com_asprise_ocr_version "_com_asprise_ocr_version@0"
179 #define FUNC_NAME_com_asprise_ocr_setup "_com_asprise_ocr_setup@4"
180 #define FUNC_NAME_com_asprise_ocr_list_supported_langs "_com_asprise_ocr_list_supported_langs@0"
181 #define FUNC_NAME_com_asprise_ocr_start "_com_asprise_ocr_start@20"
182 #define FUNC_NAME_com_asprise_ocr_stop "_com_asprise_ocr_stop@8"
183 #define FUNC_NAME_com_asprise_ocr_recognize "_com_asprise_ocr_recognize@52"
184 #define FUNC_NAME_com_asprise_ocr_util_delete "_com_asprise_ocr_util_delete@12"
185 #define FUNC_NAME_com_asprise_ocr_input_license "_com_asprise_ocr_input_license@8"
186 #else
187 #define FUNC_NAME_com_asprise_ocr_version "com_asprise_ocr_version"
188 #define FUNC_NAME_com_asprise_ocr_setup "com_asprise_ocr_setup"
189 #define FUNC_NAME_com_asprise_ocr_list_supported_langs "com_asprise_ocr_list_supported_langs"
190 #define FUNC_NAME_com_asprise_ocr_start "com_asprise_ocr_start"
191 #define FUNC_NAME_com_asprise_ocr_stop "com_asprise_ocr_stop"
192 #define FUNC_NAME_com_asprise_ocr_recognize "com_asprise_ocr_recognize"
193 #define FUNC_NAME_com_asprise_ocr_util_delete "com_asprise_ocr_util_delete"
194 #define FUNC_NAME_com_asprise_ocr_input_license "com_asprise_ocr_input_license"
195 #endif
196 
198  typedef int(CALL_CONVENTION * func_type_com_asprise_ocr_setup) (bool);
200  typedef long long(CALL_CONVENTION * func_type_com_asprise_ocr_start) (const char *, const char *, const char *, const char *, const char *);
201  typedef void(CALL_CONVENTION * func_type_com_asprise_ocr_stop) (long long);
202  typedef char * (CALL_CONVENTION * func_type_com_asprise_ocr_recognize) (long long, const char *, int, int, int, int, int, const char *, const char *, const char *, const char *, const char *);
203  typedef void(CALL_CONVENTION * func_type_com_asprise_ocr_util_delete) (long long, bool);
204  typedef void(CALL_CONVENTION * func_type_com_asprise_ocr_input_license) (const char *, const char *);
205 
206  func_type_com_asprise_ocr_input_license func_com_asprise_ocr_input_license = NULL;
209  func_type_com_asprise_ocr_setup func_com_asprise_ocr_setup = NULL;
210  func_type_com_asprise_ocr_start func_com_asprise_ocr_start = NULL;
211  func_type_com_asprise_ocr_stop func_com_asprise_ocr_stop = NULL;
212  func_type_com_asprise_ocr_util_delete func_com_asprise_ocr_util_delete = NULL;
214 
215  bool checkIfFuncFound(bool funcFound, const char * funcName) {
216  if (funcFound) {
217  return true;
218  } else {
219  printf("ERROR: Failed to look up function in the library: %s\n", funcName);
220  return false;
221  }
222  }
223 
225  LIBRARY_HANDLE dynamic_load_aocr_library(const char * pathToFolderContainingAocrLibrary) {
226  LIBRARY_HANDLE handle = NULL;
227  char libFilePath[256];
228  bool isWindows = false;
229  #ifdef WIN32
230  #ifdef _WIN64
231  const char * libName = "aocr_x64.dll";
232  #else
233  const char * libName = "aocr.dll";
234  #endif
235  isWindows = true;
236  #else
237  #ifdef __APPLE__
238  const char * libName = "libaocr_x64.dylib";
239  #else
240  #if defined(__LP64__) || defined(_LP64)
241  const char * libName = "libaocr_x64.so";
242  #else
243  const char * libName = "libaocr.so";
244  #endif
245  #endif
246  isWindows = false;
247  #endif
248 
249  const char * separator = strlen(pathToFolderContainingAocrLibrary) > 0 && (pathToFolderContainingAocrLibrary[strlen(pathToFolderContainingAocrLibrary) - 1] == '/' || pathToFolderContainingAocrLibrary[strlen(pathToFolderContainingAocrLibrary) - 1] == '\\')
250  ? "" : "/";
251  snprintf(libFilePath, 256, "%s%s%s", pathToFolderContainingAocrLibrary, separator, libName);
252  for (size_t i = 0; i < strlen(libFilePath); i++) { // correct slash directions
253  if (libFilePath[i] == '/' || libFilePath[i] == '\\') {
254  libFilePath[i] = isWindows ? '\\' : '/';
255  }
256  }
257 
258  #ifdef WIN32
259  handle = LoadLibraryA(libFilePath);
260  if(handle == NULL) {
261  printf("ERROR: GetLastError() returns: %lu\n", GetLastError());
262  return NULL;
263  }
264  func_com_asprise_ocr_input_license = (func_type_com_asprise_ocr_input_license) GetProcAddress(handle, FUNC_NAME_com_asprise_ocr_input_license);
267  func_com_asprise_ocr_setup = (func_type_com_asprise_ocr_setup)GetProcAddress(handle, FUNC_NAME_com_asprise_ocr_setup);
268  func_com_asprise_ocr_start = (func_type_com_asprise_ocr_start)GetProcAddress(handle, FUNC_NAME_com_asprise_ocr_start);
269  func_com_asprise_ocr_stop = (func_type_com_asprise_ocr_stop)GetProcAddress(handle, FUNC_NAME_com_asprise_ocr_stop);
270  func_com_asprise_ocr_util_delete = (func_type_com_asprise_ocr_util_delete)GetProcAddress(handle, FUNC_NAME_com_asprise_ocr_util_delete);
272  #else
273  handle = dlopen(libFilePath, RTLD_LAZY);
274  if (handle == NULL) {
275  printf("ERROR: dlerror(): %s\n", dlerror());
276  return NULL;
277  }
278  func_com_asprise_ocr_input_license = (func_type_com_asprise_ocr_input_license)dlsym(handle, FUNC_NAME_com_asprise_ocr_input_license);
281  func_com_asprise_ocr_setup = (func_type_com_asprise_ocr_setup)dlsym(handle, FUNC_NAME_com_asprise_ocr_setup);
282  func_com_asprise_ocr_start = (func_type_com_asprise_ocr_start)dlsym(handle, FUNC_NAME_com_asprise_ocr_start);
283  func_com_asprise_ocr_stop = (func_type_com_asprise_ocr_stop)dlsym(handle, FUNC_NAME_com_asprise_ocr_stop);
284  func_com_asprise_ocr_util_delete = (func_type_com_asprise_ocr_util_delete)dlsym(handle, FUNC_NAME_com_asprise_ocr_util_delete);
286  #endif
287  if (handle == NULL) {
288  printf("ERROR: failed to dynamically load the library: %s\nMake sure it exists. Contact support@asprise.com if you need any assistance.\n", libFilePath);
289  }
290 
299  // all found
300  }
301  else {
302  printf("ERROR: at lease one function is not found in the library.\n");
303  return NULL;
304  }
305 
306  return handle;
307  }
308 
311  int errorCode = 0;
312  if (handle == NULL) {
313  return false;
314  }
315 #ifdef WIN32
316  return FreeLibrary(handle) != 0;
317 #else
318  errorCode = dlclose(handle);
319  if(errorCode != 0) {
320  printf("dlclose() returns error code: %d\n", errorCode);
321  }
322  return errorCode == 0;
323 #endif
324  }
325 
326  bool assertFuncExists(bool exists) {
327  if (exists) {
328  return true;
329  } else {
330  printf("Function is NULL. Please call dynamic_load_aocr_library first.\n");
331  return false;
332  }
333  }
334 
336  const char * c_com_asprise_ocr_version() {
338  return NULL;
339  }
341  }
342 
346  return 0;
347  }
348  return func_com_asprise_ocr_setup(queryOnly);
349  }
350 
354  return NULL;
355  }
357  }
358 
360  long long c_com_asprise_ocr_start(const char * lang, const char * speed, const char * propSpec, const char * propSeparator, const char * propKeyValueSpeparator) {
362  return 0;
363  }
364  return func_com_asprise_ocr_start(lang, speed, propSpec, propSeparator, propKeyValueSpeparator);
365  }
366 
369  (long long handle, const char * imgFiles, int pageIndex, int startX, int startY, int width, int height, const char * recognizeType, const char * outputFormat, const char * propSpec, const char * propSeparator, const char * propKeyValueSpeparator) {
371  return NULL;
372  }
373  return func_com_asprise_ocr_recognize(handle, imgFiles, pageIndex, startX, startY, width, height, recognizeType, outputFormat, propSpec, propSeparator, propKeyValueSpeparator);
374  }
375 
377  void c_com_asprise_ocr_stop(long long handle) {
379  return;
380  }
382  }
383 
385  void c_com_asprise_ocr_input_license(const char * licenseeName, const char * licenseCode) {
387  return;
388  }
389  func_com_asprise_ocr_input_license(licenseeName, licenseCode);
390  }
391 
393  void c_com_asprise_ocr_util_delete(long long handle, bool isArray) {
395  return;
396  }
397  func_com_asprise_ocr_util_delete(handle, isArray);
398  }
399 
458 #endif // #ifndef SKIP_AOCR_DYNAMIC
459 
460 #endif // ASPRISE_OCR_API_H
int CALL_CONVENTION c_com_asprise_ocr_setup(bool queryOnly)
Definition: asprise_ocr_api.h:344
void c_com_asprise_ocr_input_license(const char *licenseeName, const char *licenseCode)
Definition: asprise_ocr_api.h:385
EXPORT_API void CALL_CONVENTION com_asprise_ocr_util_delete(long long handle, bool isArray)
EXPORT_API int CALL_CONVENTION com_asprise_ocr_setup(bool queryOnly)
func_type_com_asprise_ocr_util_delete func_com_asprise_ocr_util_delete
Definition: asprise_ocr_api.h:212
EXPORT_API long long CALL_CONVENTION com_asprise_ocr_start(const char *lang, const char *speed, const char *propSpec, const char *propSeparator, const char *propKeyValueSpeparator)
#define FUNC_NAME_com_asprise_ocr_setup
Definition: asprise_ocr_api.h:188
func_type_com_asprise_ocr_start func_com_asprise_ocr_start
Definition: asprise_ocr_api.h:210
#define FUNC_NAME_com_asprise_ocr_start
Definition: asprise_ocr_api.h:190
func_type_com_asprise_ocr_stop func_com_asprise_ocr_stop
Definition: asprise_ocr_api.h:211
typedef int(CALL_CONVENTION *func_type_com_asprise_ocr_setup)(bool)
EXPORT_API char *CALL_CONVENTION com_asprise_ocr_list_supported_langs()
#define FUNC_NAME_com_asprise_ocr_util_delete
Definition: asprise_ocr_api.h:193
const char *func_type_com_asprise_ocr_input_license func_com_asprise_ocr_input_license
Definition: asprise_ocr_api.h:206
EXPORT_API char *CALL_CONVENTION com_asprise_ocr_recognize(long long handle, const char *imgFiles, int pageIndex, int startX, int startY, int width, int height, const char *recognizeType, const char *outputFormat, const char *propSpec, const char *propSeparator, const char *propKeyValueSpeparator)
long const char const char const char const char *typedef void(CALL_CONVENTION *func_type_com_asprise_ocr_stop)(long long)
Definition: asprise_ocr_api.h:201
func_type_com_asprise_ocr_version func_com_asprise_ocr_version
Definition: asprise_ocr_api.h:213
#define FUNC_NAME_com_asprise_ocr_version
Definition: asprise_ocr_api.h:187
void c_com_asprise_ocr_util_delete(long long handle, bool isArray)
Definition: asprise_ocr_api.h:393
#define FUNC_NAME_com_asprise_ocr_input_license
Definition: asprise_ocr_api.h:194
char *CALL_CONVENTION * func_type_com_asprise_ocr_list_supported_langs()
Definition: asprise_ocr_api.h:199
EXPORT_API void CALL_CONVENTION initaocr_x64(void)
EXPORT_API const char *CALL_CONVENTION com_asprise_ocr_version()
char *CALL_CONVENTION * func_type_com_asprise_ocr_recognize(long long, const char *, int, int, int, int, int, const char *, const char *, const char *, const char *, const char *)
Definition: asprise_ocr_api.h:202
EXPORT_API void CALL_CONVENTION com_asprise_ocr_stop(long long handle)
#define CALL_CONVENTION
Definition: asprise_ocr_api.h:24
char * c_com_asprise_ocr_list_supported_langs()
Definition: asprise_ocr_api.h:352
#define EXPORT_API
Definition: asprise_ocr_api.h:23
#define FUNC_NAME_com_asprise_ocr_list_supported_langs
Definition: asprise_ocr_api.h:189
void c_com_asprise_ocr_stop(long long handle)
Definition: asprise_ocr_api.h:377
const char *CALL_CONVENTION * func_type_com_asprise_ocr_version()
Definition: asprise_ocr_api.h:197
typedef bool
Definition: asprise_ocr_api.h:203
char * c_com_asprise_ocr_recognize(long long handle, const char *imgFiles, int pageIndex, int startX, int startY, int width, int height, const char *recognizeType, const char *outputFormat, const char *propSpec, const char *propSeparator, const char *propKeyValueSpeparator)
Definition: asprise_ocr_api.h:369
EXPORT_API void CALL_CONVENTION com_asprise_ocr_input_license(const char *licenseeName, const char *licenseCode)
func_type_com_asprise_ocr_setup func_com_asprise_ocr_setup
Definition: asprise_ocr_api.h:209
EXPORT_API void CALL_CONVENTION initaocr(void)
bool assertFuncExists(bool exists)
Definition: asprise_ocr_api.h:326
void * LIBRARY_HANDLE
Definition: asprise_ocr_api.h:174
const char * c_com_asprise_ocr_version()
Definition: asprise_ocr_api.h:336
void(* FUNC_TYPE)(void *)
Definition: asprise_ocr_api.h:169
bool dynamic_unload_aocr_library(LIBRARY_HANDLE handle)
Definition: asprise_ocr_api.h:310
func_type_com_asprise_ocr_recognize func_com_asprise_ocr_recognize
Definition: asprise_ocr_api.h:208
#define FUNC_NAME_com_asprise_ocr_stop
Definition: asprise_ocr_api.h:191
bool checkIfFuncFound(bool funcFound, const char *funcName)
Definition: asprise_ocr_api.h:215
LIBRARY_HANDLE dynamic_load_aocr_library(const char *pathToFolderContainingAocrLibrary)
Definition: asprise_ocr_api.h:225
long long(CALL_CONVENTION *func_type_com_asprise_ocr_start)(const char *
Definition: asprise_ocr_api.h:200
#define FUNC_NAME_com_asprise_ocr_recognize
Definition: asprise_ocr_api.h:192
long long c_com_asprise_ocr_start(const char *lang, const char *speed, const char *propSpec, const char *propSeparator, const char *propKeyValueSpeparator)
Definition: asprise_ocr_api.h:360
func_type_com_asprise_ocr_list_supported_langs func_com_asprise_ocr_list_supported_langs
Definition: asprise_ocr_api.h:207