Exemplo n.º 1
0
        public Scenario2()
        {
            this.InitializeComponent();

            inkPresenter = inkCanvas.InkPresenter;
            inkPresenter.StrokesCollected          += InkPresenter_StrokesCollected;
            inkPresenter.StrokesErased             += InkPresenter_StrokesErased;
            inkPresenter.StrokeInput.StrokeStarted += StrokeInput_StrokeStarted;

            // We exclude CoreInputDeviceTypes.Touch because we use touch to select paragraphs.
            inkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Mouse;

            inkCanvas.Tapped       += InkCanvas_Tapped;
            inkCanvas.DoubleTapped += InkCanvas_DoubleTapped;

            inkAnalyzer       = new InkAnalyzer();
            paragraphSelected = null;

            dispatcherTimer       = new DispatcherTimer();
            dispatcherTimer.Tick += DispatcherTimer_Tick;

            // We perform analysis when there has been a change to the
            // ink presenter and the user has been idle for 1 second.
            dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
        }
Exemplo n.º 2
0
        private string ExtractTextFromParagraph(InkAnalysisParagraph paragraph)
        {
            // The paragraph.RecognizedText property also returns the text,
            // but manually walking through the lines allows us to preserve
            // line breaks.
            var lines = new List <string>();

            foreach (var child in paragraph.Children)
            {
                if (child.Kind == InkAnalysisNodeKind.Line)
                {
                    var line = (InkAnalysisLine)child;
                    lines.Add(line.RecognizedText);
                }
                else if (child.Kind == InkAnalysisNodeKind.ListItem)
                {
                    var listItem = (InkAnalysisListItem)child;
                    lines.Add(listItem.RecognizedText);
                }
            }
            return(String.Join("\n", lines));
        }
Exemplo n.º 3
0
        private void InkCanvas_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            // Convert the selected paragraph or list item
            if (paragraphSelected != null)
            {
                Rect rect = paragraphSelected.BoundingRect;
                var  text = ExtractTextFromParagraph(paragraphSelected);

                if ((rect.X > 0) && (rect.Y > 0) && (text != string.Empty))
                {
                    // Create text box with recognized text
                    var textBlock = new TextBlock();
                    textBlock.Text      = text;
                    textBlock.MaxWidth  = rect.Width;
                    textBlock.MaxHeight = rect.Height;
                    Canvas.SetLeft(textBlock, rect.X);
                    Canvas.SetTop(textBlock, rect.Y);

                    // Remove strokes from InkPresenter
                    IReadOnlyList <uint> strokeIds = paragraphSelected.GetStrokeIds();
                    foreach (var strokeId in strokeIds)
                    {
                        var stroke = inkPresenter.StrokeContainer.GetStrokeById(strokeId);
                        stroke.Selected = true;
                    }
                    inkPresenter.StrokeContainer.DeleteSelected();

                    // Remove strokes from InkAnalyzer
                    inkAnalyzer.RemoveDataForStrokes(strokeIds);

                    // Hide the SelectionRect
                    SelectionRect.Visibility = Visibility.Collapsed;

                    canvas.Children.Add(textBlock);
                    paragraphSelected = null;
                }
            }
        }
Exemplo n.º 4
0
        private void InkCanvas_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var position = e.GetPosition(inkCanvas);

            var paragraph = FindHitParagraph(position);

            if (paragraph == null)
            {
                // Did not tap on a paragraph.
                SelectionRect.Visibility = Visibility.Collapsed;
            }
            else
            {
                // Show the selection rect at the paragraph's bounding rect.
                Rect rect = paragraph.BoundingRect;
                SelectionRect.Width  = rect.Width;
                SelectionRect.Height = rect.Height;
                Canvas.SetLeft(SelectionRect, rect.Left);
                Canvas.SetTop(SelectionRect, rect.Top);

                SelectionRect.Visibility = Visibility.Visible;
            }
            paragraphSelected = paragraph;
        }