Riconoscimento della grafia
Questa sezione illustra alcune nozioni di base del riconoscimento relativamente all'input penna digitale nella piattaforma WPF.
Soluzioni di riconoscimento
Nell'esempio seguente viene mostrato come riconoscere l'input penna usando la classe Microsoft.Ink.InkCollector.
Nota
Per questo esempio è necessario che il riconoscimento grafia sia installato nel sistema.
Creare un nuovo progetto di applicazione WPF in Visual Studio denominato InkRecognition. Sostituire il contenuto del file Window1.xaml con il codice XAML seguente. Questo codice esegue il rendering dell'interfaccia utente dell'applicazione.
<Window x:Class="InkRecognition.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="InkRecognition"
>
<Canvas Name="theRootCanvas">
<Border
Background="White"
BorderBrush="Black"
BorderThickness="2"
Height="300"
Width="300"
Canvas.Top="10"
Canvas.Left="10">
<InkCanvas Name="theInkCanvas"></InkCanvas>
</Border>
<TextBox Name="textBox1"
Height="25"
Width="225"
Canvas.Top="325"
Canvas.Left="10"></TextBox>
<Button
Height="25"
Width="75"
Canvas.Top="325"
Canvas.Left="235"
Click="buttonClick">Recognize</Button>
<Button x:Name="btnClear" Content="Clear Canvas" Canvas.Left="10" Canvas.Top="367" Width="75" Click="btnClear_Click"/>
</Canvas>
</Window>
Aggiungere un riferimento all'assembly Microsoft Ink Microsoft.Ink.dll, che può trovarsi in \Programmi\File comuni\Microsoft Shared\Ink. Sostituire il contenuto del file code-behind con il codice seguente.
using System.Windows;
using Microsoft.Ink;
using System.IO;
namespace InkRecognition
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
// Recognizes handwriting by using RecognizerContext
private void buttonClick(object sender, RoutedEventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
theInkCanvas.Strokes.Save(ms);
var myInkCollector = new InkCollector();
var ink = new Ink();
ink.Load(ms.ToArray());
using (RecognizerContext context = new RecognizerContext())
{
if (ink.Strokes.Count > 0)
{
context.Strokes = ink.Strokes;
RecognitionStatus status;
var result = context.Recognize(out status);
if (status == RecognitionStatus.NoError)
textBox1.Text = result.TopString;
else
MessageBox.Show("Recognition failed");
}
else
{
MessageBox.Show("No stroke detected");
}
}
}
}
private void btnClear_Click(object sender, RoutedEventArgs e) {
theInkCanvas.Strokes.Clear();
}
}
}
Imports System.Windows
Imports Microsoft.Ink
Imports System.IO
'/ <summary>
'/ Interaction logic for Window1.xaml
'/ </summary>
Namespace InkRecognition
Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
' Recognizes handwriting by using RecognizerContext
Private Sub buttonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Using ms As New MemoryStream()
theInkCanvas.Strokes.Save(ms)
Dim myInkCollector As InkCollector = New InkCollector()
Dim ink As Ink = New Ink()
ink.Load(ms.ToArray())
Using context As New RecognizerContext()
If ink.Strokes.Count > 0 Then
context.Strokes = ink.Strokes
Dim status As RecognitionStatus
Dim result As RecognitionResult = context.Recognize(status)
If status = RecognitionStatus.NoError Then
textBox1.Text = result.TopString
Else
MessageBox.Show("Recognition failed")
End If
Else
MessageBox.Show("No stroke detected")
End If
End Using
End Using
End Sub
Private Sub btnClear_Click(sender As Object, e As RoutedEventArgs)
theInkCanvas.Strokes.Clear()
End Sub
End Class
End Namespace
Vedi anche
.NET Desktop feedback