Universal Windows Platform – Analysis App

Analysis App shows how to create a simple application that can recognise anything written as text in an InkCanvas and output it to a TextBlock.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using Windows.UI.Core;
using Windows.UI.Input.Inking;
using Windows.UI.Input.Inking.Analysis;
using Windows.UI.Xaml.Controls;

public class Library
{
    private InkPresenter _presenter;
    private InkAnalyzer _analyser;

    private void Presenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
    {
        _analyser.AddDataForStrokes(args.Strokes);
    }

    private void Presenter_StrokesErased(InkPresenter sender, InkStrokesErasedEventArgs args)
    {
        foreach (InkStroke stroke in args.Strokes)
        {
            _analyser.RemoveDataForStroke(stroke.Id);
        }
    }

    public void Init(ref InkCanvas inkCanvas)
    {
        _presenter = inkCanvas.InkPresenter;
        _presenter.StrokesCollected += Presenter_StrokesCollected;
        _presenter.StrokesErased += Presenter_StrokesErased;
        _presenter.InputDeviceTypes =
        CoreInputDeviceTypes.Pen |
        CoreInputDeviceTypes.Mouse |
        CoreInputDeviceTypes.Touch;
        _analyser = new InkAnalyzer();
    }

    public async void Analyse(TextBlock display)
    {
        IReadOnlyList<InkStroke> strokes = _presenter.StrokeContainer.GetStrokes();
        foreach (InkStroke stroke in strokes)
        {
            _analyser.SetStrokeDataKind(stroke.Id, InkAnalysisStrokeKind.Writing);
        }
        InkAnalysisResult result = await _analyser.AnalyzeAsync();
        if (result.Status == InkAnalysisStatus.Updated)
        {
            display.Text = _analyser.AnalysisRoot.RecognizedText;
        }
    }

    public void Clear(ref TextBlock display)
    {
        display.Text = string.Empty;
        _presenter.StrokeContainer.Clear();
        _analyser.ClearDataForAllStrokes();
    }
}

In the Code File for Library there are using statements to include the necessary functionality. There are Members for InkPresenter and InkAnalyzer. The Presenter_StrokesCollected and Presenter_StrokesErased Event Handlers are used to call the AddDataForStrokes and RemoveDataForStroke Methods of the InkAnalyzer respectively.

While in the Library Class there is an Init Method which is used to assign the InkPresenter from an InkCanvas and setup the Event Handlers and configure the InputDeviceTypes. The Analyse Method is used to get a IReadOnlyList of InkStroke from the InkPresenter and foreach of these call SetStrokeDataKind to InkAnalysisStrokeKind.Writing and then will call the AnalyzeAsync of the InkAnalyzer to recognise any written text and once the Status is InkAnalysisStatus.Updated the TextBlock passed in has its Text Property set to the RecognizedText of the InkAnalyzer. The Clear Method is used to reset the TextBlock and the InkPresenter and InkAnalyzer.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in in this between the Grid and /Grid elements, enter the following XAML:

<Grid Margin="50">
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="50*"/>
		<RowDefinition Height="50*"/>
	</Grid.RowDefinitions>
	<InkToolbar Grid.Row="0" TargetInkCanvas="{x:Bind InkCanvas}" InitialControls="None">
		<InkToolbarBallpointPenButton/>
		<InkToolbarPencilButton/>
		<InkToolbarEraserButton/>
	</InkToolbar>
	<InkCanvas Grid.Row="1" x:Name="InkCanvas"/>
	<Grid Grid.Row="2" Background="WhiteSmoke">
		<TextBlock Name="Display" FontSize="100"
		HorizontalAlignment="Center" VerticalAlignment="Center"/>
	</Grid>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Scan" Label="Analyse" Click="Analyse_Click"/>
	<AppBarButton Icon="Clear" Label="Clear" Click="Clear_Click"/>
</CommandBar>

Within the main Grid Element, the first block of XAML is a Grid Control which has three Rows, in the first Row is an InkToolbar, the third Row is an InkCanvas and in the third Row is a TextBlock. The second block of XAML is a CommandBar with AppBarButton for Analyse which calls Analyse_Click and Clear which calls Clear_Click.

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	library.Init(ref InkCanvas);
}

private void Analyse_Click(object sender, RoutedEventArgs e)
{
	library.Analyse(Display);
}

private void Clear_Click(object sender, RoutedEventArgs e)
{
	library.Clear(ref Display);
}

There is an OnNavigatedTo Event Handler which calls the Init Method in the Library Class and Analyse_Click which calls the Analyse Method and Clear_Click which calls the Clear Method of the Library Class.

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

Once the Application has started running you can then write something with a Pen, Mouse or Touch in the InkCanvas and then select Analyse to display the recognised text in the TextBlock

uwp-ran-analysis-app

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

This example shows how you can use the build-in functionality of an InkCanvas to not only draw easily with Pen, Mouse or Touch but also use Ink Analysis to recognise what has been written as Text. This example is based upon the Ink Analysis sample in the Windows universal samples on GitHub by Microsoft.

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s