/** @brief Clear the handwriter background. */ void ChandWriter::clearImage() { if ( Cgraphics::m_defaults.handwriting_detection_enabled) { #ifdef USE_SDL2 assert(0); #else SDL_Rect rect; rect.h =(Uint16)m_surface->h; rect.w =(Uint16)m_surface->w; rect.x =0; rect.y =0; SDL_FillRect( m_surface, &rect, m_background.getColour()); #endif m_stroke =0; m_index =0; m_lastPoint =Cpoint(-5,-100); #ifdef USE_ZINNIA if ( m_character) { zinnia_character_clear( m_character); zinnia_character_set_width( m_character, m_rect.width()*8); zinnia_character_set_height( m_character, m_rect.height()*8); } #endif } }
int main(int argc, char **argv) { size_t i; zinnia_recognizer_t *recognizer; zinnia_character_t *character; zinnia_result_t *result; recognizer = zinnia_recognizer_new(); if (!zinnia_recognizer_open(recognizer, "/usr/local/lib/zinnia/model/tomoe/handwriting-ja.model")) { fprintf(stderr, "ERROR: %s\n", zinnia_recognizer_strerror(recognizer)); return -1; } zinnia_character_t *character = zinnia_character_new(); zinnia_character_clear(character); zinnia_character_set_width(character, 300); zinnia_character_set_height(character, 300); zinnia_character_add(character, 0, 51, 29); zinnia_character_add(character, 0, 117, 41); zinnia_character_add(character, 1, 99, 65); zinnia_character_add(character, 1, 219, 77); zinnia_character_add(character, 2, 27, 131); zinnia_character_add(character, 2, 261, 131); zinnia_character_add(character, 3, 129, 17); zinnia_character_add(character, 3, 57, 203); zinnia_character_add(character, 4, 111, 71); zinnia_character_add(character, 4, 219, 173); zinnia_character_add(character, 5, 81, 161); zinnia_character_add(character, 5, 93, 281); zinnia_character_add(character, 6, 99, 167); zinnia_character_add(character, 6, 207, 167); zinnia_character_add(character, 6, 189, 245); zinnia_character_add(character, 7, 99, 227); zinnia_character_add(character, 7, 189, 227); zinnia_character_add(character, 8, 111, 257); zinnia_character_add(character, 8, 189, 245); result = zinnia_recognizer_classify(recognizer, character, 10); if (result == NULL) { fprintf(stderr, "%s\n", zinnia_recognizer_strerror(recognizer)); return -1; } for (i = 0; i < zinnia_result_size(result); ++i) { fprintf(stdout, "%s\t%f\n", zinnia_result_value(result, i), zinnia_result_score(result, i)); } zinnia_result_destroy(result); zinnia_character_destroy(character); zinnia_recognizer_destroy(recognizer); return 0; }
void QHWR::stok2qchar() { zinnia_result_t *result; LineStroke * l; character = zinnia_character_new(); dstr.clear(); zinnia_character_clear(character); zinnia_character_set_width(character, 250); zinnia_character_set_height(character, 250); for(int i=0;i<strokes.size();i++) { l = &strokes[i]; for (int j= 0; j < (l->segments); j+=(l->segments/10+1)) { l->points[j]->x(), l->points[j]->y() ; // if(j>2) // j=l->segments-1; zinnia_character_add(character, i, l->points[j]->x(), l->points[j]->y()); } } result = zinnia_recognizer_classify(recognizer, character, 100); if (result == NULL) { fprintf(stderr, "%s\n", zinnia_recognizer_strerror(recognizer)); zinnia_character_destroy(character); //zinnia_recognizer_destroy(recognizer); return ; } for (int i = 0; i < zinnia_result_size(result); ++i) { dstr.append(QString::fromUtf8(zinnia_result_value(result, i),3)); // strcpy(c->charactor,zinnia_result_value(result, i)); fprintf(stdout, "%s\t%f\n", zinnia_result_value(result, i), zinnia_result_score(result, i)); } zinnia_result_destroy(result); zinnia_character_destroy(character); return; }
static gboolean ibus_handwrite_recog_zinnia_domatch(IbusHandwriteRecog*obj,int want) { size_t i; size_t ii; zinnia_recognizer_t *recognizer; zinnia_character_t *character; zinnia_result_t *result; LineStroke cl; obj->matched = g_array_set_size(obj->matched,0); recognizer = IBUS_HANDWRITE_RECOG_ZINNIA(obj)->recognizer; character = zinnia_character_new(); zinnia_character_clear(character); zinnia_character_set_width(character, 200); zinnia_character_set_height(character, 200); for (ii=0; ii < obj->strokes->len ; ++ii) { cl = g_array_index(obj->strokes, LineStroke , ii); for (i = 0; i < (cl.segments); ++i) { zinnia_character_add(character, ii, cl.points[i].x, cl.points[i].y); } } result = zinnia_recognizer_classify( recognizer, character, want); g_print("char %p\n",result); if (result == NULL) { fprintf(stderr, "%s\n", zinnia_recognizer_strerror(recognizer)); zinnia_character_destroy(character); return FALSE; } int result_num = zinnia_result_size(result); g_print("char \n"); for (i = 0; i < result_num; ++i) { MatchedChar matched; strcpy(matched.chr,zinnia_result_value(result, i)); obj->matched = g_array_append_val(obj->matched,matched); } zinnia_result_destroy(result); zinnia_character_destroy(character); return TRUE; }
/* * Class: com_key_handwrite_Zinnia * Method: zinnia_character_set_width * Signature: (JJ)V */ JNIEXPORT void JNICALL Java_com_key_handwrite_Zinnia_zinnia_1character_1set_1width (JNIEnv *env, jobject jobj, jlong character, jlong width) { zinnia_character_t* p = reinterpret_cast<zinnia_character_t*>(character); zinnia_character_set_width(p, width); }
void ZinniaProcess(void* ud, pt_t* points, int nPoints) { Zinnia* zn = (Zinnia*) ud; // clear the last result if(zn->result != NULL) { zinnia_result_destroy(zn->result); zn->result = NULL; } // get bounding box short xmin=SHRT_MAX,xmax=SHRT_MIN,ymin=SHRT_MAX,ymax=SHRT_MIN; for(int i=0; i<nPoints; ++i) { if(PT_ISVALID(points[i])) { if(points[i].x > xmax) xmax = points[i].x; if(points[i].x < xmin) xmin = points[i].x; if(points[i].y > ymax) ymax = points[i].y; if(points[i].y < ymin) ymin = points[i].y; } } // add some margin ymin -= 4; xmin -= 4; ymax += 4; xmax += 4; // determine scaling factors float sf; if(xmax - xmin > ymax - ymin) { sf = 300.0 / (xmax - xmin); ymin = 0; } else { sf = 300.0 / (ymax - ymin); xmin = 0; } // the stroke index size_t id = 0; // create a new character zinnia_character_t *character = zinnia_character_new(); zinnia_character_clear(character); zinnia_character_set_width(character, 300); zinnia_character_set_height(character, 300); // the index in the point array from which this stroke begins int from = 0; for(int i=0; i<nPoints; ++i) { // If we get to a stroke break, do the magic if(!PT_ISVALID(points[i])) { pt_t* simplified = 0; // Simplify the line we have so far. An epsilon of 6 is a guess, // it probably should be determined based on xmax,xmin,ymax,ymin int n = SimplifyLine(&points[from],i-from,&simplified,6); // Add the scaled stroke to the zinnia character for(int j=0;j<n;++j) zinnia_character_add(character, id, sf * (simplified[j].x - xmin), sf * (simplified[j].y - ymin)); // clear the simplified stroke memory free(simplified); // advance to the next stroke id++; from = i+1; } } zn->result = zinnia_recognizer_classify(zn->recog, character, 10); if (zn->result == NULL) { FcitxLog(ERROR, "Could not classify character"); return; } zinnia_character_destroy(character); }