Dela via


Tablet PC: Performing multi-language recognition with the InkAnalysis API

One of the important scenarios to consider when developing a world class Ink-enabled application is localization and multi-language considerations. Why not tap into the power of the handwriting recognizers for various locales? Handwriting recognition with the tablet PC can enable new scenarios for users around the world.

Using the InkAnalysis API, multi language recognition is easier and more seamless than in the old recognition API (V 1.7 RecognizerContext). When you want to analyze ink, you first call the InkAnalyzer.AddStroke() or InkAnalyzer.AddStrokes() methods, and later call InkAnalyzer.Analyze() or InkAnalyzer.BackgroundAnalyze(). By default the language ID corresponding to the thread the InkAnalyzer was created on is used. This works for most scenarios. If you want to specify a specific language ID for the stroke(s) being added to the InkAnalyzer, you just call the AddStroke() or AddStrokes() overrides that have a second parameter which is the integer language ID to associate with the stroke. It’s that easy!

In today’s code sample I’ve demonstrated the following:

1. Enumerating the recognizers installed on the system by calling InkAnalyzer.GetInkRecognizersByPriority()

2. Adding the names of the recognizers to a ComboBox

3. Storing the first supported language from each recognizer in a list of integers

4. Adding newly created strokes to the InkAnalyzer with the integer corresponding to the first supported language ID of the recognizer currently selected in the ComboBox

5. Performing asynchronous Analysis

6. Displaying the multi language recognized string when the user clicks the Recognize() button

Screenshot of Application on Vista:

Example Code:

//

// Basic Ink enabled Windows Forms application with

// handwriting recognition using InkAnalyzer

// Gavin Gear - https://blogs.msdn.com/gavingear

// 09/2006

//

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.Ink; // The managed Tablet PC API

namespace BasicInkApplication

{

    public partial class BasicInkApplication : Form

    {

        // The InkOverlay that we'll attach to our Form

        private InkOverlay inkOverlay;

        private InkAnalyzer inkAnalyzer;

        private InkRecognizerCollection recos;

        private List<int> topLanguageIDs;

        public BasicInkApplication()

        {

            InitializeComponent();

            // Create an InkOverlay object that's attached to the Form

            this.inkOverlay = new InkOverlay(this);

       // Enable the InkOverlay (default is Enabled == false)

            this.inkOverlay.Enabled = true;

            this.inkOverlay.Stroke += new InkCollectorStrokeEventHandler(inkOverlay_Stroke);

            // Create a new InkAnalyzer

            // - Associate with the InkOverlay's Ink object

            // - Send the Form "this" as the synchronizing object

            this.inkAnalyzer = new InkAnalyzer(this.inkOverlay.Ink, this);

            // The InkOverlay needs to be disposed due to unmanaged resources

            // used by the InkOverlay

            this.FormClosing += new FormClosingEventHandler(BasicInkApplication_FormClosing);

            this.recos = this.inkAnalyzer.GetInkRecognizersByPriority();

            this.topLanguageIDs = new List<int>();

            if (recos.Count == 0)

            {

                MessageBox.Show("No recognizers installed on system.", "Error");

                Application.Exit();

            }

           

            foreach (InkRecognizer reco in recos)

            {

                // Check to see if the current recognizer in the collection

                // has supported languages, if so, add it to the combo box

                if (reco.GetLanguages().Length != 0)

                {

                    this.comboBoxLanguage.Items.Add(reco.Name);

                    this.topLanguageIDs.Add(reco.GetLanguages()[0]);

                }

            }

            this.comboBoxLanguage.SelectedIndex = 0;

        }

        void inkOverlay_Stroke(object sender, InkCollectorStrokeEventArgs e)

        {

            // We have a new stroke, add it to the InkAnalyzer

            this.inkAnalyzer.AddStroke(

                e.Stroke,

                this.topLanguageIDs[this.comboBoxLanguage.SelectedIndex]);

            this.inkAnalyzer.BackgroundAnalyze();

        }

        void BasicInkApplication_FormClosing(object sender, FormClosingEventArgs e)

        {

            this.inkOverlay.Dispose();

            this.inkAnalyzer.Dispose(); // Free the unmanaged resources

        }

        private void buttonRecognize_Click(object sender, EventArgs e)

        {

            MessageBox.Show(this.inkAnalyzer.GetRecognizedString());

        }

    }

}

As you can see- multi language recognition with InkAnalysis is pretty straight forward. This makes dealing with composite documents easy and opens up a lot of new possibilities for international Ink aware applications.

See ya,

Gavin

Comments