Streltsov
Streltsov

Reputation: 417

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

Basing myself on the code given by MSDN for Handwriting Recognition in windows 10 apps, I tried to have it recognize the handwriting automatically after the user has finished tracing an ink stroke rather than when clicking a button.

I have been able to do something similar in a Windows 8.1 metro app by calling my handwriting recognition method when the PointerReleased event was triggered in my canvas. It worked great and I tried to emulate the same behavior in UWP.

The PointerReleased event was not firing in the UWP app so I used the InkCanvas.InkPresenter.StrokeInput.StrokeEnded event instead to call this method :

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.";
    }
}

It's close to what I want to achieve, except the last stroke is not taken into account. I guess that when the StrokeEnded event is triggered, the InkStroke has not yet been "processed" and thus it is not included in currentStrokes.

I tried to circumvent this problem by adding the Strokes corresponding to the InkStrokeInput of the event to the StrokeContainer I use as a parameter for the recognition :

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);

But input.InkPresenter.StrokeContainer.GetStrokes() returns an empty List.

Is there a way for me to access the current Stroke when the event is triggered ? Or is there another event I could use to call the handwriting recognition after the stroke has been "processed" ? Or another way to automatically recognize the handwriting for all the current InkStrokes alltogether ?

Upvotes: 2

Views: 1058

Answers (1)

Streltsov
Streltsov

Reputation: 417

I have found a way to achieve the result I was expecting by calling my RecognizedAsync method when the InkCanvas.InkPresenter.StrokesCollected event is triggered.

From the MSDN documentation :

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).

Upvotes: 3

Related Questions