コード例 #1
0
        //public class DefaultBitmapPreprocessorClassDefinitionItem
        //{
        //    public string Value { set; get; }
        //    public string Value { set; get; }
        //}

        internal void SetUI(Template t, bool updateSharedValuesOnly)
        {
            Text = "Scanned image preparation for '" + t.Name + "'";
            if (!updateSharedValuesOnly)
            {
                PageRotation.SelectedIndex             = (int)t.PageRotation;
                bitmapPreprocessorClassDefinition.Text = t.BitmapPreprocessorClassDefinition;

                Deskewer.Config dc = t.Deskew != null ? t.Deskew : new Deskewer.Config();
                DeskewBlockMaxHeight.Value      = dc.BlockMaxLength;
                DeskewBlockMinGap.Value         = dc.BlockMinGap;
                DeskewStructuringElementX.Value = dc.StructuringElementSize.Width;
                DeskewStructuringElementY.Value = dc.StructuringElementSize.Height;
                DeskewContourMaxCount.Value     = dc.ContourMaxCount;
                DeskewBlockMinGap.Value         = dc.BlockMinGap;
                DeskewMarginColor.BackColor     = dc.MarginColor;
                DeskewMarginColor.Enabled       = DeskewContourMaxCount.Enabled = DeskewAngleMaxDeviation.Enabled = DeskewSingleBlock.Enabled = DeskewColumnOfBlocks.Enabled = DeskewStructuringElementX.Enabled = DeskewStructuringElementY.Enabled = Deskew.Checked = t.Deskew != null;
                if (t.Deskew != null)
                {
                    DeskewSingleBlock.Checked = t.Deskew.Mode.HasFlag(Deskewer.Modes.SingleBlock) || !t.Deskew.Mode.HasFlag(Deskewer.Modes.ColumnOfBlocks);
                }
                DeskewColumnOfBlocks.Checked = !DeskewSingleBlock.Checked;

                changed = false;
            }
            if (!changed && t.ScalingAnchorId != ScalingAnchor.SelectedItem as int?)
            {
                changed = true;
            }
            ScalingAnchor.Items.Clear();
            ScalingAnchor.Items.Add("");
            ScalingAnchor.Items.AddRange(t.Anchors.Where(a => a is Template.Anchor.CvImage).Select(a => (object)a.Id).ToArray());
            ScalingAnchor.SelectedItem = t.GetScalingAnchor()?.Id;
        }
コード例 #2
0
ファイル: Page.cs プロジェクト: aTiKhan/PdfDocumentParser
        internal void OnActiveTemplateUpdating(Template newTemplate)
        {
            if (PageCollection.ActiveTemplate == null)
            {
                return;
            }

            if (newTemplate == null ||
                newTemplate.PageRotation != PageCollection.ActiveTemplate.PageRotation ||
                !Serialization.Json.IsEqual(newTemplate.Deskew, PageCollection.ActiveTemplate.Deskew) ||
                newTemplate.ScalingAnchorId != PageCollection.ActiveTemplate.ScalingAnchorId ||
                newTemplate.ScalingAnchorId > 0 && !Serialization.Json.IsEqual(newTemplate.GetScalingAnchor(), PageCollection.ActiveTemplate.GetScalingAnchor()) ||
                newTemplate.BitmapPreprocessorClassDefinition != PageCollection.ActiveTemplate.BitmapPreprocessorClassDefinition
                )
            {
                _activeTemplateImageData = null;
                _activeTemplateBitmap?.Dispose();
                _activeTemplateBitmap      = null;
                _activeTemplateOcrCharBoxs = null;
                _activeTemplateCvImage?.Dispose();
                _activeTemplateCvImage = null;
            }
            else if (newTemplate.TesseractPageSegMode != PageCollection.ActiveTemplate.TesseractPageSegMode)
            {
                _activeTemplateOcrCharBoxs = null;
            }

            if (!Serialization.Json.IsEqual(PageCollection.ActiveTemplate, newTemplate))
            {
                anchorIds2anchorActualInfo.Clear();
                fieldNames2fieldActualInfos.Clear();
            }
        }
