Temel Mürekkep Analizi Örneği

Temel Mürekkep Analizi örneği, InkAnalyzer sınıfının mürekkebi çeşitli sözcük ve çizim bölümlerine nasıl böldüğünü gösterir.

Bu örnek, Mürekkep Ayırıcı Örneğigüncelleştirilmiş bir sürümüdür. Mürekkep Ayırıcı Örneği Bölücü sınıfını kullanırken, bu örnek daha yeni ve tercih edilen InkAnalysis API'sini kullanır. InkAnalysis API'RecognizerContext ve Divider'ı tek bir API'de birleştirir ve her ikisinin de işlevselliğini genişletir.

Formu güncelleştirdiğinizde örnek, analiz edilen her birimin etrafına bir dikdörtgen çizer: sözcükler, satırlar, paragraflar, yazma bölgeleri, çizimler ve madde işaretleri. Form, farklı birimler için farklı renkler kullanır. Dikdörtgenlerin hiçbiri diğerleri tarafından gizlenmesin diye dikdörtgenler de farklı miktarlarda büyütülür.

Aşağıdaki tabloda, analiz edilen her birim için renk ve büyütme belirtmektedir.

Analiz edilen birim Dikdörtgenin rengi Dikdörtgen büyütme (piksel)
Kelime
Yeşil
1
Satır
Macenta
3
Paragraf
Mavi
5
Yazı bölgesi
Sarı
7
Çizim
Kırmızı
1
Mermi
Portakal
1

Formdaki vuruşları silebilirsiniz. Örnek uygulamada, kalemin işlevini değiştirmek için Mürekkep ve Silme modu arasında geçiş yapabilirsiniz.

        private void miInk_Click(object sender, System.EventArgs e)
        {
            // Turn on the inking mode
            myInkOverlay.EditingMode = InkOverlayEditingMode.Ink;

            // Update the state of the Ink and Erase menu items
            miInk.Checked = true;
            miErase.Checked = false;

            // Update the UI
            this.Refresh();
        }

        private void miErase_Click(object sender, System.EventArgs e)
        {
            // Turn on the ink deletion mode
            myInkOverlay.EditingMode = InkOverlayEditingMode.Delete;

            // Update the state of the Ink and Erase menu items
            miInk.Checked = false;
            miErase.Checked = true;

            // Update the UI
            this.Refresh();
        }

Vuruş eklediğinizde veya sildiğinizde, örnekler InkAnalyzertarafından güncellenir.

        private void myInkOverlay_Stroke(object sender, InkCollectorStrokeEventArgs e)
        {
            // Filter out the eraser stroke.
            if (InkOverlayEditingMode.Ink == myInkOverlay.EditingMode)
            {

                // Add the new stroke to the InkAnalyzer's stroke collection
                myInkAnalyzer.AddStroke(e.Stroke);

                if (miAutomaticLayoutAnalysis.Checked)
                {
                    // Invoke an analysis operation on the background thread
                    myInkAnalyzer.BackgroundAnalyze();
                }
            }
        }

        void myInkOverlay_StrokeDeleting(object sender, InkOverlayStrokesDeletingEventArgs e)
        {
            // Remove the strokes to be deleted from the InkAnalyzer's stroke collection
            myInkAnalyzer.RemoveStrokes(e.StrokesToDelete);

            // If automatic layout analysis is turned on, analyze the ink on the background thread
            if ( miAutomaticLayoutAnalysis.Checked )
            {
                // Invoke an analysis operation on the background thread
                myInkAnalyzer.BackgroundAnalyze ( );
            }
        }

Mod menüsünde Otomatik Düzen Analizi'nin varsayılan olarak açık olduğuna dikkat edin. Bu seçenek seçiliyken, InkOverlay nesnesinin Çizgi ve StrokesDeleting olay işleyicileri, her çizgi oluşturulduğunda veya silindiğinde BackgroundAnalyze yöntemini çağırır.

Not

InkAnalyzer nesnesinin Çözümle yöntemi, birkaç çizgiden fazlası mevcutken çağrıldığında uygulamada belirgin bir gecikmeye neden olur. Bunun nedeni Çözümle'nin zaman uyumlu bir mürekkep analizi işlemi olmasıdır. Uygulamada, Çözümle yöntemini yalnızca sonuca ihtiyacınız olduğunda çağırın. Aksi takdirde örnekte gösterildiği gibi zaman uyumsuz BackgroundAnalyze yöntemini kullanın.

Çözümleme Sonuçlarını İşleme

