Example #1
0
        void _inkCanvas_StrokeCollected_StrokeToImage(object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            InkCanvas inkCanvas = (InkCanvas)sender;

            if (analyze.RootNode.Strokes.IndexOf(inkCanvas.Strokes[inkCanvas.Strokes.Count - 1]) == -1)
            {
                analyze.AddStroke(inkCanvas.Strokes[inkCanvas.Strokes.Count - 1]);
            }
            analyze.BackgroundAnalyze();
            string analyzeStr = analyze.GetRecognizedString();
            //Stream streamOpen = new FileStream("pack://siteoforigin:,,,/xml/StrokeToImage.xml", FileMode.Open);//提示“不支持指定格式”
            Stream streamOpen = new FileStream(GlobalValues.FilesPath + "/WPFInk/WPFInk/bin/Debug/xml/StrokeToImage.xml", FileMode.Open);

            using (System.IO.StreamReader streamReader = new System.IO.StreamReader(streamOpen))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                XmlReader         Reader   = XmlReader.Create(streamReader, settings);
                while (Reader.Read())
                {
                    if (Reader.NodeType == XmlNodeType.Element)
                    {
                        if (Reader.Name == "Image")
                        {
                            Reader.MoveToAttribute("stroke");
                            string strokeStr = Reader.Value;
                            Reader.MoveToAttribute("imageName");
                            searchStrokes(strokeStr, Reader.Value, inkCanvas);
                        }
                    }
                }
            }
        }
Example #2
0
        private void InkAnalyzer_ResultsUpdated(object sender, ResultsUpdatedEventArgs e)
        {
            running = false;

            InkUtils.MergeParagraphs(inkAnalyzer);

            if (processor < inkProcessors.Count)
            {
                InkProcessor proc = inkProcessors[processor];
                proc.process(inkAnalyzer);
                processor++;
                fireoff();
                return;
            }

            if (adds.Count > 0 || removes.Count > 0)
            {
                //Flush adds and removes
                while (adds.Count > 0)
                {
                    Stroke stroke = adds.Dequeue();
                    inkAnalyzer.AddStroke(stroke);
                }
                while (removes.Count > 0)
                {
                    Stroke stroke = removes.Dequeue();
                    inkAnalyzer.RemoveStroke(stroke);
                }
                fireoff();
                return;
            }

            if (PipelineComplete != null)
            {
                PipelineComplete(this, new EventArgs());
            }

            processor  = 0;
            processing = false;

            if (queued)
            {
                queued     = false;
                processor  = 0;
                processing = true;
                fireoff();
            }
        }
        /// <summary>
        /// 识别
        /// </summary>
        /// <param name="strokes">笔迹集合</param>
        /// <returns>候选词数组</returns>
        public string[] Recognize(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
            {
                return(Params.EmptyAlternates);
            }

            var stroke = GetCombinedStore(strokes);

            var analyzer = new InkAnalyzer();

            analyzer.AddStroke(stroke, Params.SimplifiedChineseLanguageId);
            analyzer.SetStrokeType(stroke, StrokeType.Writing);

            var status = analyzer.Analyze();

            if (status.Successful)
            {
                var result = analyzer.GetAlternates()
                             .OfType <AnalysisAlternate>()
                             .Select(x => x.RecognizedString)
                             .ToArray();
                analyzer.Dispose();
                return(result);
            }

            analyzer.Dispose();

            return(Params.EmptyAlternates);
        }
Example #4
0
        private void theInkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            var stroke = GetCombinedStore(this.theInkCanvas.Strokes);

            InkAnalyzer theInkAnalyzer = new InkAnalyzer();

            //theInkAnalyzer.AddStrokes(this.theInkCanvas.Strokes, 0x0804);
            //theInkAnalyzer.SetStrokesType(this.theInkCanvas.Strokes, StrokeType.Writing);

            theInkAnalyzer.AddStroke(stroke, 0x0804);
            theInkAnalyzer.SetStrokeType(stroke, StrokeType.Writing);
            AnalysisStatus status = theInkAnalyzer.Analyze();

            if (status.Successful)
            {
                List <StrokeWord> slist = new List <StrokeWord>();
                //textBox1.Text = theInkAnalyzer.GetRecognizedString();
                for (int i = 0; i < theInkAnalyzer.GetAlternates().Count; i++)
                {
                    StrokeWord sw = new StrokeWord();
                    sw.StrokeName = theInkAnalyzer.GetAlternates()[i].RecognizedString;
                    slist.Add(sw);
                }

                this.StrokeWordList = new List <StrokeWord>();
                this.StrokeWordList = slist;
            }
            else
            {
                MessageBox.Show("识别失败");
            }
        }
