package pothos_tegaki;

import java.util.ArrayList;

import jp.ac.kyutech.mns.ist.ShortStroke;
import jp.ac.kyutech.mns.ist.util.MyQueue;
import jp.co.nttcom_tokai.business.framework.recognizer.PothosApi;

/**
 * 認識器とのインタフェース
 * 
 * @author miuramo
 *
 */
public class PothosLibRecognizer extends Thread {
//	PothosApi engine;
	MyQueue<TegakiRecogTask> queue;
	boolean isRun = true;
	public PothosLibRecognizer(){
		PothosApi.HandsInkInitialize();
		queue = new MyQueue<TegakiRecogTask>();
	}
	public void finalize(){
		PothosApi.HandsInkClose();
	}
	public void startThread(){
		isRun = true;
		start();
	}
	public void stopThread(){
		isRun = false;
	}
	public void run(){
		while(isRun){
			TegakiRecogTask trt = queue.get();
			String s = recognize(trt);
			trt.result = s; // 手書き結果を認識タスクオブジェクトにセット
			trt.callback(); // コールバック
		}
		finalize();
	}	
	public String recognize(TegakiRecogTask t){
		ArrayList<ShortStroke> tempssvec2 = t.ssvec;
		for (ShortStroke ss : tempssvec2) {
			PothosApi.HandsInkAddStroke();
			for (int i = 0; i < ss.sx.length; i++) {
				PothosApi.HandsInkAddPoint((int)ss.sx[i], (int)ss.sy[i]);
			}
		}
		String retStr = null;
		try{
			byte[] b = PothosApi.HandsInkRecognize();
			retStr = new String(b,"MS932");
		}catch(Exception ex){
		}
		PothosApi.HandsInkClear();
		return retStr;
	}
}