Örnek, çeşitli dikdörtgenleri yatay veya döndürülmüş halde tutmak için iki dizi oluşturur. Bir sözcük satırının yazıldığı açıyı elde etmek için döndürülmüş sınırlayıcı kutu kullanın. Örnek, InkAnalyzer tarafından geri döndürülen özellikleri gösterir ve sınırlayıcı kutuyu veya döndürülmüş sınırlayıcı kutuyu (menü seçimine bağlı olarak) görüntüler.

      private Rectangle[] GetHorizontalBBoxes(Guid nodeType, int inflate)
        {
            // Declare the array of rectangles to hold the result
            Rectangle[] analysisRects;

            // Get the division units from the division result of division type
            ContextNodeCollection nodes = myInkAnalyzer.FindNodesOfType(nodeType);

            // If there is at least one unit, we construct the rectangles
            if ((null != nodes) && (0 < nodes.Count))
            {
                // We need to convert rectangles from ink units to
                // pixel units. For that, we need Graphics object
                // to pass to InkRenderer.InkSpaceToPixel method
                using (Graphics g = drawArea.CreateGraphics())
                {
                    // Construct the rectangles
                    analysisRects = new Rectangle[nodes.Count];

                    // InkRenderer.InkSpaceToPixel takes Point as parameter. 
                    // Create two Point objects to point to (Top, Left) and
                    // (Width, Height) properties of rectangle. (Width, Height)
                    // is used instead of (Right, Bottom) because (Right, Bottom)
                    // are read-only properties on Rectangle
                    Point ptLocation = new Point();
                    Point ptSize = new Point();

                    // Index into the bounding boxes
                    int i = 0;

                    // Iterate through the collection of division units to obtain the bounding boxes
                    foreach (ContextNode node in nodes)
                    {
                        // Get the bounding box of the strokes of the division unit
                        analysisRects[i] = node.Location.GetBounds();

                        // The bounding box is in ink space unit. Convert them into pixel unit. 
                        ptLocation = analysisRects[i].Location;
                        ptSize.X = analysisRects[i].Width;
                        ptSize.Y = analysisRects[i].Height;

                        // Convert the Location from Ink Space to Pixel Space
                        myInkOverlay.Renderer.InkSpaceToPixel(g, ref ptLocation);

                        // Convert the Size from Ink Space to Pixel Space
                        myInkOverlay.Renderer.InkSpaceToPixel(g, ref ptSize);

                        // Assign the result back to the corresponding properties
                        analysisRects[i].Location = ptLocation;
                        analysisRects[i].Width = ptSize.X;
                        analysisRects[i].Height = ptSize.Y;

                        // Inflate the rectangle by inflate pixels in both directions
                        analysisRects[i].Inflate(inflate, inflate);

                        // Increment the index
                        ++i;
                    }

                } // Relinquish the Graphics object
            }
            else
            {
                // Otherwise we return null
                analysisRects = null;
            }

            // Return the Rectangle[] object
            return analysisRects;
        }

        private System.Collections.ArrayList GetRotatedBBoxes(Guid nodeType, int inflate)
        {
            //Find the correct collection of results nodes.
            ContextNodeCollection Nodes = myInkAnalyzer.FindNodesOfType(nodeType);

            // Declare the array list to hold the results; 
            // This array represents the four points of a rectangle, with the first point
            // copied again to complete the cycle of points.

            ArrayList polygonPoints = new ArrayList(Nodes.Count);

            // Cycle through each results node, get and convert the
            // rotated bounding box points
            foreach (ContextNode node in Nodes)
            {
                //Declare the point array
                Point[] rotatedBoundingBox = null;

                //Switch on the type of ContextNode to cast into the
                //appropriate type.  This is required to access the 
                //type specific property "RotatedBoundingBox" which
                //is not found on all ContextNode types.
                if (nodeType == ContextNodeType.InkWord)
                {
                    rotatedBoundingBox = ((InkWordNode)node).GetRotatedBoundingBox();
                }
                else if (nodeType == ContextNodeType.Line)
                {
                    rotatedBoundingBox = ((LineNode)node).GetRotatedBoundingBox();
                }
                else if (nodeType == ContextNodeType.Paragraph)
                {
                    rotatedBoundingBox = ((ParagraphNode)node).GetRotatedBoundingBox();
                }
                else if (nodeType == ContextNodeType.WritingRegion ||
                         nodeType == ContextNodeType.InkDrawing ||
                         nodeType == ContextNodeType.InkBullet )
                {

                    // Rotated Bounding Boxes are not a supported option for 
                    // Writing Regions or Drawings.  We return the axis aligned 
                    // bounding box instead
                    Rectangle rect = node.Location.GetBounds();

                    // We need to create a looped list of 4 points to be consistent
                    // with the way InkAnalysis represents rotated bounding boxes.  
                    rotatedBoundingBox = new Point[4];

                    rotatedBoundingBox[0] = new Point(rect.X, rect.Y);
                    rotatedBoundingBox[1] = new Point(rect.Right, rect.Y);
                    rotatedBoundingBox[2] = new Point(rect.Right, rect.Bottom);
                    rotatedBoundingBox[3] = new Point(rect.X, rect.Bottom);

                }


                if (null != rotatedBoundingBox)
                {
                    // We need to convert rectangles from ink units to
                    // pixel units. For that, we need Graphics object
                    // to pass to InkRenderer.InkSpaceToPixel method
                    using (Graphics g = drawArea.CreateGraphics())
                    {
                        // convert each of the points from ink space to pixel space
                        for (int i = 0; i < rotatedBoundingBox.Length; i++)
                        {
                            myInkOverlay.Renderer.InkSpaceToPixel(g, ref rotatedBoundingBox[i]);
                        }

                        //inflate the points by calling helper method
                        InflateHelperMethod(ref rotatedBoundingBox, inflate);

                        // increment the node portion of the polygonPoints array
                        polygonPoints.Add(rotatedBoundingBox);
                    }
                }
            }
            //Return the results
            return polygonPoints;
        }

Ayrıştırıcı, analiz sırasında GetRotatedBoundingBox hesaplar. Çeşitli yararlı nedenlerle, uygulamanızdaki döndürülmüş sınırlayıcı kutulardan bilgilere erişebilirsiniz:

  • Tek bir satır, paragraf veya başka bir birimin sınırlarını algılayın veya çizin.
  • Satır veya paragrafın yazıldığı açıyı belirleyin.
  • Satır, paragraf veya başka bir birim için seçim gibi özellikleri uygulayın.

Temel Tanıma ve Mürekkep Analizi

Taranmış Kağıt Formu Örneği

Mürekkep Ayırıcı Örneği