コード例 #3
0
        //public class DefaultBitmapPreprocessorClassDefinitionItem
        //{
        //    public string Value { set; get; }
        //    public string Value { set; get; }
        //}

        internal void SetUI(Template t, bool updateSharedValuesOnly)
        {
            Text = "Bitmap preparation for '" + t.Name + "'";
            if (!updateSharedValuesOnly)
            {
                PageRotation.SelectedIndex                = (int)t.PageRotation;
                Deskew.Checked                            = t.Deskew;
                DeskewBlockMaxHeight.Value                = t.DeskewBlockMaxHeight;
                DeskewBlockMinSpan.Value                  = t.DeskewBlockMinSpan;
                CvImageScalePyramidStep.Value             = t.CvImageScalePyramidStep;
                bitmapPreprocessorClassDefinition.Text    = t.BitmapPreprocessorClassDefinition;
                bitmapPreprocessorClassDefinition.Enabled = PreprocessBitmap.Checked = t.PreprocessBitmap;
                TesseractPageSegMode.SelectedItem         = t.TesseractPageSegMode;
                changed = false;
            }
            if (!changed && t.ScalingAnchorId != ScalingAnchor.SelectedItem as int?)
            {
                changed = true;
            }
            ScalingAnchor.Items.Clear();
            ScalingAnchor.Items.Add("");
            ScalingAnchor.Items.AddRange(t.Anchors.Where(a => a is Template.Anchor.CvImage).Select(a => (object)a.Id).ToArray());
            ScalingAnchor.SelectedItem = t.GetScalingAnchor()?.Id;
        }