Example #5
0
        void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
        {
            foreach (Stroke stroke in e.Added)
            {
                if (!graphAnalyzer.newStroke(stroke))
                {
                    inkAnalyzer.AddStroke(stroke);
                }

                AutocorrectHandleAddStroke(stroke);
            }

            double ymax = InkUtils.StrokeYMax(e.Added);

            if (ymax > MainInkCanvas.ActualHeight - 300.0)
            {
                MainInkCanvas.Height = ymax + 800.0;
            }

            foreach (Stroke stroke in e.Removed)
            {
                graphAnalyzer.removeStroke(stroke);
                // If we erase a word and try to replace it with autocorrect
                // suggestions, there's no good way to define the behavior
                // so just hide the suggestions.
                suggestionsBox.Visibility = Visibility.Collapsed;

                inkAnalyzer.RemoveStroke(stroke);
            }
        }
Example #6
0
 private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
 {
     dispatcherTimer.Stop();
     m_analyzer.AddStroke(e.Stroke);
     dispatcherTimer.Start();
     Console.WriteLine("START");
 }
Example #7
0
        /// <summary>
        /// 识别(将多个笔画集合成一起识别,提高单字的识别率)
        /// </summary>
        /// <param name="strokes">笔迹集合</param>
        /// <returns>候选词数组</returns>
        public string[] Recognize(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
            {
                return(null);
            }

            var stroke = GetCombinedStore(strokes);

            var analyzer = new InkAnalyzer();

            analyzer.AddStroke(stroke, 0x0804);
            analyzer.SetStrokeType(stroke, StrokeType.Writing);

            var status = analyzer.Analyze();

            if (status.Successful)
            {
                return(analyzer.GetAlternates()
                       .OfType <AnalysisAlternate>()
                       .Select(x => x.RecognizedString)
                       .ToArray());
            }

            analyzer.Dispose();

            return(null);
        }
Example #8
0
        static public bool IsChar(this Stroq stroke, string charset)
        {
            System.Windows.Ink.InkAnalyzer ia = new InkAnalyzer();
            ia.AddStroke(stroke.BackingStroke);
            //AnalysisHintNode node = ia.CreateAnalysisHint();
            //node.Factoid = "IS_ONECHAR";
            //node.CoerceToFactoid = true;
            //node.Location.MakeInfinite();
            AnalysisStatus astat = ia.Analyze();
            string         reco  = ia.GetRecognizedString();

            Console.WriteLine("Recognized:<" + reco + ">");
            if (astat.Successful && reco != "Other")
            {
                return(charset.Contains(reco[0]));
            }
            return(false);
        }
Example #9
0
        private static bool strokeIsHorizontalLine(Stroke stroke)
        {
            InkAnalyzer temp = new InkAnalyzer();

            temp.AddStroke(stroke);
            temp.Analyze();
            ContextNode node = temp.RootNode.SubNodes[0];

            if (node is InkDrawingNode)
            {
                InkDrawingNode  drawing     = node as InkDrawingNode;
                PointCollection boundingBox = drawing.GetRotatedBoundingBox();

                double d1 = distSquared(boundingBox[0], boundingBox[1]);
                double d2 = distSquared(boundingBox[1], boundingBox[2]);
                return((d2 > 0 && d1 / d2 > 10 * 10 && d1 > 100 && Math.Abs(slope(boundingBox[0], boundingBox[1])) < 0.5) ||
                       (d1 > 0 && d2 / d1 > 10 * 10 && d2 > 100 && Math.Abs(slope(boundingBox[1], boundingBox[2])) < 0.5));
            }
            return(false);
        }
Example #10
0
    /// <summary>
    /// InkCanvas.StrokeCollected event handler.  Begins
    /// ink analysis and starts the timer to clear the strokes.
    /// If five seconds pass without a Stroke being added,
    /// the strokes on the InkCanvas will be cleared.
    /// </summary>
    /// <par    am name="sender">InkCanvas that raises the
    /// StrokeCollected event.</param>
    /// <param name="args">Contains the event data.</param>
    private void RestartAnalysis(object sender,
                                 InkCanvasStrokeCollectedEventArgs args)
    {
        // If strokeRemovalTimer is enabled, stop it.
        if (strokeRemovalTimer != null && strokeRemovalTimer.IsEnabled)
        {
            strokeRemovalTimer.Stop();
        }

        // Restart the timer to clear the strokes in five seconds
        strokeRemovalTimer = new DispatcherTimer(
            TimeSpan.FromSeconds(CLEAR_STROKES_DELAY),
            DispatcherPriority.Normal,
            ClearCanvas,
            Dispatcher.CurrentDispatcher);

        // Add the new stroke to the InkAnalyzer and
        // begin background analysis.
        analyzer.AddStroke(args.Stroke);
        analyzer.BackgroundAnalyze();
    }
