Android中的手势分割

时间:2013-04-13 10:05:38

标签: android gesture-recognition handwriting

我正在通过Android的手势进行手写字母检测的示例工作。当我一次输入 1个字符时效果很好。这意味着当我通过手势在屏幕上写 A 时,程序会很好地识别它(因为我之前将它放在手势库上)。截至目前,我的代码就像这样。

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gLib.recognize(gesture);

if (predictions.size() > 0 && predictions.get(0).score > 1.0) {

    String letter = predictions.get(0).name;  

    Toast.makeText(this, letter, Toast.LENGTH_SHORT).show();

    if(letter.contains("A"))  //when matches i print it to edittext
    edittext.setText("A");
    .
    .      //rest of stuff here like previous way
    .

    }
}

但我的标准不是那样。我想认识一个字。我想一次写一个单词。 example

在为每个成功的比赛写一个单词时,相应的字母应该像edit一样打印在edittext上。

A,N,D,R,O,I,D

所以我的问题是如何才能获得它?是否可以分割手势(在写作时对单词进行分段)?任何有效的代码示例或链接都将受到赞赏。

1 个答案:

答案 0 :(得分:4)

如果您将单词写成单独的字母(即不是草书),如问题中给出的图像所示。然后只需这样做 -

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gLib.recognize(gesture);

    if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);
        String letter = prediction.name;

        if (prediction.score > 1.0) {
            edittext.setText(edittext.getText().toString() + letter);
        }
    }
}

本质上是将新字母附加到现有的edittext字符串。

但如果你在谈论草书写作,那就太复杂了。这是一些可以跟踪草书写作的代码。

public class MainActivity extends Activity {
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Tracker t = new Tracker();
        t.start();
    }

    @Override
    protected void onPause() {
        if (mHandler != null) 
            mHandler.getLooper().quit();
        super.onPause();
    }   

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (mHandler != null) {
                Message msg = Message.obtain();
                msg.obj = event.getX() + "," + event.getY();
                mHandler.sendMessage(msg);
            }
            break;
        }
        return true;
    }

    private class Tracker extends Thread {
        private static final int LETTER_SIZE = 30;

        private GestureLibrary gLib;
        private ArrayList<GesturePoint> points;

        public Tracker() {
            points = new ArrayList<GesturePoint>();
            gLib = GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
            gLib.load();
        }

        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler() {

                public void handleMessage(Message msg) {
                    String[] pos = String.valueOf(msg.obj).split(",");
                    points.add(new GesturePoint(Float.parseFloat(pos[0]), Float.parseFloat(pos[1]), System.currentTimeMillis()));

                    if (points.size() < LETTER_SIZE) return;

                    GestureStroke stroke = new GestureStroke(points);
                    Gesture gesture = new Gesture();
                    gesture.addStroke(stroke);

                    ArrayList<Prediction> predictions = gLib.recognize(gesture);
                    if (predictions.size() > 0) {
                        Prediction prediction = predictions.get(0);
                        String letter = prediction.name;

                        if (prediction.score > 1.0) {
                            Log.e("Found", letter);
                            points.clear();
                        }
                    }
                }
            };          
            Looper.loop();
        }
    }   
}

所以基本上我们捕捉触摸位置并从中创建一个Gesture,它传递给GestureLibrary的recogn()方法。如果识别出手势,则我们将其打印并清除触摸位置,以便可以识别新的字母。

示例项目Cursive_eclipse_project.zip