コード例 #4
0
        internal Template GetTemplateFromUI(bool saving)
        {
            Template t = new Template();

            if (saving && string.IsNullOrWhiteSpace(name.Text))
            {
                throw new Exception("Name is empty!");
            }

            t.Name = name.Text.Trim();

            t.TextAutoInsertSpace = new TextAutoInsertSpace
            {
                Threshold      = (float)textAutoInsertSpaceThreshold.Value,
                Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text),
            };

            t.CvImageScalePyramidStep = (int)CvImageScalePyramidStep.Value;
            t.TesseractPageSegMode    = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem;

            if (SingleFieldFromFieldImage.Checked)
            {
                t.FieldOcrMode |= Template.FieldOcrModes.SingleFieldFromFieldImage;
            }
            else
            {
                t.FieldOcrMode &= ~Template.FieldOcrModes.SingleFieldFromFieldImage;
            }
            if (ColumnFieldFromFieldImage.Checked)
            {
                t.FieldOcrMode |= Template.FieldOcrModes.ColumnFieldFromFieldImage;
            }
            else
            {
                t.FieldOcrMode &= ~Template.FieldOcrModes.ColumnFieldFromFieldImage;
            }

            bitmapPreparationForm.SetTemplate(t);

            bool?      removeNotLinkedAnchors = null;
            List <int> conditionAnchorIds     = null;

            if (saving)
            {
                conditionAnchorIds = new List <int>();
                foreach (DataGridViewRow r in conditions.Rows)
                {
                    Template.Condition c = (Template.Condition)r.Tag;
                    if (c != null && c.IsSet())
                    {
                        conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value));
                    }
                }
                conditionAnchorIds = conditionAnchorIds.Distinct().ToList();
            }
            t.Anchors = new List <Template.Anchor>();
            foreach (DataGridViewRow r in anchors.Rows)
            {
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a == null)
                {
                    continue;
                }

                if (saving)
                {
                    if (!a.IsSet())
                    {
                        throw new Exception("Anchor[Id=" + a.Id + "] is not set!");
                    }

                    if (!conditionAnchorIds.Contains(a.Id))
                    {
                        bool engaged = false;
                        foreach (DataGridViewRow rr in anchors.Rows)
                        {
                            Template.Anchor a_ = (Template.Anchor)rr.Tag;
                            if (a_ == null)
                            {
                                continue;
                            }
                            if (a_.ParentAnchorId == a.Id)
                            {
                                engaged = true;
                                break;
                            }
                        }
                        if (!engaged)
                        {
                            foreach (DataGridViewRow rr in fields.Rows)
                            {
                                Template.Field m = (Template.Field)rr.Tag;
                                if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id))
                                {
                                    engaged = true;
                                    break;
                                }
                            }
                            if (!engaged)
                            {
                                if (a.Id != t.ScalingAnchorId)
                                {
                                    if (removeNotLinkedAnchors == null)
                                    {
                                        removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?");
                                    }
                                    if (removeNotLinkedAnchors == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a));
            }
            t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList();

            if (saving)
            {
                t.GetScalingAnchor();//check is it is correct;
            }
            t.Conditions = new List <Template.Condition>();
            foreach (DataGridViewRow r in conditions.Rows)
            {
                Template.Condition c = (Template.Condition)r.Tag;
                if (c == null)
                {
                    continue;
                }
                if (saving)
                {
                    if (!c.IsSet())
                    {
                        throw new Exception("Condition[name=" + c.Name + "] is not set!");
                    }
                    BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id));
                }
                t.Conditions.Add(Serialization.Json.Clone(c));
            }
            if (saving)
            {
                var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                if (dcs != null)
                {
                    throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!");
                }
            }

            t.Fields = new List <Template.Field>();
            foreach (DataGridViewRow r in fields.Rows)
            {
                Template.Field f = (Template.Field)r.Tag;
                if (f == null)
                {
                    continue;
                }
                if (saving && !f.IsSet())
                {
                    throw new Exception("Field[" + r.Index + "] is not set!");
                }
                if (saving)
                {
                    foreach (int?ai in new List <int?> {
                        f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id
                    })
                    {
                        if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null)
                        {
                            throw new Exception("Anchor[Id=" + ai + " does not exist.");
                        }
                    }
                }
                t.Fields.Add(Serialization.Json.Clone(f));
            }
            if (saving)
            {
                if (t.Fields.Count < 1)
                {
                    throw new Exception("Fields is empty!");
                }
                //var dfs = t.Fields.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                //if (dfs != null)
                //    throw new Exception("Field '" + dfs.First().Name + "' is duplicated!");

                //foreach (string columnOfTable in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable != null).Select(x => x.ColumnOfTable))
                //{
                //    Dictionary<string, List<Template.Field>> fieldName2orderedFields = new Dictionary<string, List<Template.Field>>();
                //    foreach (Template.Field.PdfText pt in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable == columnOfTable))
                //    {
                //        List<Template.Field> fs;
                //        if (!fieldName2orderedFields.TryGetValue(pt.Name, out fs))
                //        {
                //            fs = new List<Template.Field>();
                //            fieldName2orderedFields[pt.Name] = fs;
                //        }
                //        fs.Add(pt);
                //    }
                //    int definitionCount = fieldName2orderedFields.Max(x => x.Value.Count());
                //    foreach (string fn in fieldName2orderedFields.Keys)
                //        if (definitionCount > fieldName2orderedFields[fn].Count)
                //            throw new Exception("Field '" + fn + "' is column of table " + columnOfTable + " and so it must have the same number of definitions as the rest column fields!");
                //}

                //foreach (Template.Field f0 in t.Fields)
                //{
                //    int tableDefinitionsCount = t.Fields.Where(x => x.Name == f0.Name).Count();
                //    string fn = t.Fields.Where(x => x.ColumnOfTable == f0.Name).GroupBy(x => x.Name).Where(x => x.Count() > tableDefinitionsCount).FirstOrDefault()?.First().Name;
                //    if (fn != null)
                //        throw new Exception("Field '" + fn + "' is a column of table field " + f0.Name + " so " + f0.Name + " must have number of definitions not less then that of " + fn + ".");
                //}

                //it is done only to avoid unneeded OCR
                //string fn = t.Fields.Where(x => x.ColumnOfTable != null && (x.DefaultValueType != Template.Field.ValueTypes.PdfText && x.DefaultValueType != Template.Field.ValueTypes.PdfTextLines)).FirstOrDefault()?.Name;
                //if (fn != null)
                //    throw new Exception("Field '" + fn + "' is a column of a table field. Its default value type can be either " + Template.Field.ValueTypes.PdfText + " or " + Template.Field.ValueTypes.PdfTextLines);
            }

            if (saving)
            {
                t.Editor = new Template.EditorSettings
                {
                    TestFile         = testFile.Text,
                    TestPictureScale = pictureScale.Value,
                    ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked,
                    ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked,
                    CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked,
                };
            }

            return(t);
        }