Example #11
0
        /// <summary>
        /// Analyze a new stroke.
        /// </summary>
        /// <param name="stroke"></param>
        /// <returns>true if the stroke was recognized as belonging to a graph (and so should be excluded from InkAnalyzer)</returns>
        public bool newStroke(Stroke stroke)
        {
            if (strokes.ContainsKey(stroke))
            {
                // already have it!
                return(true);
            }
            foreach (Graph g in graphs)
            {
                if (g.containsStroke(stroke) && g.takeStroke(stroke))
                {
                    strokes.Add(stroke, g);
                    return(true);
                }
            }
            Stroke copy = stroke.Clone();

            analyzer.AddStroke(copy);

            analyzer.Analyze();

            StrokeCollection      sc       = new StrokeCollection(new Stroke[] { copy });
            ContextNodeCollection ctxNodes = analyzer.FindInkLeafNodes(sc);

            foreach (ContextNode ctxNode in ctxNodes)
            {
                if (ctxNode is InkDrawingNode && (ctxNode as InkDrawingNode).GetShapeName() == "Rectangle")
                {
                    Graph g = new XYGraph(this, stroke);
                    graphs.Add(g);
                    strokes.Add(stroke, g);
                    analyzer.RemoveStroke(copy);
                    return(true);
                }
            }
            analyzer.RemoveStroke(copy);
            return(false);
        }
Example #12
0
        public override void _presenter_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            InkCanvas inkCanvas  = (InkCanvas)sender;
            Stroke    lastStroke = inkCanvas.Strokes[inkCanvas.Strokes.Count - 1];

            if (analyze.RootNode.Strokes.IndexOf(lastStroke) == -1)
            {
                analyze.AddStroke(inkCanvas.Strokes[inkCanvas.Strokes.Count - 1]);
            }
            analyze.BackgroundAnalyze();
            string    analyzeStr = analyze.GetRecognizedString();
            MyGraphic MyGraphicContrainStroke = _inkCollector.Sketch.getMyGraphicContrainStroke(lastStroke);

            if (MyGraphicContrainStroke != null)
            {
                if (MyGraphicContrainStroke.textStrokeCollection.IndexOf(lastStroke) == -1)
                {
                    MyGraphicContrainStroke.addTextStroke(lastStroke);
                    MyGraphicContrainStroke.Text = analyzeStr;
                    //Console.WriteLine("analyzeStr:" + analyzeStr);
                }
            }
        }
 private void CanvasTinta_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
 {
     m_analyzer.AddStroke(e.Stroke);
 }
 /// <summary>
 /// When a stroke is collected, add it to the InkAnalyzer.
 /// After a Stroke has been added, it will be analyzed
 /// the next time BackgroundAnalyze or Analyze is called.
 /// </summary>
 private void MyInkOverlayStroke(object sender, InkCollectorStrokeEventArgs e)
 {
     analyzer.AddStroke(e.Stroke);
 }
