⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocrshell.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************** * File:			ocrshell.cpp * Description:	Code for the OCR side of the OCR API. * Author:		Hewlett-Packard Co * * (C) Copyright 1996, Hewlett-Packard Co. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. * **********************************************************************//********************************************************************** * This file contains code for the OCR side of the HP OCR interface. * The code is designed to be used with either an ANSI C or C++ compiler. * The structures are designed to allow them to be used with any * structure alignment upto 8. **********************************************************************/#include          "mfcpch.h"#include          "ocrshell.h"#include          "tprintf.h"#include          <stdlib.h>#define EXTERN#ifdef __UNIX__EXTERN ESHM_INFO shm;            /*info on shm */#define TICKS       1#endif#ifdef __MSW32__EXTERN ESHM_INFO shm;            /*info on shm */#define TICKS       1000#endif#ifdef __MAC__#if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)#pragma import on#endifextern volatile ESHM_INFO shm;   /*info on shm */extern unsigned short WaitForSingleObject(  /*"C" */                                          volatile Boolean &semaphore,                                          unsigned long timeout);extern unsigned short ReleaseSemaphore(  /*"C" */                                       volatile Boolean &semaphore);#if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)#pragma import reset#endif#define WAIT_OBJECT_0   1#define TICKS       60#endiftypedef enum {  OCS_UNINIT,                    /*uninitialized */  OCS_SETUP_SHM,                 /*shm setup done */  OCS_SETUP_INFO,                /*startinfo sent */  OCS_READING_STRIPS,            /*read first but more to come */  OCS_READ_STRIPS,               /*read all but no monitor yet */  OCS_RECOGNIZING,               /*OCR incomplete */  OCS_SENDING_TEXT,              /*sent buffer but more to come */  OCS_DEAD                       /*disconnected */} OCR_STATE;/* forward declarations - not in .h file as not needed outside this file*/INT16 ocr_internal_shutdown();  /*closedown */INT16 wait_for_mutex();  /*wait for HP to be ready */INT16 wait_for_hp(               /*wait for semaphore */                  INT32 timeout  /*in seconds */                 );INT16 release_mutex();  /*release mutex */INT16 release_ocr();  /*release semaphore */static INT32 font_count = 0;     /*number of fonts */static INT16 lines_read = 0;     /*no read in this image */                                 /*current state */static OCR_STATE ocr_state = OCS_UNINIT;#ifdef __MAC__pascal short TerminateOCR(AppleEvent *theEvent,                          AppleEvent *theReply,                          long refCon) {  ocr_internal_shutdown();   ExitToShell(); }#endif/********************************************************************** * ocr_open_shm * * Attempt to connect to the shared memory segment and semaphores used * in talking to the OCR engine. Called from OCR engine. * The parameters are the command line arguments in order. **********************************************************************/#ifdef __MAC__INT16ocr_open_shm (UINT16 * lang)#elseINT16ocr_open_shm (                   /*open the shm */const char *shm_h,               /*handle of shm */const char *shm_size,            /*size of shm segment */const char *mutex_h,             /*hp mutex */const char *ocr_h,               /*ocr semaphore */const char *hp_h,                /*hp semaphore */const char *lang_str,            /*language */UINT16 * lang                    /*required language */)#endif{  font_count = 0;                /*no fonts yet */  #ifdef __MAC__  if (shm.OCRProcess.lowLongOfPSN && shm.OCRProcess.highLongOfPSN)    return HPERR;  *lang = shm.language;  GetCurrentProcess (&shm.OCRProcess);  if (WakeUpProcess (&shm.IPEProcess))    ExitToShell();   AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,    (AEEventHandlerUPP) TerminateOCR, 0, FALSE);  #else  if (lang != NULL)                                 /*get language */    *lang = (UINT16) strtol (lang_str, NULL, 10);  #endif  if (ocr_state != OCS_UNINIT) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }  #ifdef __MSW32__  shm.shm_size = strtol (shm_size, NULL, 10);                                 /*convert to handle */  shm.shm_hand = (HANDLE) strtol (shm_h, NULL, 10);  shm.shm_mem = MapViewOfFile (shm.shm_hand, FILE_MAP_WRITE, 0, 0, 0);  if (shm.shm_mem == NULL)    return HPERR;                /*failed */                                 /*convert to handle */  shm.mutex = (HANDLE) strtol (mutex_h, NULL, 10);                                 /*convert to handle */  shm.ocr_sem = (HANDLE) strtol (ocr_h, NULL, 10);                                 /*convert to handle */  shm.hp_sem = (HANDLE) strtol (hp_h, NULL, 10);  #endif  ocr_state = OCS_SETUP_SHM;     /*record state */  return OKAY;}/********************************************************************** * ocr_error * * Inform the HP side of an error. * The OCR engine should do any cleanup of its own and exit aferwards. * Uses the current state to determine how to send it and cleanup. **********************************************************************/void ocr_error(                   /*send an error code */               OCR_ERR_CODE code  /*error code */              ) {  ESTRIP_DESC *strip = (ESTRIP_DESC *) shm.shm_mem;  /*strip info */  ETEXT_DESC *monitor = (ETEXT_DESC *) shm.shm_mem;  /*progress monitor */  switch (ocr_state) {    case OCS_UNINIT:             /*uninitialized */    case OCS_DEAD:               /*uninitialized */      return;                    /*can't do anything else */    case OCS_SETUP_SHM:          /*shm setup done */      if (font_count < 1)        font_count = 1;      ocr_setup_startinfo_ansi (-code, LANGE_NONE, "", "");      /*report error */      break;    case OCS_SETUP_INFO:         /*startinfo sent */      if (ocr_get_first_image_strip () == NULL)        break;                   /*disconnected */    case OCS_READING_STRIPS:     /*read first but more to come */      strip->x_size = -code;     /*report error */      release_ocr();  /*send ack */      release_mutex();       break;    case OCS_READ_STRIPS:        /*read all but no monitor yet */      monitor->count = 0;        /*chars in this buffer(-1) */      monitor->progress = 0;     /*percent complete increasing (0-100) */                                 /*text not complete */      monitor->more_to_come = FALSE;      monitor->ocr_alive = TRUE; /*ocr sets to 1, hp 0 */      monitor->err_code = -code; /*report error */      monitor->cancel = FALSE;   /*0=continue, 1=cancel */      release_ocr();  /*send ack */      break;    case OCS_RECOGNIZING:        /*OCR incomplete */    case OCS_SENDING_TEXT:       /*sent buffer but more to come */      monitor->err_code = -code; /*report error */      release_ocr();  /*send ack */  }  ocr_internal_shutdown();  /*get ready for exit */}/********************************************************************** * ocr_append_fontinfo * * Initialize one of the font descriptors. **********************************************************************/INT16 ocr_append_fontinfo(                    /*put info into shm */                          UINT16 language,    /*default language */                          UINT8 font_family,  /*serif/not, fixed/not */                          UINT8 char_set,     /*character set standard */                          UINT8 pitch,        /*fixed or prop */                          const char *name    /*plain ascii name */                         ) {  EOCR_DESC *desc;               /*ocr engine info */  int index;                     /*char index */  INT32 font_index;              /*which font */  if (ocr_state != OCS_SETUP_SHM) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }                                 /*turn to right type */  desc = (EOCR_DESC *) shm.shm_mem;  if (font_count >    (INT32) ((shm.shm_size - sizeof (EOCR_DESC)) / sizeof (EFONT_DESC)))    return OCR_API_NO_MEM;       /*insufficient space */  font_index = font_count++;     /*add a font */                                 /*setup structure */  desc->fonts[font_index].language = language;                                 /*setup structure */  desc->fonts[font_index].font_family = font_family;                                 /*setup structure */  desc->fonts[font_index].char_set = char_set;                                 /*setup structure */  desc->fonts[font_index].pitch = pitch;  if (name != NULL) {    for (index = 0; index < MAX_FONT_NAME && name[index] != 0; index++)      desc->fonts[font_index].name[index] = name[index];  }  else    index = 0;  desc->fonts[font_index].name[index] = 0;  return OKAY;}/********************************************************************** * ocr_setup_startinfo * * Setup the info on the OCR engine. Uses 16 bit chars to name the * engine. **********************************************************************/INT16 ocr_setup_startinfo(                       /*put info into shm */                          INT32 protocol,        /*interface version */                          UINT16 language,       /*default language */                          const UINT16 *name,    /*name of engine */                          const UINT16 *version  /*version of engine */                         ) {  EOCR_DESC *desc;               /*ocr engine info */  int index;                     /*char index */  INT16 result;                  /*from open */  if (ocr_state != OCS_SETUP_SHM || font_count < 1) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }                                 /*turn to right type */  desc = (EOCR_DESC *) shm.shm_mem;  desc->protocol = protocol;     /*setup structure */  desc->font_count = font_count;  desc->language = language;  for (index = 0; index < MAX_OCR_NAME && name[index] != 0; index++)    desc->name[index] = name[index];  desc->name[index] = 0;  for (index = 0; index < MAX_OCR_VERSION && version[index] != 0; index++)    desc->version[index] = version[index];  desc->version[index] = 0;  result = release_ocr ();  if (result != OKAY)    return result;  ocr_state = OCS_SETUP_INFO;    /*record state */  return OKAY;}/********************************************************************** * ocr_setup_startinfo_ansi * * Setup the info on the OCR engine. Uses 8 bit chars to name the * engine. **********************************************************************/INT16 ocr_setup_startinfo_ansi(                     /*put info into shm */                               UINT32 protocol,     /*interface version */                               UINT16 language,     /*default language */                               const char *name,    /*name of engine */                               const char *version  /*version of engine */                              ) {  EOCR_DESC *desc;               /*ocr engine info */  int index;                     /*char index */  INT16 result;                  /*from open */  if (ocr_state != OCS_SETUP_SHM || font_count < 1) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }                                 /*turn to right type */  desc = (EOCR_DESC *) shm.shm_mem;  desc->protocol = protocol;     /*setup structure */  desc->font_count = font_count;  desc->language = language;  for (index = 0; index < MAX_OCR_NAME && name[index] != 0; index++)    desc->name[index] = name[index];  desc->name[index] = 0;  for (index = 0; index < MAX_OCR_VERSION && version[index] != 0; index++)    desc->version[index] = version[index];  desc->version[index] = 0;  result = release_ocr ();  if (result != OKAY)    return result;  ocr_state = OCS_SETUP_INFO;    /*record state */  return OKAY;}/********************************************************************** * ocr_get_first_image_strip * * Wait for the master to send the first image strip and return a * pointer to it. The result is NULL if it is time to exit. **********************************************************************/ESTRIP_DESC *ocr_get_first_image_strip() {  /*get image strip */  ESTRIP_DESC *strip;            /*strip info */  INT16 result;                  /*of wait/release */  if (ocr_state != OCS_SETUP_INFO) {    tprintf ("Bad state reading strip");    ocr_error(OCR_ERR_BAD_STATE);     return NULL;                 /*incorrect state */  }                                 /*strip info */  strip = (ESTRIP_DESC *) shm.shm_mem;  lines_read = 0;  result = wait_for_mutex ();  if (result != OKAY) {    tprintf ("Mutax wait failed reading strip");    return NULL;                 /*HP dead */  }  result = release_mutex ();  if (result != OKAY) {    tprintf ("Mutax release failed reading strip");    return NULL;                 /*HP dead */  }  result = wait_for_hp (READIM_TIMEOUT);  if (result != OKAY) {    tprintf ("Wait for HP failed reading strip");    return NULL;                 /*HP dead */  }  lines_read = strip->strip_size;/*lines read so far */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -