public frmActionBarAnalysis(Bitmap segment, Color backgroundColour, frmActionBarSelection sender, Design design)
        {
            InitializeComponent();

            _segment = segment;
            _backgroundColour = backgroundColour;

            _senderForm = sender;
            _design = design;

            double gini;

            try
            {
                Bitmap preview;
                Views.ActionBar result = WidgetAnalyser.AnalyseActionBar(_segment, _backgroundColour, out gini, out preview);

                segmentPictureBox.BackgroundImage = preview;

                if (result != null)
                {
                    lblGini.Text = gini.ToString();
                    lblType.Text = result.WidgetType.ToString();

                    txtActionBarLabel.Text = result.TitleText;

                    _actionbar = result;
                }
                else
                {
                    lblGini.Text = "";
                    lblType.Text = "I'm not sure this is an action bar. If it is, enter its title.";
                }
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("Background colour must be selected!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
                _senderForm.Show();
                return;
            }
        }
        private void btnDone_Click(object sender, EventArgs e)
        {
            if (_actionbar == null)
            {
                if (MessageBox.Show("Are you sure you want to proceed without setting an actionbar?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
            }

            if (txtActionBarLabel.Text == "")
            {
                if (MessageBox.Show("Actionbar text is blank, continue?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
            }

            if (_actionbar != null)
            {
                _actionbar = new Views.ActionBar();
                _actionbar.TitleText = txtActionBarLabel.Text;
                _actionbar.RegisterTitleTextWithStringsFile(_design);
            }

            _design.ActionBar = _actionbar;

            this.Close();
            _senderForm.Show();
            _senderForm.IncrementStep();
        }
Ejemplo n.º 3
0
        private static Views.ActionBar AnalyseActionBar(Image<Bgr, Byte> image, Color BackgroundColour)
        {
            ActionBarText title = new ActionBarText();
            bool currentTextIsTitle = true;

            List<ActionBarText> textualButtons = new List<ActionBarText>();

            _ocr.Recognize(image.Convert<Gray, Byte>());
            Tesseract.Charactor[] charactors = _ocr.GetCharactors();

            bool[] alreadyScannedXCoord = new bool[image.Width];

            ActionBarText button = new ActionBarText();
            button.Text = "";

            if (charactors.Length > 0)
            {
                int startCharactor = 0;

                string completeString = "";

                for (int i = 0; i < charactors.Length; i++)
                {
                    completeString += charactors[i].Text;
                }

                Regex regex = new Regex(@"^((?=[a-zA-Z][a-zA-Z])[a-zA-Z]|[0-9]).+$");

                for (int i = 0; i < completeString.Length; i++)
                {
                    if (regex.IsMatch(completeString.Substring(i)))
                    {
                        startCharactor = i;
                        break;
                    }
                }

                Regex isWord = new Regex(@"^[a-zA-Z]*$");

                int startX = charactors[startCharactor].Region.X;
                int endX = startX + charactors[startCharactor].Region.Width;

                int indexOfStartTextRegion = startCharactor;

                if (startX > image.Width / 4)
                {
                    currentTextIsTitle = false;
                }
                else
                {
                    title.Text += charactors[startCharactor].Text;
                }

                for (int i = startCharactor + 1; i < charactors.Length; i++)
                {
                    if (currentTextIsTitle && (endX + 5 > charactors[i].Region.X || charactors[i].Text == ""))
                    {
                        title.Text += charactors[i].Text;
                        title.UpdateStartPoint(charactors[i].Region.X, charactors[i].Region.Y);
                        title.UpdateFurthestPoint(charactors[i].Region.X + charactors[i].Region.Width, charactors[i].Region.Y + charactors[i].Region.Height);

                        if (charactors[i].Text == " ")
                        {
                            endX += 10;
                        }
                        else
                        {
                            endX = charactors[i].Region.X + charactors[i].Region.Width;
                        }
                    }
                    else if (endX + 5 > charactors[i].Region.X && charactors[i].Text != " ")
                    {
                        button.Text += charactors[i].Text;
                        button.UpdateStartPoint(charactors[i].Region.X, charactors[i].Region.Y);
                        button.UpdateFurthestPoint(charactors[i].Region.X + charactors[i].Region.Width, charactors[i].Region.Y + charactors[i].Region.Height);

                        endX = charactors[i].Region.X + charactors[i].Region.Width;
                    }
                    else
                    {
                        if (currentTextIsTitle)
                        {
                            currentTextIsTitle = false;
                            title.Text = title.Text.Trim();

                            break;
                        }
                        else
                        {
                            if (isWord.IsMatch(button.Text.Trim()))
                            {
                                textualButtons.Add(button);
                            }
                            button = new ActionBarText();
                            button.Text = "";
                        }

                        for (int x = startX; x <= endX; x++)
                        {
                            alreadyScannedXCoord[x] = true;
                        }

                        if (charactors[i].Text == " ")
                        {
                            i++;
                        }

                        startX = charactors[i].Region.X;
                        endX = startX + charactors[i].Region.Width;

                        indexOfStartTextRegion = i;
                    }
                }
            }

            Views.ActionBar result = new Views.ActionBar();
            result.WidgetType = WidgetType.ActionBar;
            result.TitleText = title.Text;

            //Background colour and text colour detection

            Color backgroundColor;
            Color textColor;

            getPrimaryAndSecondaryColours(image, out backgroundColor, out textColor);

            string backgroundColourName = Colour.AddPrimaryColour(backgroundColor);
            string textColourName = Colour.AddColour(textColor, result.WidgetType, result.TitleText);

            return result;
        }