コード例 #5
0
        internal Template GetTemplateFromUI(bool saving)
        {
            Template t = new Template();

            if (saving && string.IsNullOrWhiteSpace(name.Text))
            {
                throw new Exception("Name is empty!");
            }

            t.Name = name.Text.Trim();

            t.TextAutoInsertSpace = new TextAutoInsertSpace
            {
                Threshold          = (float)textAutoInsertSpace_Threshold.Value,
                IgnoreSourceSpaces = textAutoInsertSpace_IgnoreSourceSpaces.Checked,
                //Representative//default
            };

            t.TesseractPageSegMode      = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem;
            t.AdjustLineBorders         = AdjustLineBorders.Checked;
            t.SingleFieldFromFieldImage = SingleFieldFromFieldImage.Checked;
            t.ColumnCellFromCellImage   = ColumnCellFromCellImage.Checked;
            if (CharSizeFilterMinWidth.Value > 0 || CharSizeFilterMaxWidth.Value > 0 || CharSizeFilterMinHeight.Value > 0 || CharSizeFilterMaxHeight.Value > 0)
            {
                t.CharFilter = new CharFilter {
                    MinWidth = (float)CharSizeFilterMinWidth.Value, MaxWidth = (float)CharSizeFilterMaxWidth.Value, MinHeight = (float)CharSizeFilterMinHeight.Value, MaxHeight = (float)CharSizeFilterMaxHeight.Value
                }
            }
            ;
            if (LinePaddingY.Value > 0)
            {
                t.LinePaddingY = (int)LinePaddingY.Value;
            }

            bitmapPreparationForm.SetTemplate(t);

            bool?      removeNotLinkedAnchors = null;
            List <int> conditionAnchorIds     = null;

            if (saving)
            {
                conditionAnchorIds = new List <int>();
                foreach (DataGridViewRow r in conditions.Rows)
                {
                    Template.Condition c = (Template.Condition)r.Tag;
                    if (c != null && c.IsSet())
                    {
                        conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value));
                    }
                }
                conditionAnchorIds = conditionAnchorIds.Distinct().ToList();
            }
            t.Anchors = new List <Template.Anchor>();
            foreach (DataGridViewRow r in anchors.Rows)
            {
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a == null)
                {
                    continue;
                }

                if (saving)
                {
                    if (!a.IsSet())
                    {
                        throw new Exception("Anchor[Id=" + a.Id + "] is not set!");
                    }

                    if (!conditionAnchorIds.Contains(a.Id))
                    {
                        bool engaged = false;
                        foreach (DataGridViewRow rr in anchors.Rows)
                        {
                            Template.Anchor a_ = (Template.Anchor)rr.Tag;
                            if (a_ == null)
                            {
                                continue;
                            }
                            if (a_.ParentAnchorId == a.Id)
                            {
                                engaged = true;
                                break;
                            }
                        }
                        if (!engaged)
                        {
                            foreach (DataGridViewRow rr in fields.Rows)
                            {
                                Template.Field m = (Template.Field)rr.Tag;
                                if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id))
                                {
                                    engaged = true;
                                    break;
                                }
                            }
                            if (!engaged)
                            {
                                if (a.Id != t.ScalingAnchorId)
                                {
                                    if (removeNotLinkedAnchors == null)
                                    {
                                        removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?", this);
                                    }
                                    if (removeNotLinkedAnchors == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a));
            }
            t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList();

            if (saving)
            {
                t.GetScalingAnchor();//check is it is correct;
            }
            t.Conditions = new List <Template.Condition>();
            foreach (DataGridViewRow r in conditions.Rows)
            {
                Template.Condition c = (Template.Condition)r.Tag;
                if (c == null)
                {
                    continue;
                }
                if (saving)
                {
                    if (!c.IsSet())
                    {
                        throw new Exception("Condition[name=" + c.Name + "] is not set!");
                    }
                    BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id));
                }
                t.Conditions.Add((Template.Condition)Serialization.Json.Clone2(c));
            }
            if (saving)
            {
                var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                if (dcs != null)
                {
                    throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!");
                }
            }

            t.Fields = new List <Template.Field>();
            foreach (DataGridViewRow r in fields.Rows)
            {
                Template.Field f = (Template.Field)r.Tag;
                if (f == null)
                {
                    continue;
                }
                if (saving && !f.IsSet())
                {
                    throw new Exception("Field[" + r.Index + "] is not set!");
                }
                if (saving)
                {
                    foreach (int?ai in new List <int?> {
                        f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id
                    })
                    {
                        if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null)
                        {
                            throw new Exception("Anchor[Id=" + ai + " does not exist.");
                        }
                    }
                }
                t.Fields.Add((Template.Field)Serialization.Json.Clone2(f));
            }
            if (saving)
            {
                if (t.Fields.Count < 1)
                {
                    throw new Exception("Fields is empty!");
                }

                var fs = t.Fields.GroupBy(x => x.Name).Where(x => x.GroupBy(a => a.GetType()).Count() > 1).FirstOrDefault();
                if (fs != null)
                {
                    throw new Exception("Definitions of the field '" + fs.First().Name + "' are not of the same type!");
                }
            }

            if (saving)
            {
                t.Editor = new Template.EditorSettings
                {
                    //TestFile = testFile.Text,
                    TestPictureScale = pictureScale.Value,
                    ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked,
                    ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked,
                    CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked,
                };
            }

            return(t);
        }
    }