void ofxOcrad::doOCR() { OCRAD_Descriptor * od; char str[1024]; const unsigned char invert = 1; int blocknum, linenum; od = OCRAD_open(); OCRAD_set_image(od, &pixmap, 0); OCRAD_get_errno(od); OCRAD_recognize(od, 1); blocknum = OCRAD_result_blocks(od); int pos = 0; for(int i=0; i<blocknum; i++){ linenum = OCRAD_result_lines(od, i); for(int j=0; j<linenum; j++){ sprintf(&str[pos], "%s\n", OCRAD_result_line(od,i,j)); pos = pos + strlen(OCRAD_result_line(od,i,j)); } } strcpy(str_result, str); OCRAD_close(od); }
// Function: osra_ocrad_ocr() // Make an attempt to OCR the image box with OCRAD engine. // // Parameters: // ocrad_pixmap - includes pixel map and the image mode // char_filter - character filter // // Returns: // 0 in case the recognition failed or valid alphanumeric character char osra_ocrad_ocr(const OCRAD_Pixmap * const ocrad_pixmap, const string &char_filter) { char result = 0; string line; OCRAD_Descriptor * const ocrad_res = OCRAD_open(); // If the box height is less than 10px, it should be scaled up a bit, otherwise OCRAD is unable to catch it: if (ocrad_res && OCRAD_get_errno(ocrad_res) == OCRAD_ok && OCRAD_set_image(ocrad_res, ocrad_pixmap, 0) == 0 && (ocrad_pixmap->height >= 10 || OCRAD_scale(ocrad_res, 2) == 0) && OCRAD_recognize(ocrad_res, 0) == 0) { result = OCRAD_result_first_character(ocrad_res); if (OCRAD_result_blocks(ocrad_res) >= 1 && OCRAD_result_lines(ocrad_res, 0) && OCRAD_result_line( ocrad_res, 0, 0) != 0) line = OCRAD_result_line(ocrad_res, 0, 0); } OCRAD_close(ocrad_res); // TODO: Why line should have 0 or 1 characters? Give examples... if (line.length() > 2 || !isalnum(result) || (!char_filter.empty() && char_filter.find(result, 0) == string::npos)) return UNKNOWN_CHAR; return result; }