Ejemplo n.º 1
0
        //-------------------------------------------------------------------------
        private void Init()
        {
            ImagePanel.AutoScroll = false;
            LoadSettings();
            editorTools = new Control[]
            { this.RotateLeftButton
            , this.RotateRightButton
            , this.btnRemoveNoise
            , this.rbtnRotate
            //, this.splitBtnRestore
            , this.rbtnClear
            , this.rbtnCut
            , this.btnInvert
            };

            FileInfo fi = new FileInfo(Path.Combine(OcrAppConfigDefaults.BaseTempFolder, "testUids.json"));
            if (fi.Exists)
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                //Dictionary<string, eDoctrinaOcrEd.TestUids.AreaSettings[]> temp;
                try
                {
                    //temp = js.Deserialize<Dictionary<string, eDoctrinaOcrEd.TestUids.AreaSettings[]>>("testUids.json");//testUids
                    string s = File.ReadAllText(Path.Combine(OcrAppConfigDefaults.BaseTempFolder, "testUids.json"));
                    testUids = js.Deserialize<TestUids>(s);
                    if (testUids == null)
                    {
                        testUids = new TestUids();
                        testUids.Test = new Dictionary<string, eDoctrinaOcrEd.TestUids.AreaSettings[]>();
                    }
                }
                catch (Exception)
                {
                    testUids = new TestUids();
                    //testUids.Test = new Dictionary<TestUids.TestUid, RegionsArea[]>();
                    testUids.Test = new Dictionary<string, eDoctrinaOcrEd.TestUids.AreaSettings[]>();
                }
            }
            else
            {
                testUids = new TestUids();
                testUids.Test = new Dictionary<string, eDoctrinaOcrEd.TestUids.AreaSettings[]>();
            }
            defaults = new OcrAppConfig();
            if (defaults.exception != null)
            {
                UpdateUI("Exception", defaults.exception.Message);
                this.Close();
                return;
            }
            defaults.DualControl = true;
            if (!VerifyDirectory(defaults.ManualConfigsFolder) || Directory.GetFiles(defaults.ManualConfigsFolder, "*.json").Length == 0)
            {
                string message = "Config files not found. The program will be closed.";
                log.LogMessage(message);
                UpdateUI("Exception", message);
                this.Close();
                return;
            }
            if (!string.IsNullOrEmpty(OcrAppConfigDefaults.BaseTempFolder))
            {
                VerifyDirectory(OcrAppConfig.TempEdFolder);
            }
            BoxSheet.Text = "Sheet identifier";
            BoxSheet.DropDownStyle = ComboBoxStyle.DropDownList;

            AddMiniatureControlsAndSheetIdentifiers();
            InitButtonsAndControls();
        }
