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

📄 ocrshell.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  if (lines_read < strip->y_size)                                 /*record state */      ocr_state = OCS_READING_STRIPS;  else    ocr_state = OCS_READ_STRIPS;  if (strip->x_size == 0 || strip->y_size == 0)    return NULL;                 /*end of job */  return strip;}/********************************************************************** * ocr_get_next_image_strip * * Wait for the master to send the next image strip and return a * pointer to it. The result is NULL if it is time to exit. **********************************************************************/ESTRIP_DESC *ocr_get_next_image_strip() {  /*get image strip */  ESTRIP_DESC *strip;            /*strip info */  INT16 result;                  /*of wait/release */  if (ocr_state != OCS_READING_STRIPS) {    ocr_error(OCR_ERR_BAD_STATE);     return NULL;                 /*incorrect state */  }                                 /*strip info */  strip = (ESTRIP_DESC *) shm.shm_mem;  result = release_ocr ();  if (result != OKAY)    return NULL;                 /*HP dead */  result = wait_for_hp (READIM_TIMEOUT);  if (result != OKAY)    return NULL;                 /*HP dead */                                 /*lines read so far */  lines_read += strip->strip_size;  if (lines_read < strip->y_size)                                 /*record state */      ocr_state = OCS_READING_STRIPS;  else    ocr_state = OCS_READ_STRIPS;  return strip;}/********************************************************************** * ocr_setup_monitor * * Setup the progress monitor. Call before starting the recognize task. **********************************************************************/ETEXT_DESC *ocr_setup_monitor() {  /*setup monitor */  ETEXT_DESC *monitor;           /*progress monitor */                                 /*text info */  monitor = (ETEXT_DESC *) shm.shm_mem;  monitor->count = 0;            /*chars in this buffer(-1) */  monitor->progress = 0;         /*percent complete increasing (0-100) */  monitor->more_to_come = TRUE;  /*text not complete */  monitor->ocr_alive = TRUE;     /*ocr sets to 1, hp 0 */  monitor->err_code = 0;         /*used by ocr_error */  monitor->cancel = FALSE;       /*0=continue, 1=cancel */  if (release_ocr () != OKAY)    return NULL;                 /*release failed */  ocr_state = OCS_RECOGNIZING;   /*record state */  return monitor;}/********************************************************************** * ocr_char_space * * Return the number of chars that can be fitted into the buffer. **********************************************************************/INT32 ocr_char_space() {  /*put char into shm */  ETEXT_DESC *buf;               /*text buffer */  int result;                                 /*progress info */  buf = (ETEXT_DESC *) shm.shm_mem;  result =    (shm.shm_size - sizeof (ETEXT_DESC)) / sizeof (EANYCODE_CHAR) -    buf->count + 1;  //      while (buf->hp_alive==-1)  //              Sleep(50);                                                                              /*wait for HP*/  return result;}/********************************************************************** * ocr_append_char * * Add a character to the output. Returns OKAY if successful, OCR_API_NO_MEM * if there was insufficient room in the buffer. **********************************************************************/INT16 ocr_append_char(                              /*put char into shm */                      UINT16 char_code,             /*character itself */                      INT16 left,                   /*of char (-1) */                      INT16 right,                  /*of char (-1) */                      INT16 top,                    /*of char (-1) */                      INT16 bottom,                 /*of char (-1) */                      INT16 font_index,             /*what font (-1) */                      UINT8 confidence,             /*0=perfect, 100=reject (0/100) */                      UINT8 point_size,             /*of char, 72=i inch, (10) */                      INT8 blanks,                  /*no of spaces before this char (1) */                      UINT8 enhancement,            /*char enhancement (0) */                      OCR_CHAR_DIRECTION text_dir,  /*rendering direction (OCR_CDIR_RIGHT_LEFT) */                      OCR_LINE_DIRECTION line_dir,  /*line rendering direction (OCR_LDIR_DOWN_RIGHT) */                      OCR_NEWLINE_TYPE nl_type      /*type of newline (if any) (OCR_NL_NONE) */                     ) {  ETEXT_DESC *buf;               /*text buffer */  int index;                     /*char index */  INT16 result;                  /*of callback */  if (ocr_state != OCS_RECOGNIZING && ocr_state != OCS_SENDING_TEXT) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }  if (char_code == ' ' || char_code == '\n' || char_code == '\r'    || char_code == '\t')    return OCR_API_BAD_CHAR;     /*illegal char */                                 /*progress info */  buf = (ETEXT_DESC *) shm.shm_mem;  result =    (shm.shm_size - sizeof (ETEXT_DESC)) / sizeof (EANYCODE_CHAR) -    buf->count;  if (result < 1)    return OCR_API_NO_MEM;       /*insufficient room */  index = buf->count++;          /*count of chars */                                 /*setup structure */  buf->text[index].char_code = char_code;  buf->text[index].left = left;  /*setup structure */  buf->text[index].right = right;/*setup structure */  buf->text[index].top = top;    /*setup structure */                                 /*setup structure */  buf->text[index].bottom = bottom;                                 /*setup structure */  buf->text[index].font_index = font_index;                                 /*setup structure */  buf->text[index].confidence = confidence;                                 /*setup structure */  buf->text[index].point_size = point_size;                                 /*setup structure */  buf->text[index].blanks = blanks;  if (nl_type == OCR_NL_NONE) {    if (text_dir == OCR_CDIR_TOP_BOTTOM || text_dir == OCR_CDIR_BOTTOM_TOP)      buf->text[index].formatting = (text_dir << 5) | 128;    /*setup structure */    else                                 /*setup structure */      buf->text[index].formatting = text_dir << 5;  }  else {    buf->text[index].formatting = (nl_type << 6) | (line_dir << 5);    /*setup structure */  }  buf->text[index].formatting |= enhancement & (~EUC_FORMAT_MASK);  return OKAY;}/********************************************************************** * ocr_send_text * * Send the text to the host and wait for the ack. * Use this function after a sequence of ocr_append_char calls to * actually sent the text to the master process. * Set more to come TRUE if there is more text in this page, FALSE * if the OCR engine is now ready to receive another image. **********************************************************************/INT16 ocr_send_text(                    /*send shm */                    BOOL8 more_to_come  /*any text left */                   ) {  ETEXT_DESC *buf;               /*text buffer */  if (ocr_state != OCS_RECOGNIZING && ocr_state != OCS_SENDING_TEXT) {    ocr_error(OCR_ERR_BAD_STATE);     return OCR_API_BAD_STATE;    /*incorrect state */  }                                 /*progress info */  buf = (ETEXT_DESC *) shm.shm_mem;                                 /*setup structure */  buf->more_to_come = more_to_come;  if (more_to_come) {    if ((buf->text[buf->count - 1].formatting >> 6) != OCR_NL_NEWLINE    && (buf->text[buf->count - 1].formatting >> 6) != OCR_NL_NEWPARA) {                                 /*force line end */      buf->text[buf->count - 1].formatting &= 63;      buf->text[buf->count - 1].formatting |= OCR_NL_NEWLINE << 6;    }  }  else {    if (buf->count < 1)      ocr_append_char ('~', -1, -1, -1, -1, 0, 100, 10, 0,        0, OCR_CDIR_RIGHT_LEFT, OCR_LDIR_DOWN_RIGHT,        OCR_NL_NEWPARA);    /*dummy character */    else if ((buf->text[buf->count - 1].formatting >> 6) != OCR_NL_NEWPARA) {                                 /*force para end */      buf->text[buf->count - 1].formatting &= 63;      buf->text[buf->count - 1].formatting |= OCR_NL_NEWPARA << 6;    }  }  if (release_ocr () != OKAY)    return HPERR;                /*release failed */  if (wait_for_hp (READTEXT_TIMEOUT) != OKAY)    return HPERR;  if (more_to_come) {    buf->count = 0;              /*setup structure */    ocr_state = OCS_SENDING_TEXT;/*record state */  }  else    ocr_state = OCS_SETUP_INFO;  /*record state */  return OKAY;}/********************************************************************** * ocr_shutdown * * Closedown communications with the HP side and free up handles. **********************************************************************/INT16 ocr_shutdown() {  /*closedown */  #ifdef __MAC__  shm.OCRProcess.lowLongOfPSN = kNoProcess;  shm.OCRProcess.highLongOfPSN = 0;  #endif  ocr_error(OCR_ERR_CLEAN_EXIT);  /*signal exit */  return OKAY;}/********************************************************************** * ocr_internal_shutdown * * Free up handles or whatever to clean up without attempting to communicate. **********************************************************************/INT16 ocr_internal_shutdown() {  /*closedown */  ocr_state = OCS_DEAD;          /*record state */  #ifdef __MSW32__  if (shm.shm_mem != NULL) {    UnmapViewOfFile (shm.shm_mem);    CloseHandle (shm.shm_hand);  /*no longer used */    CloseHandle (shm.mutex);     /*release handles */    CloseHandle (shm.ocr_sem);    CloseHandle (shm.hp_sem);    shm.shm_mem = NULL;  }  #elif defined (__MAC__)  shm.OCRProcess.lowLongOfPSN = kNoProcess;  shm.OCRProcess.highLongOfPSN = 0;  #endif  return OKAY;}/********************************************************************** * wait_for_mutex * * Wait for the HP side to release its mutex. * The return value is HPERR if the HP side has terminated. **********************************************************************/INT16 wait_for_mutex() {  /*wait for HP to be ready */  INT16 result = HPERR;          /*return code */  #if defined (__MSW32__) || defined (__MAC__)  result = WaitForSingleObject (shm.mutex, (unsigned long) -1)  /*wait for thread to move */                                 /*bad if timeout */    == WAIT_OBJECT_0 ? OKAY : HPERR;  #endif  if (result != OKAY)    ocr_internal_shutdown();   return result;}/********************************************************************** * wait_for_hp * * Wait for the HP side to release its semaphore. * The return value is HPERR if the timeout (in seconds) elapsed. **********************************************************************/INT16 wait_for_hp(               /*wait for semaphore */                  INT32 timeout  /*in seconds */                 ) {  INT16 result = HPERR;          /*return code */  #if defined (__MSW32__) || defined (__MAC__)                                 /*wait for thread to move */  result = WaitForSingleObject (shm.hp_sem, timeout * TICKS)                                 /*bad if timeout */    == WAIT_OBJECT_0 ? OKAY : HPERR;  #endif  if (result != OKAY)    ocr_internal_shutdown();   return result;}/********************************************************************** * release_mutex * * Release the HP mutex. * The return value is OKAY if the call succeeds. **********************************************************************/INT16 release_mutex() {  /*release mutex */  INT16 result = HPERR;          /*return code */  #ifdef __MSW32__                                 /*release it */  result = ReleaseMutex (shm.mutex) ? OKAY : HPERR;  #elif defined (__MAC__)                                 /*release it */  result = ReleaseSemaphore (shm.mutex) ? OKAY : HPERR;  #endif  if (result != OKAY)    ocr_internal_shutdown();   return result;}/********************************************************************** * release_ocr * * Release the OCR semaphore. * The return value is OKAY if the call succeeds. **********************************************************************/INT16 release_ocr() {  /*release semaphore */  INT32 timeout;                 //time allowed  timeout = RELEASE_TIMEOUT * TICKS;  #ifdef __MSW32__  BOOL result = 0;               //of release  do {                                 //release it    result = ReleaseSemaphore (shm.ocr_sem, 1, NULL);    if (result == FALSE) {      timeout -= 50;      Sleep (50);    }  }  while (result == FALSE && timeout > 0);  if (!result)    ocr_internal_shutdown();   return OKAY;  #elif defined (__MAC__)  INT16 result = HPERR;          /*return code */                                 /*release it */  result = ReleaseSemaphore (shm.ocr_sem) ? OKAY : HPERR;  if (result != OKAY)    ocr_internal_shutdown();   return result;  #elif defined (__UNIX__)  return 0;  #endif}

⌨️ 快捷键说明

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