每次用户完成描笔后,如何进行手写识别?

How can I do handwriting recognition each time a user has finished tracing a stroke?

根据 MSDN 为 windows 10 个应用程序中的 Handwriting Recognition 提供的代码,我尝试让它在用户完成笔迹描画后自动识别笔迹,而不是在单击时自动识别一个按钮。

当我的 canvas 中触发 PointerReleased 事件时,我已经能够通过调用我的手写识别方法在 Windows 8.1 metro 应用程序中做类似的事情。它运行良好,我尝试在 UWP 中模拟相同的行为。

UWP 应用程序中未触发 PointerReleased 事件,因此我使用 InkCanvas.InkPresenter.StrokeInput.StrokeEnded 事件来调用此方法:

async void RecognizeAsync(InkStrokeInput input, PointerEventArgs e)
{
    IReadOnlyList<InkStroke> currentStrokes =myInkCanvas.InkPresenter.StrokeContainer.GetStrokes();
    if (currentStrokes.Count > 0)
    {
        var recognitionResults = await inkRecognizerContainer.RecognizeAsync(myInkCanvas.InkPresenter.StrokeContainer, InkRecognitionTarget.All);

        if (recognitionResults.Count > 0)
        {
            // Display recognition result
            string str = "Recognition result:";
            foreach (var r in recognitionResults)
            {
                str += " " + r.GetTextCandidates()[0];
            }
            Status.Text=str;
        }
        else
        {
            Status.Text = "No text recognized.";
        }
    }
    else
    {
        Status.Text="Must first write something.";
    }
}

很接近我想要达到的效果,只是没有考虑到最后一笔。 我猜想当 StrokeEnded 事件被触发时,InkStroke 尚未被“处理”,因此它不包含在 currentStrokes 中。

我试图通过将事件的 InkStrokeInput 对应的 Strokes 添加到我用作识别参数的 StrokeContainer 来规避这个问题:

InkStrokeContainer totalStrokes=new InkStrokeContainer();
if (currentStrokes.Count > 0) {
    totalStrokes= myInkCanvas.InkPresenter.StrokeContainer;
}
totalStrokes.AddStrokes(input.InkPresenter.StrokeContainer.GetStrokes());

var recognitionResults = await inkRecognizerContainer.RecognizeAsync(totalStrokes, InkRecognitionTarget.All);

但是 input.InkPresenter.StrokeContainer.GetStrokes() returns 一个空列表。

有没有办法让我在事件触发时访问当前的Stroke?或者在笔划被“处理”后,我可以使用另一个事件来调用手写识别吗? 或者另一种自动识别所有当前 InkStrokes 的笔迹的方法?

我找到了一种方法,通过在触发 InkCanvas.InkPresenter.StrokesCollected 事件时调用我的 RecognizedAsync 方法来实现我期望的结果。

来自 MSDN 文档:

InkPresenter.StrokesCollected event

Occurs when one or more ink strokes are processed ("wet" to "dry") by the >application thread.

By default, an ink stroke is processed on a low-latency background thread and >rendered wet as it is drawn. When the stroke is completed (pen or finger >lifted, or mouse button released), the stroke is processed on the UI thread >and rendered dry to the InkCanvas layer (above the application content).