Ejemplo n.º 2
0
        //-------------------------------------------------------------------------
        void btnOk_Click(object sender, EventArgs e)
        {
            Status = StatusMessage.NULL;
            try
            {
                RecognizeBubblesButton.Enabled = false;
                for (int i = 0; i < factAreasManualSet.Length; i++)
                {
                    if (!factAreasManualSet[i])
                    {
                        int x = (int)Math.Round(factAreas[i].left * Zoom);
                        int y = (int)Math.Round(factAreas[i].top * Zoom);
                        areaNaturalSize = allAreasNaturalSize[i];
                        pictureBox1_MouseUp(null, new MouseEventArgs(MouseButtons.Left, 0, x, y, 0));
                        factAreasManualSet[i] = false;
                    }
                }
                eDoctrinaOcrEd.TestUids testUid = new TestUids();
                string testUidAmoutOfQuestions = "", testUidIndexOfFirstQuestion = "";
                for (int i = 0; i < barCodeList.ControlList.Count; i++)
                {
                    BarCodeListItemControl item = (BarCodeListItemControl)barCodeList.ControlList[i];
                    //}
                    //foreach (BarCodeListItemControl item in barCodeList.ControlList)
                    //{
                    RecognizeBubblesButton.Enabled = false;
                    if (item.Name == "amout_of_questions")
                    {
                        amout_of_questionsIndex = i;
                        if (string.IsNullOrEmpty(item.comboBox1.Text) || !utils.IsNumeric(item.comboBox1.Text))
                        {
                            if (factLinesPerArea[0] == 0)
                            {
                                item.btnCheck.PerformClick();
                                return;
                            }
                            item.comboBox1.Text = factLinesPerArea.Sum().ToString();
                            if (errList.Contains("Error in " + item.Name))
                            {
                                errList.Remove("Error in " + item.Name);
                                VerifyErrList();
                            }
                            break;
                        }
                        else
                        {
                            testUidAmoutOfQuestions = item.comboBox1.Text;
                        }
                    }
                    else if (item.Name == "index_of_first_question" || item.Name == "question_number_1")
                    {
                        if (string.IsNullOrEmpty(item.comboBox1.Text) || !utils.IsNumeric(item.comboBox1.Text))
                        {
                            item.comboBox1.Text = "1";
                            if (errList.Contains("Error in " + item.Name))
                            {
                                errList.Remove("Error in " + item.Name);
                                VerifyErrList();
                            }
                        }
                        else
                        {
                            testUidIndexOfFirstQuestion = item.comboBox1.Text;
                        }
                    }
                    else if (item.Name == "test_id")
                    {
                        if (string.IsNullOrEmpty(item.comboBox1.Text) || !utils.IsNumeric(item.comboBox1.Text))
                        {

                        }
                        else
                        {
                            testId = item.comboBox1.Text;
                        }
                    }
                    else if (item.Name == "district_id")
                    {
                        if (string.IsNullOrEmpty(item.comboBox1.Text) || !utils.IsNumeric(item.comboBox1.Text))
                        {

                        }
                        else
                        {
                            districtId = item.comboBox1.Text;
                        }
                    }

                }
                PrevRecognizeBubbles();
                //testUidTestId = testId;
                //testUidShetId = rec.SheetIdentifier;
                string key = rec.SheetIdentifier + "_" + testId + "_" + amoutOfQuestions + "_" + indexOfQuestion;
                //int index=testUids.
                eDoctrinaOcrEd.TestUids.AreaSettings[] arrs = new TestUids.AreaSettings[factAreas.Length];
                if (rec.SheetIdentifier == "FLEX")
                {
                    if (factAreas[0].bubblesPerLine > rec.bubbles_per_lineFLEX)
                    {
                        MessageBox.Show("Invalid value of cols."
                           , Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        rbtnGrid.Checked = true;
                        return;
                    }
                }
                for (int i = 0; i < factAreas.Length; i++)
                {
                    arrs[i].BubblesPrrLine = factAreas[i].bubblesPerLine;
                    arrs[i].SubLineAmout = factAreas[i].subLinesAmount;
                    //if (rec.areas[i].bubblesPerLine < factAreas[i].bubblesPerLine)
                    //{
                    //}
                }
                if (testUids.Test.ContainsKey(key))
                {
                    testUids.Test[key] = arrs;//factAreas;
                }
                else
                {
                    testUids.Test.Add(key, arrs);
                }
                areaNaturalSize = Rectangle.Empty;
                for (int i = 0; i < factAreas.Length; i++)
                {
                    if (factAreas[i].width == 0 && allAreasNaturalSize[i].Width != 0)
                    {
                        factAreas[i].left = allAreasNaturalSize[i].X;
                        factAreas[i].top = allAreasNaturalSize[i].Y;
                        factAreas[i].width = allAreasNaturalSize[i].Width;
                        factAreas[i].height = allAreasNaturalSize[i].Height;
                    }
                }
                double heightEt;
                double kx, ky;
                //rec.Status = RecognizeAction.SearchBublesFinished;
                rec.allContourMultiLine = new Dictionary<Bubble, Point[]>();
                rec.factRectangle = new Rectangle[0];
                Rectangle bubble = Rectangle.Empty;
                double factStepX, factStepY;
                int amout_of_questions = 0;
                Bubble bubble1 = new Bubble();
                //bubble1.point = new Point(rec.indexOfFirstBubble, rec.IndexOfFirstQuestion - 1);
                bubble1.point = new Point(0, rec.IndexOfFirstQuestion - 1);
                for (int i = 0; i < factAreas.Length; i++)
                {//Areas++
                    if (factAreas[i].left < 0 || factAreas[i].left > pictureBox1.Image.Width
                        || factAreas[i].width <= 0 || factAreas[i].width > pictureBox1.Image.Width
                        || factAreas[i].top < 0 || factAreas[i].top > pictureBox1.Image.Height
                        || factAreas[i].height <= 0 || factAreas[i].height > pictureBox1.Image.Height
                        || factAreas[i].lineHeight < 0)
                    {

                        factAreas[i] = new RegionsArea();
                    }
                    if (factAreas.Length > 1 && factAreas[0].height == 0 || (i > 0 && factAreas[i - 1].height == 0))
                    {
                        MessageBox.Show("You must specify all areas of bubbles."
                            , Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        rbtnGrid.Checked = true;
                        return;
                    }
                    if (factAreas[i].height == 0)
                        continue;
                    amout_of_questions += factLinesPerArea[i];

                    if (rec.kx == 0)
                        rec.kx = 1;
                    if (rec.ky == 0)
                        rec.ky = 1;

                    if (rec.kx != 1 || rec.ky != 1)
                    {//EditorForm.cs:строка 3737
                        rec.bubblesOfRegion[i] = new Rectangle((int)Math.Round(rec.bubblesOfRegion[i].X / rec.kx)
                        , (int)Math.Round(rec.bubblesOfRegion[i].Y / rec.ky), rec.bubblesOfRegion[i].Width, rec.bubblesOfRegion[i].Height);
                        rec.bubblesRegions[i].Width = (int)Math.Round(rec.bubblesRegions[i].Width / rec.kx);
                        rec.bubblesRegions[i].Height = (int)Math.Round(rec.bubblesRegions[i].Height / rec.ky);
                    }
                    double EtStepX = (double)
                        (bubblesRegions.areas[i].width - bubblesRegions.areas[i].bubble.Width
                        * bubblesRegions.areas[i].bubblesPerLine)
                        / (bubblesRegions.areas[i].bubblesPerLine - 1);
                    int widthEt = (int)(bubblesRegions.areas[i].bubble.Width
                        * factAreas[i].bubblesPerLine + EtStepX * (factAreas[i].bubblesPerLine - 1));//(int)linsForm.nudCols.Value - 1);
                    kx = (double)factAreas[i].width / widthEt;
                    rec.kx = (decimal)kx;
                    double EtStepY = 0;
                    if (rec.linesPerArea[i] > 1)
                    {
                        EtStepY = (double)(bubblesRegions.areas[i].height
                            - bubblesRegions.areas[i].bubble.Height * rec.linesPerArea[i]) / (rec.linesPerArea[i] - 1);
                    }
                    //double EtStepY = bubblesRegions.areas[i].lineHeight - bubblesRegions.areas[i].bubble.Height;
                    heightEt = bubblesRegions.areas[i].bubble.Height * factLinesPerArea[i]
                       + EtStepY * (factLinesPerArea[i] - 1)
                       + (bubblesRegions.areas[i].subLineHeight
                       * (factAreas[i].subLinesAmount));//bubblesRegions.areas[i].subLinesAmount -
                    ky = (double)factAreas[i].height / heightEt;
                    rec.ky = (decimal)ky;

                    //rec.ProcessingRegions();

                    bubble.Width = (int)Math.Round(bubblesRegions.areas[i].bubble.Width * kx);
                    bubble.Height = (int)Math.Round(bubblesRegions.areas[i].bubble.Height * ky);
                    factStepX = (double)(factAreas[i].width + EtStepX * kx) / (double)factAreas[i].bubblesPerLine;
                    double distX = (double)factStepX - bubble.Width;
                    //double halfDistX = (double)distX / 2;
                    factAreas[i].lineHeight = (int)Math.Round(bubblesRegions.areas[i].lineHeight * ky);
                    factAreas[i].subLineHeight = (int)Math.Round(bubblesRegions.areas[i].subLineHeight * ky);
                    factAreas[i].bubble = new Rectangle((int)(rec.bubblesOfRegion[i].X * kx)
                                                            , (int)(rec.bubblesOfRegion[i].Y * ky)
                                                            , bubble.Width
                                                            , bubble.Height
                                                            );//???

                    rec.bubblesOfRegion[i] = new Rectangle((int)(rec.bubblesOfRegion[i].X * kx)
                        , (int)(rec.bubblesOfRegion[i].Y * ky), bubble.Width, bubble.Height);

                    rec.bubblesRegions[i].Width = (int)Math.Round(rec.bubblesRegions[i].Width * rec.kx);
                    rec.bubblesRegions[i].Height = (int)Math.Round(rec.bubblesRegions[i].Height * rec.ky);

                    factStepY = (double)((factAreas[i].height + EtStepY * ky)
                        - factAreas[i].subLineHeight * factAreas[i].subLinesAmount) / (double)factLinesPerArea[i];
                    Array.Resize(ref rec.factRectangle, rec.factRectangle.Length + 1);
                    rec.factRectangle[rec.factRectangle.Length - 1] = new Rectangle
                        (
                          factAreas[i].left
                        , factAreas[i].top
                        , bubble.Width
                        , bubble.Height
                        );

                    bubble1.areaNumber = i;
                    //bubble1.point = new System.Drawing.Point(rec.indexOfFirstBubble, bubble1.point.Y + 1);
                    bubble1.point = new System.Drawing.Point(0, bubble1.point.Y + 1);
                    bubble1.subLine = 0;
                    rec.allContourMultiLine.Add(bubble1, new System.Drawing.Point[5]
                {
                 new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                });

                    //Bitmap b2 = (Bitmap)pictureBox1.Image.Clone();
                    //using (Graphics g = Graphics.FromImage(b2))
                    //{
                    for (int j = 0; j < factLinesPerArea[i]; j++)//(int)linsForm.nudRows.Value
                    {//line++
                        //g.DrawLine
                        //    (
                        //      new Pen(Color.Blue)
                        //    , factAreas[i].left
                        //    , (int)(factAreas[i].top + factStepY * j)
                        //    , factAreas[i].left + factAreas[i].width
                        //    , (int)(factAreas[i].top + factStepY * j)
                        //    );
                        if (j > 0)
                        {
                            Array.Resize(ref rec.factRectangle, rec.factRectangle.Length + 1);
                            rec.factRectangle[rec.factRectangle.Length - 1] = new Rectangle
                                (
                                  factAreas[i].left
                                , factAreas[i].top + (int)Math.Round((factStepY) * j)
                                , bubble.Width
                                , bubble.Height
                                );

                            //bubble1.point = new Point(rec.indexOfFirstBubble, bubble1.point.Y + 1);
                            bubble1.point = new Point(0, bubble1.point.Y + 1);
                            bubble1.subLine = 0;

                            rec.allContourMultiLine.Add(bubble1, new System.Drawing.Point[5]
                                {
                                 new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                });

                        }
                        for (int k = 0; k <= (int)bubblesRegions.areas[i].subLinesAmount; k++)
                        {
                            if (k > 0)
                            {//subLine++
                                Array.Resize(ref rec.factRectangle, rec.factRectangle.Length + 1);
                                rec.factRectangle[rec.factRectangle.Length - 1] = new Rectangle
                                (
                                  factAreas[i].left
                                , rec.factRectangle[rec.factRectangle.Length - 2].Y + factAreas[i].subLineHeight
                                , bubble.Width
                                , bubble.Height
                                );

                                bubble1.subLine = bubble1.subLine + 1;
                                //bubble1.point = new Point(rec.indexOfFirstBubble, bubble1.point.Y);
                                bubble1.point = new Point(0, bubble1.point.Y);
                                rec.allContourMultiLine.Add(bubble1, new System.Drawing.Point[5]
                                {
                                 new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                });
                            }

                            for (int l = 1; l < factAreas[i].bubblesPerLine; l++)//(int)linsForm.nudCols.Value
                            {//point.X++

                                //    g.DrawLine
                                //(
                                //new Pen(Color.Blue)
                                //, (int)(factAreas[i].left + factStepX * l)
                                //, factAreas[i].top
                                //, (int)(factAreas[i].left + factStepX * l)
                                //, factAreas[i].top + factAreas[i].height
                                //);

                                Array.Resize(ref rec.factRectangle, rec.factRectangle.Length + 1);
                                rec.factRectangle[rec.factRectangle.Length - 1] = new Rectangle
                                   (
                                     factAreas[i].left + (int)Math.Round((factStepX) * l)
                                   , rec.factRectangle[rec.factRectangle.Length - 2].Y
                                   , bubble.Width
                                   , bubble.Height
                                  );
                                bubble1.point = new Point(bubble1.point.X + 1, bubble1.point.Y);
                                rec.allContourMultiLine.Add(bubble1, new System.Drawing.Point[5]
                                {
                                 new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Right, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Bottom)
                                ,new Point(rec.factRectangle[rec.factRectangle.Length - 1].Left, rec.factRectangle[rec.factRectangle.Length - 1].Top)
                                });
                            }
                        }
                    }
                    //    g.DrawRectangle(new Pen(Color.Red), area);
                    //}
                    //b2.Save("Rectangles.bmp", ImageFormat.Bmp);
                    //b2.Dispose();
                }
                //rec.BubblesRecognition();
                //ShowProcessingForm();
                linsForm.btnOk.Enabled = false;

                rec.AmoutOfQuestions = amout_of_questions;
                BarCodeItems[amout_of_questionsIndex].Value = amout_of_questions.ToString();
                rec.areas = bubblesRegions.areas;// factAreas;
                //rec.UpdateGui();

                barCodesPrompt = "";
                maxCountRectangles = rec.AddMaxCountRectangles();
                rec.BubblesRecognition();
                //rec.BubblesRecognition(false);
                if (backgroundWorker.IsBusy)
                {
                    CancelBackgroundWorker();
                    //do
                    //{
                    //    Application.DoEvents();
                    //} while (backgroundWorker.IsBusy);
                    //System.Threading.Thread.Sleep(500);
                }
                backgroundWorker.RunWorkerAsync(new string[] { "BubblesRecognition2" });
                //Focus();
                ShowProcessingForm();

                //Bitmap b = (Bitmap)pictureBox1.Image.Clone();
                //using (Graphics g = Graphics.FromImage(b))
                //{
                //    foreach (Rectangle item in rec.factRectangle)
                //    {
                //        g.DrawRectangle(new Pen(Color.Red), item);
                //    }
                //}
                //b.Save("factRectangles.bmp", ImageFormat.Bmp);
                //b.Dispose();

                //area= Rectangle.Empty;
                linsForm.btnOk.Enabled = true;
                rpf = null;
                //Focus();
                //linsForm.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Send this sheet to developers for fixing problem."
                 , Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                log.LogMessage(ex);
                string message = "Send log file to developers for fixing problem.";
                log.LogMessage(message);

                linsForm.btnOk.Enabled = true;
                rpf = null;
            }
        }