private async void recognizeText_ClickAsync(object sender, RoutedEventArgs e) { strokesText = inkCanvas.InkPresenter.StrokeContainer.GetStrokes(); // Ensure an ink stroke is present. if (strokesText.Count > 0) { analyzerText.AddDataForStrokes(strokesText); resultText = await analyzerText.AnalyzeAsync(); if (resultText.Status == InkAnalysisStatus.Updated) { words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (var word in words) { InkAnalysisInkWord concreteWord = (InkAnalysisInkWord)word; foreach (string s in concreteWord.TextAlternates) { recognitionResult.Text += s; } } } analyzerText.ClearDataForAllStrokes(); } strokesText = inkCanvas.InkPresenter.StrokeContainer.GetStrokes(); // Ensure an ink stroke is present. if (strokesText.Count > 0) { analyzerText.AddDataForStrokes(strokesText); // Force analyzer to process strokes as handwriting. foreach (var stroke in strokesText) { analyzerText.SetStrokeDataKind(stroke.Id, InkAnalysisStrokeKind.Writing); } // Clear recognition results string. recognitionResult.Text = ""; resultText = await analyzerText.AnalyzeAsync(); if (resultText.Status == InkAnalysisStatus.Updated) { var text = analyzerText.AnalysisRoot.RecognizedText; words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (var word in words) { InkAnalysisInkWord concreteWord = (InkAnalysisInkWord)word; foreach (string s in concreteWord.TextAlternates) { recognitionResult.Text += s + " "; } recognitionResult.Text += " / "; } } analyzerText.ClearDataForAllStrokes(); } }
public async Task <InkTransformResult> TransformTextAndShapesAsync() { var result = new InkTransformResult(_drawingCanvas); var inkStrokes = GetStrokesToConvert(); if (inkStrokes.Any()) { _inkAnalyzer.ClearDataForAllStrokes(); _inkAnalyzer.AddDataForStrokes(inkStrokes); var inkAnalysisResults = await _inkAnalyzer.AnalyzeAsync(); if (inkAnalysisResults.Status == InkAnalysisStatus.Updated) { var words = AnalyzeWords(); var shapes = AnalyzeShapes(); //Generate result result.Strokes.AddRange(inkStrokes); result.TextAndShapes.AddRange(words); result.TextAndShapes.AddRange(shapes); } } return(result); }
// End "Step 5: Support handwriting recognition" // Begin "Step 6: Recognize shapes" //On click recognise shape private async void recognizeShape_ClickAsync(object sender, RoutedEventArgs e) { strokesShape = inkCanvas.InkPresenter.StrokeContainer.GetStrokes(); if (strokesShape.Count > 0) { analyzerShape.AddDataForStrokes(strokesShape); resultShape = await analyzerShape.AnalyzeAsync(); if (resultShape.Status == InkAnalysisStatus.Updated) { var drawings = analyzerShape.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); foreach (var drawing in drawings) { var shape = (InkAnalysisInkDrawing)drawing; if (shape.DrawingKind == InkAnalysisDrawingKind.Drawing) { // Catch and process unsupported shapes (lines and so on) here. } else { // Process recognized shapes here. if (shape.DrawingKind == InkAnalysisDrawingKind.Circle || shape.DrawingKind == InkAnalysisDrawingKind.Ellipse) { DrawEllipse(shape); } else { DrawPolygon(shape); } foreach (var strokeId in shape.GetStrokeIds()) { var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } } analyzerShape.RemoveDataForStrokes(shape.GetStrokeIds()); } inkCanvas.InkPresenter.StrokeContainer.DeleteSelected(); } } }
private async void DispatcherTimer_Tick(object sender, object e) { dispatcherTimer.Stop(); if (!inkAnalyzer.IsAnalyzing) { var result = await inkAnalyzer.AnalyzeAsync(); } else { // Ink analyzer is busy. Wait a while and try again. dispatcherTimer.Start(); } }
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; } }
private async void AppBarButton_Click(object sender, RoutedEventArgs e) { var res = await inkAnalyzer.AnalyzeAsync(); if (res.Status == InkAnalysisStatus.Updated) { StringBuilder bl = new StringBuilder(); var nodes = inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (InkAnalysisInkWord node in nodes) { bl.Append(node.RecognizedText); bl.Append(' '); } displayText.Text = bl.ToString(); } }
private async static void recognize(InkPresenter presenter) { IReadOnlyList <InkStroke> strokes = presenter.StrokeContainer.GetStrokes(); if (strokes.Any()) { inkAnalyzer.AddDataForStrokes(strokes); InkAnalysisResult results = await inkAnalyzer.AnalyzeAsync(); if (results.Status == InkAnalysisStatus.Updated) { ConvertShapes(presenter, inkAnalyzer.AnalysisRoot); } } inkAnalyzer.ClearDataForAllStrokes(); }
private async void DispatcherTimer_Tick(object sender, object e) { dispatcherTimer.Stop(); if (!inkAnalyzer.IsAnalyzing) { InkAnalysisResult results = await inkAnalyzer.AnalyzeAsync(); if (results.Status == InkAnalysisStatus.Updated) { ConvertShapes(); } } else { // Ink analyzer is busy. Wait a while and try again. dispatcherTimer.Start(); } }
private async void RecognizeButton_Click(object sender, RoutedEventArgs e) { var inkStrokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes(); // Ensure an ink stroke is present. if (inkStrokes.Count > 0) { _inkAnalyzer.AddDataForStrokes(inkStrokes); var inkAnalysisResults = await _inkAnalyzer.AnalyzeAsync(); // Have ink strokes on the canvas changed? if (inkAnalysisResults.Status == InkAnalysisStatus.Updated) { var inkdrawingNodes = _inkAnalyzer.AnalysisRoot.FindNodes( InkAnalysisNodeKind.InkDrawing); foreach (InkAnalysisInkDrawing node in inkdrawingNodes) { if (node.DrawingKind != InkAnalysisDrawingKind.Drawing) { // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse). if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse) { DrawEllipse(node); } // Draw a Polygon object on the recognitionCanvas. else { DrawPolygon(node); } foreach (var strokeId in node.GetStrokeIds()) { var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } } _inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } inkCanvas.InkPresenter.StrokeContainer.DeleteSelected(); } } }
private async Task Analyze() { if (_analyzer.IsAnalyzing) { return; } var r = await _analyzer.AnalyzeAsync(); Debug.WriteLine($"{DateTime.Now}: {r.Status}"); output.Text = string.Join("", _analyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord) .OfType <InkAnalysisInkWord>() .Select(x => x.RecognizedText)); foreach (var node in _analyzer.AnalysisRoot.Children) { Debug.WriteLine(node.Kind); } Debug.WriteLine("====="); }
internal async void RecognizeStrokes(IEnumerable <InkStroke> strokes) { inkAnalyzer.AddDataForStrokes(strokes); inkAnalysisResults = await inkAnalyzer.AnalyzeAsync(); // Find all strokes that are recognized as a drawing and create a corresponding ink analysis InkDrawing node. var inkdrawingNodes = inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); foreach (InkAnalysisInkDrawing node in inkdrawingNodes) { // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse). if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse) { DrawEllipse(node); inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } else if (node.DrawingKind != InkAnalysisDrawingKind.Drawing) // Draw a Polygon object on the inkCanvas. { DrawPolygon(node); inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } } }
private async Task AnalyzeInkAsync() { // Does all the recognition var result = await _inkAnalyzer.AnalyzeAsync(); if (result.Status == InkAnalysisStatus.Updated) { // Filter recognition by shapes. Options inlcude lists, paragraphs, words etc. var drawings = _inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); foreach (InkAnalysisInkDrawing drawing in drawings) { if (drawing.DrawingKind == InkAnalysisDrawingKind.Circle) { AddHeart(drawing); RemoveInkStrokes(drawing); } else if (drawing.DrawingKind == InkAnalysisDrawingKind.Triangle || drawing.DrawingKind == InkAnalysisDrawingKind.EquilateralTriangle) { if (string.IsNullOrEmpty(customEmojiGlyph)) { await PickEmoji(); } if (!string.IsNullOrEmpty(customEmojiGlyph)) { AddEmoji(drawing, customEmojiGlyph); } RemoveInkStrokes(drawing); } } _inkAnalyzer.ClearDataForAllStrokes(); _inkPresenter.StrokeContainer.DeleteSelected(); } }
/// <summary> /// Process drawn ink strokes into a bounding box to track /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void UIUpdateButton_Click(object sender, RoutedEventArgs e) { var inkStrokes = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokes(); // Ensure an ink stroke is present. if (inkStrokes.Count > 0) { m_inkAnalyzer.AddDataForStrokes(inkStrokes); // Only analyze as drawings foreach (var strokeNode in inkStrokes) { m_inkAnalyzer.SetStrokeDataKind(strokeNode.Id, InkAnalysisStrokeKind.Drawing); } var inkAnalysisResults = await m_inkAnalyzer.AnalyzeAsync(); // Have ink strokes on the canvas changed? if (inkAnalysisResults.Status == InkAnalysisStatus.Updated) { // Find all strokes that are recognized as a drawing and // create a corresponding ink analysis InkDrawing node. var inkdrawingNodes = m_inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); // Draw recognized shapes on UICanvasOverlay and // delete ink analysis data and recognized strokes. List <Rect> drawnRects = new List <Rect>(); foreach (InkAnalysisInkDrawing node in inkdrawingNodes) { if (node.DrawingKind == InkAnalysisDrawingKind.Drawing) { // Catch and process unsupported shapes (lines and so on) here. foreach (var strokeId in node.GetStrokeIds()) { var stroke = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokeById(strokeId); var drawingAttributes = stroke.DrawingAttributes; drawingAttributes.Color = Windows.UI.Colors.Red; stroke.DrawingAttributes = drawingAttributes; } } // Process generalized shapes here (ellipses and polygons). else { // Normalized rect var boundingRectNormalized = new Rect(node.BoundingRect.X / UIInkCanvasOverlay.ActualWidth, node.BoundingRect.Y / UIInkCanvasOverlay.ActualHeight, node.BoundingRect.Width / UIInkCanvasOverlay.ActualWidth, node.BoundingRect.Height / UIInkCanvasOverlay.ActualHeight); drawnRects.Add(boundingRectNormalized); // Mark ink node strokes for deletion foreach (var strokeId in node.GetStrokeIds()) { var stroke = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } } m_inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } UIInkCanvasOverlay.InkPresenter.StrokeContainer.DeleteSelected(); await m_bboxLock.WaitAsync(); { m_drawnRects.AddRange(drawnRects); m_objectTrackRenderer.RenderRects(m_drawnRects); } m_bboxLock.Release(); } } }
//Recognizes text from ink strokes private async void timer_TickAsync(object sender, object e) { timer.Stop(); if (!analyzerText.IsAnalyzing) { InkAnalysisResult resultText = await analyzerText.AnalyzeAsync(); if (resultText.Status == InkAnalysisStatus.Updated) { var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (InkAnalysisInkWord word in words) { if (calculated) { clear(); } else if (currentCanvasNumber == 0) { int temp = numbers[0]; if (Int32.TryParse(word.RecognizedText, out numbers[0])) { textBlock[0].Text = numbers[0] + ""; hasValues[0] = true; } else if (hasValues[0]) { numbers[0] = temp; } } else if (currentCanvasNumber == 1) { if (word.RecognizedText == "/") { mathOperator = '/'; textBlock[1].Text = "/"; } else if (word.RecognizedText == "*" || word.RecognizedText == "x" || word.RecognizedText == "X") { mathOperator = '*'; textBlock[1].Text = "*"; } else if (word.RecognizedText == "+") { mathOperator = '+'; textBlock[1].Text = "+"; } else if (word.RecognizedText == "-" || word.RecognizedText == '_') { mathOperator = '-'; textBlock[1].Text = "-"; } } else if (currentCanvasNumber == 2) { int temp = numbers[1]; if (Int32.TryParse(word.RecognizedText, out numbers[1])) { textBlock[2].Text = numbers[1] + ""; hasValues[1] = true; } else if (hasValues[1]) { numbers[1] = temp; } } else if (currentCanvasNumber == 3) { if (word.RecognizedText == "=" && canCalculate()) { textBlock[3].Text = "="; calculate(); } } foreach (var strokeId in word.GetStrokeIds()) { var stroke = inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } analyzerText.RemoveDataForStrokes(word.GetStrokeIds()); } inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.DeleteSelected(); } else { timer.Start(); } analyzerText.ClearDataForAllStrokes(); } }
//Recognizes text from the ink strokes private async void timer_TickAsync(object sender, object e) { timer.Stop(); if (!analyzerText.IsAnalyzing) { InkAnalysisResult resultText = await analyzerText.AnalyzeAsync(); if (resultText.Status == InkAnalysisStatus.Updated) { var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (InkAnalysisInkWord word in words) { int userAnswer = 0; if (Int32.TryParse(word.RecognizedText, out userAnswer)) { if (userAnswer == answer) { Info.Text = "Correct!"; await Task.Delay(TimeSpan.FromSeconds(2)); Info.Text = ""; generateNewEquation(); } else { Info.Text = "Wrong, try again!"; await Task.Delay(TimeSpan.FromSeconds(2)); Info.Text = ""; } } else { Info.Text = "Could not detect a number. Try again!"; await Task.Delay(TimeSpan.FromSeconds(2)); Info.Text = ""; } foreach (var strokeId in word.GetStrokeIds()) { var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } analyzerText.RemoveDataForStrokes(word.GetStrokeIds()); } inkCanvas.InkPresenter.StrokeContainer.DeleteSelected(); } else { timer.Start(); } analyzerText.ClearDataForAllStrokes(); } }
private async void Analyze() { InkAnalysisResult result = await inkAnalyzer.AnalyzeAsync(); Shape drawnShape = null; if (result.Status == InkAnalysisStatus.Updated && inkAnalyzer.AnalysisRoot.Children.Count > 0) { if (inkAnalyzer.AnalysisRoot.Children.Last().Kind == InkAnalysisNodeKind.InkDrawing) { // the things we can recognise: https://docs.microsoft.com/en-us/uwp/api/windows.ui.input.inking.analysis.inkanalysisdrawingkind LinearGradientBrush newRandomBrush = GetRandomGradientBrush(); InkAnalysisInkDrawing drawing = inkAnalyzer.AnalysisRoot.Children.Last() as InkAnalysisInkDrawing; if (drawing.DrawingKind == InkAnalysisDrawingKind.Circle || drawing.DrawingKind == InkAnalysisDrawingKind.Ellipse) { // we have detected a circle or ellipse CompositeTransform transform = new CompositeTransform(); Ellipse ellipse = new Ellipse(); AttachDragHandlers(ellipse); ellipse.Fill = newRandomBrush; ellipse.Stroke = new SolidColorBrush(Colors.Black); ellipse.Width = Distance(drawing.Points[0], drawing.Points[2]); var center = new Point((drawing.Points[0].X + drawing.Points[2].X) / 2.0, (drawing.Points[0].Y + drawing.Points[2].Y) / 2.0); if (drawing.DrawingKind == InkAnalysisDrawingKind.Circle) { ellipse.Height = ellipse.Width; } else { ellipse.Height = Distance(drawing.Points[1], drawing.Points[3]); double rotationAngle = Math.Atan2(drawing.Points[2].Y - drawing.Points[0].Y, drawing.Points[2].X - drawing.Points[0].X); transform.Rotation = rotationAngle * 180.0 / Math.PI; } transform.TranslateX = center.X - ellipse.Width / 2.0d; transform.TranslateY = center.Y - ellipse.Height / 2.0d; ellipse.RenderTransform = transform; ellipse.RenderTransformOrigin = new Point(0.5, 0.5); root.Children.Add(ellipse); drawnShape = ellipse; if (animationToggle.IsChecked == true) { AddAnimation(ellipse); } } else { // we have detected a polygon Polygon polygon = new Polygon(); AttachDragHandlers(polygon); polygon.Fill = newRandomBrush; polygon.Stroke = new SolidColorBrush(Colors.Black); foreach (Point pt in drawing.Points) { polygon.Points.Add(pt); } CompositeTransform transform = new CompositeTransform(); transform.CenterX = drawing.Center.X; transform.CenterY = drawing.Center.Y; polygon.RenderTransform = transform; root.Children.Add(polygon); drawnShape = polygon; if (animationToggle.IsChecked == true) { AddAnimation(polygon); } } } else { // neither ellipse or polygon System.Diagnostics.Debug.WriteLine(inkAnalyzer.AnalysisRoot.Children.Last().Kind.ToString()); } // see if we have a match if (drawnShape != null) { ViewModel.SeekTarget(drawnShape); } } inkCanvas.InkPresenter.StrokeContainer.Clear(); inkAnalyzer.ClearDataForAllStrokes(); }
private async void Recognize_OnClick(object sender, RoutedEventArgs e) { var analyzer = new InkAnalyzer(); var strokes = InkCanvas.InkPresenter.StrokeContainer.GetStrokes(); if (strokes.Count <= 0) { return; } analyzer.AddDataForStrokes(strokes); var results = await analyzer.AnalyzeAsync(); if (results.Status != InkAnalysisStatus.Updated) { return; } // find recognized strokes var textNodes = analyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); // draw all text foreach (InkAnalysisInkWord node in textNodes) { DrawText(node.RecognizedText, node.BoundingRect); foreach (var strokeId in node.GetStrokeIds()) { var stroke = InkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); if (UseSystemFocusVisuals) { stroke.Selected = true; } } analyzer.RemoveDataForStrokes(node.GetStrokeIds()); } InkCanvas.InkPresenter.StrokeContainer.DeleteSelected(); var shapeNodes = analyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); // draw shapes foreach (InkAnalysisInkDrawing node in shapeNodes) { if (node.DrawingKind == InkAnalysisDrawingKind.Drawing) { // unsupported shape continue; } Draw(node); foreach (var strokeId in node.GetStrokeIds()) { var stroke = InkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } analyzer.RemoveDataForStrokes(node.GetStrokeIds()); } InkCanvas.InkPresenter.StrokeContainer.DeleteSelected(); }
/// <summary> /// Analyzes all the strokes collected from the ink presenter /// </summary> private async Task AnalyzeInkAsync() { // Does all the recognition var result = await _inkAnalyzer.AnalyzeAsync(); if (result.Status == InkAnalysisStatus.Updated) { // Filter recognition by shapes. Options inlcude lists, paragraphs, words etc. var drawings = _inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing); foreach (InkAnalysisInkDrawing drawing in drawings) { if (drawing.DrawingKind == InkAnalysisDrawingKind.Rectangle || drawing.DrawingKind == InkAnalysisDrawingKind.Square) { // Swap strokes with border, using details from drawing var border = new Border { Width = drawing.BoundingRect.Width, Height = drawing.BoundingRect.Height, Margin = new Thickness(drawing.BoundingRect.X, drawing.BoundingRect.Y, 0, 0), BorderThickness = new Thickness(new Random((int)DateTime.Now.Ticks).Next(5, 30)), BorderBrush = new SolidColorBrush(Colors.Black), Background = new SolidColorBrush(Colors.WhiteSmoke), IsHitTestVisible = false }; // Using the same composition show hide animation technique ElementCompositionPreview.SetImplicitShowAnimation(border, _fadeIn); ElementCompositionPreview.SetImplicitHideAnimation(border, _fadeOut); Canvas.SetZIndex(border, -1); // Image placeholder var image = new Image { Width = drawing.BoundingRect.Width, Height = drawing.BoundingRect.Height, Margin = new Thickness(drawing.BoundingRect.X, drawing.BoundingRect.Y, 0, 0), Stretch = Stretch.UniformToFill, IsHitTestVisible = false, Visibility = Visibility.Collapsed }; ElementCompositionPreview.SetImplicitShowAnimation(image, _fadeIn); _images.Add(image); LayoutGrid.Children.Add(image); LayoutGrid.Children.Add(border); } } // Same as shapes, we filter by words var inkWords = _inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (InkAnalysisInkWord inkWord in inkWords) { var textBlock = new TextBlock { Text = inkWord.RecognizedText, Margin = new Thickness(inkWord.BoundingRect.X, inkWord.BoundingRect.Y, 0, 0), FontSize = 128, IsHitTestVisible = false }; LayoutGrid.Children.Add(textBlock); } // Remove all strokes from canvas and analyzer. _inkAnalyzer.ClearDataForAllStrokes(); _inkPresenter.StrokeContainer.Clear(); } }
private async Task FreeFormHandWriting() { inkStrokes = InputCanvas.InkPresenter.StrokeContainer.GetStrokes(); // Ensure an ink stroke is present. if (inkStrokes.Count > 0) { inkAnalyzer.AddDataForStrokes(inkStrokes); // In this example, we try to recognizing both // writing and drawing, so the platform default // of "InkAnalysisStrokeKind.Auto" is used. // If you're only interested in a specific type of recognition, // such as writing or drawing, you can constrain recognition // using the SetStrokDataKind method as follows: // foreach (var stroke in strokesText) // { // analyzerText.SetStrokeDataKind( // stroke.Id, InkAnalysisStrokeKind.Writing); // } // This can improve both efficiency and recognition results. inkAnalysisResults = await inkAnalyzer.AnalyzeAsync(); // Have ink strokes on the canvas changed? if (inkAnalysisResults.Status == InkAnalysisStatus.Updated) { // Find all strokes that are recognized as handwriting and // create a corresponding ink analysis InkWord node. var inkwordNodes = inkAnalyzer.AnalysisRoot.FindNodes( InkAnalysisNodeKind.InkWord); // Iterate through each InkWord node. // Draw primary recognized text on recognitionCanvas // (for this example, we ignore alternatives), and delete // ink analysis data and recognized strokes. foreach (InkAnalysisInkWord node in inkwordNodes) { // Draw a TextBlock object on the recognitionCanvas. DrawText(node.RecognizedText, node.BoundingRect); foreach (var strokeId in node.GetStrokeIds()) { var stroke = InputCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } InputCanvas.InkPresenter.StrokeContainer.DeleteSelected(); // Find all strokes that are recognized as a drawing and // create a corresponding ink analysis InkDrawing node. var inkdrawingNodes = inkAnalyzer.AnalysisRoot.FindNodes( InkAnalysisNodeKind.InkDrawing); // Iterate through each InkDrawing node. // Draw recognized shapes on recognitionCanvas and // delete ink analysis data and recognized strokes. foreach (InkAnalysisInkDrawing node in inkdrawingNodes) { if (node.DrawingKind == InkAnalysisDrawingKind.Drawing) { // Catch and process unsupported shapes (lines and so on) here. } // Process generalized shapes here (ellipses and polygons). else { // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse). if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse) { DrawEllipse(node); } // Draw a Polygon object on the recognitionCanvas. else { DrawPolygon(node); } foreach (var strokeId in node.GetStrokeIds()) { var stroke = InputCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } } inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds()); } InputCanvas.InkPresenter.StrokeContainer.DeleteSelected(); } } }
//Recognizes text from ink strokes private async void timer_TickAsync(object sender, object e) { timer.Stop(); if (!analyzerText.IsAnalyzing) { InkAnalysisResult resultText = await analyzerText.AnalyzeAsync(); if (resultText.Status == InkAnalysisStatus.Updated) { var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord); foreach (InkAnalysisInkWord word in words) { if (gameEnded) { NewGame(); } else if (gridResults[currentCanvasNumber] == ' ') { if (isXNext && (word.RecognizedText == "x" || word.RecognizedText == "X")) { DrawText(word.RecognizedText, word.BoundingRect); gridResults[currentCanvasNumber] = 'x'; isXNext = !isXNext; Info.Text = "Now it is Player 2's turn. Draw an 'O' in any remaining square."; } else if (!isXNext && (word.RecognizedText == "o" || word.RecognizedText == "O" || word.RecognizedText == "0")) { DrawText(word.RecognizedText, word.BoundingRect); gridResults[currentCanvasNumber] = 'o'; isXNext = !isXNext; Info.Text = "Now it is Player 1's turn. Draw an 'X' in any remaining square."; } } foreach (var strokeId in word.GetStrokeIds()) { var stroke = inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.GetStrokeById(strokeId); stroke.Selected = true; } analyzerText.RemoveDataForStrokes(word.GetStrokeIds()); } inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.DeleteSelected(); } else { timer.Start(); } analyzerText.ClearDataForAllStrokes(); checkForWinner(); } }