在android中替换手势库模板

时间:2013-09-10 21:09:55

标签: android gestures gesture-recognition template-matching

是否可以从正在运行的应用程序中替换模板库的手势模板? 我正在构建一个手写识别器系统,其中手势库文件中有字母模板。所以基本上在代码中加载库后我比较用户输入的手势如:

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

   if (predictions.size() > 1) {
    for(Prediction prediction: predictions){
       //i compare prediction name here and if matches print it in an edittext
    }

这应该可以正常工作,直到用户提供与构建库模板时相同的模式。但是我想让用户在预测不匹配时灵活地用他的手写模式替换模板项。

因为下面2个手写的手势样本在图案方面不同,而不是作为字母。假设,我的系统支持第1个图像模式,我想当用户给出第2个图像模式时系统会要求用户确认更换使用A的库模式然后在确认之后将其替换。所以下次系统将更好地识别用户模式。

非常感谢任何帮助。

enter image description here enter image description here

2 个答案:

答案 0 :(得分:5)

如果我理解正确,您想用新的手势替换现有的手势吗?

因此,当用户输入不在库中的手势时,您的应用会要求用户选择要替换的手势?根据您的问题,我将假设当用户绘制小写a时(如果a不在库中),则会向用户显示当前应用的所有可用手势/字母的列表支持。然后,用户选择大写A,现在,大写A必须替换为小写a。在以下代码中,oldGesture是与A对应的手势。 newGesture是刚刚绘制的手势。

这个过程将是:删除旧手势,使用旧手势的名称添加新手势。要删除手势,请使用GestureLibrary.removeGesture(String,Gesture):

public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {

    ArrayList<Prediction> predictions = gesturelib.recognize(gesture);

    if (predictions.size() > 1) {

        for(Prediction prediction: predictions){

            if (prediction.score > ...) {

            } else {

                if (user wants to replace) {

                    showListWithAllGestures(gesture);
                }
            }
        }
    }
}

public void showListWithAllGestures(Gesture newGesture) {
    ....
    ....

    // User picks a gesture
    Gesture oldGesture = userPickedItem.gesture;
    String gestureName = userPickedItem.name;

    // delete the gesture
    gesturelib.removeGesture(gestureName, oldGesture);
    gesturelib.save();

    // add gesture
    gesturelib.addGesture(gestureName, newGesture);
    gesturelib.save();

}

获取所有可用手势的列表:

// Wrapper to hold a gesture
static class GestureHolder {
    String name;
    Gesture gesture;
}

使用GestureLibrary.load()加载手势:

if (gesturelib.load()) {

    for (String name : gesturelib.getGestureEntries()) {

        for (Gesture gesture : gesturelib.getGestures(name)) {

            final GestureHolder gestureHolder = new GestureHolder();
            gestureHolder.gesture = gesture;
            gestureHolder.name = name;

            // Add `gestureHolder` to a list

        }
    }

    // Return the list that holds GestureHolder objects

}

修改

很抱歉,但是我建议的检查:if (wants to replace)正在代码中的错误位置执行。

if (predictions.size() > 1) {

    // To check whether a match was found
    boolean gotAMatch = false;

    for(int i = 0; i < predictions.size() && !gotAMatch; i++){

        if (prediction.score > ... ) {

            ....
            ....

            // Found a match, look no more
            gotAMatch = true;

        }
    }

    // If a match wasn't found, ask the user s/he wants to add it
    if (!gotAMatch) {

        if (user wants to replace) {

            showListWithAllGestures(gesture);
        }
    }
}

答案 1 :(得分:0)

您可以看到此链接,也许可以向我提供更多信息

How to send file from android to computer via bluetooth