Example #15
0
        public void Populate(ICollection <Recognition> recogs, StroqCollection stroqs)
        {
            alternateRec = recogs.Count == 1 ? recogs.Single() : null;
            if (alternateRec != null && !stroqs.All((s) => alternateRec.strokes.Contains(_mrec.Sim[s])))
            {
                alternateRec = null;
            }
            altstroqsRec = stroqs;
            altrecogsRec = recogs;

            _menuShell.Items.Clear();

            MenuItem mi;
            bool     needseparator = false;

            if (recogs.Count == 1)
            {
                /* regular alternates*/
                Recognition rr = recogs.Single();
                for (int i = 0; i < rr.alts.Length; i++)
                {
                    string label;
                    char   c = rr.alts[i].Character;
                    if (c != 0)
                    {
                        label = c.ToString();
                    }
                    else
                    {
                        label = rr.alts[i].Word;
                        if (label == null)
                        {
                            label = rr.alts[i].ToString();
                            label = label.Substring(1, label.Length - 2);
                        }
                    }
                    mi        = new MenuItem();
                    mi.Header = label;
                    if (c != 0)
                    {
                        mi.ToolTip    = Unicode.NameOf(c);
                        mi.FontFamily = new FontFamily(starPadSDK.MathExpr.ExprWPF.EDrawingContext.FontFamilyURIBase,
                                                       starPadSDK.MathExpr.ExprWPF.EDrawingContext.FontFamilyURIRel);
                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= 'α' && c <= 'ω') || c == Unicode.G.GREEK_PHI_SYMBOL)
                        {
                            mi.FontStyle = FontStyles.Italic;
                        }
                    }
                    else
                    {
                        mi.ToolTip = label;
                    }
                    mi.Tag               = i;
                    mi.Click            += ChooseAlternate;
                    mi.PreviewMouseDown += mi_MouseDown;
                    mi.PreviewMouseUp   += mi_MouseUp;
                    if (i == 0)
                    {
                        mi.AllowDrop  = true;
                        mi.DragEnter += mi_DragCheck;
                        mi.DragOver  += mi_DragCheck;
                        mi.Drop      += mi_Drop;
                    }
                    if (rr.curalt == i)
                    {
                        mi.IsChecked = true;
                    }
                    _menuShell.Items.Add(mi);
                }
                needseparator = true;
            }
            if (stroqs.Count > 1 && stroqs.Count != recogs.Count /* FIXME: if a stroke has no recog, this won't work */)
            {
                /* option to split apart and recognize each stroke separately */
                if (needseparator)
                {
                    _menuShell.Items.Add(new Separator());
                    needseparator = false;
                }
                string label = "";
                foreach (Stroq s1 in stroqs)
                {
                    Recognition r = _mrec.Charreco.Classify(_mrec.Sim[s1], true);
                    if (r == null)
                    {
                        continue;
                    }
                    string l;
                    char   c = r.alt.Character;
                    if (c != 0)
                    {
                        l = c.ToString();
                    }
                    else
                    {
                        l = r.alt.Word;
                        if (l == null)
                        {
                            l = r.alt.ToString();
                            l = l.Substring(1, l.Length - 2);
                        }
                    }
                    label += l;
                }
                mi         = new MenuItem();
                mi.Header  = label;
                mi.ToolTip = "split combined symbol into separate symbols";
                mi.Tag     = -1;
                mi.Click  += ChooseAlternate;
                _menuShell.Items.Add(mi);
                needseparator = true;
            }
            if (stroqs.Count > 0)
            {
                /* Interpret everything as a single word */
                if (needseparator)
                {
                    _menuShell.Items.Add(new Separator());
                    needseparator = false;
                }
                InkAnalyzer      ia  = new InkAnalyzer();
                AnalysisHintNode ahn = ia.CreateAnalysisHint();
                ahn.WordMode = true;
                ahn.Location.MakeInfinite();
                foreach (Stroq s in stroqs)
                {
                    ia.AddStroke(s.BackingStroke);
                }
                AnalysisStatus stat = ia.Analyze();
                if (stat.Successful)
                {
                    AnalysisAlternateCollection aac = ia.GetAlternates();
                    for (int i = 0; i < aac.Count; i++)
                    {
                        if (aac[i].AlternateNodes.Count > 1 || !(aac[i].AlternateNodes[0] is InkWordNode))
                        {
                            continue;
                        }
                        mi         = new MenuItem();
                        mi.Header  = aac[i].RecognizedString;
                        mi.ToolTip = "interpret all selected strokes as a single character or word: alternate " + (i + 1);
                        mi.Tag     = aac[i];
                        mi.Click  += ChooseAlternate;
                        if (alternateRec != null)
                        {
                            mi.PreviewMouseDown += mi_MouseDown;
                            mi.PreviewMouseUp   += mi_MouseUp;
                        }
                        if (aac[i].RecognizedString.Length == 1)
                        {
                            char c = aac[i].RecognizedString[0];
                            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= 'α' && c <= 'ω'))
                            {
                                mi.FontStyle = FontStyles.Italic;
                            }
                            mi.ToolTip = (string)mi.ToolTip + " (" + Unicode.NameOf(c) + ")";
                        }
                        _menuShell.Items.Add(mi);
                    }
                }
            }
            _menuShell.InvalidateMeasure(); // odd that I need this
        }
Example #16
0
        private static bool strokeIsHorizontalLine(Stroke stroke)
        {
            InkAnalyzer temp = new InkAnalyzer();
            temp.AddStroke(stroke);
            temp.Analyze();
            ContextNode node = temp.RootNode.SubNodes[0];
            if (node is InkDrawingNode)
            {
                InkDrawingNode drawing = node as InkDrawingNode;
                PointCollection boundingBox = drawing.GetRotatedBoundingBox();

                double d1 = distSquared(boundingBox[0], boundingBox[1]);
                double d2 = distSquared(boundingBox[1], boundingBox[2]);
                return (d2 > 0 && d1 / d2 > 10 * 10 && d1 > 100 && Math.Abs(slope(boundingBox[0], boundingBox[1])) < 0.5)
                    || (d1 > 0 && d2 / d1 > 10 * 10 && d2 > 100 && Math.Abs(slope(boundingBox[1], boundingBox[2])) < 0.5);
            }
            return false;
        }