Beispiel #1
0
        private Dictionary <OptionsField, string> ExtractOptionsFields(CropBoxes cropBoxes, byte[] imageEdit)
        {
            var cropedImages = new Dictionary <OptionsField, string>();

            foreach (var field in cropBoxes.GetOptionsBoxes())
            {
                if (!cropedImages.ContainsKey(field))
                {
                    var dimension = CropBoxes.GetOptionsFieldDimension(field);

                    var imageId = _documentosRepository.AttachFile(_imageService.CropImage(imageEdit, cropBoxes.GetOptionsBox(field), dimension.Item1, dimension.Item2, dimension.Item3, dimension.Item4));

                    cropedImages.Add(field, imageId.ToString());
                }
            }

            return(cropedImages);
        }
Beispiel #2
0
        public string CreateDocumentFromFile(byte[] streamedFileContent)
        {
            try
            {
                var heigth = 2000 * 1.38;
                var btm    = _imageService.ResizeImage(Bitmap.FromStream(new MemoryStream(streamedFileContent)), 2000, (int)heigth);
                var client = ImageAnnotatorClient.Create();


                byte[] resizeImageEdit = _imageService.ImageToByteArray(btm);
                var    image           = Google.Cloud.Vision.V1.Image.FromBytes(resizeImageEdit);
                var    response        = client.DetectDocumentText(image);

                //Graphics.FromImage()

                var blocos         = new List <Bloco>();
                var boxesWords     = new List <Tuple <System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF> >();
                var boxesParagraph = new List <Tuple <System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF> >();
                var boxesBlock     = new List <Tuple <System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF, System.Drawing.PointF> >();

                var cropBoxes = new CropBoxes();

                foreach (var page in response.Pages)
                {
                    foreach (var block in page.Blocks)
                    {
                        boxesBlock.Add(BoundigBoxToPoints(block.BoundingBox));
                        var bloco = new Bloco();

                        foreach (var paragraph in block.Paragraphs)
                        {
                            boxesParagraph.Add(BoundigBoxToPoints(paragraph.BoundingBox));
                            var paragrafo = new Paragrafo();

                            var texton1 = string.Empty;
                            var texton2 = string.Empty;
                            foreach (var word in paragraph.Words)
                            {
                                var wordBox = BoundigBoxToPoints(word.BoundingBox);
                                boxesWords.Add(wordBox);
                                string palavra = string.Join(string.Empty, word.Symbols.Select(x => x.Text));
                                var    field   = VerificarCampos(palavra, texton1, texton2);
                                cropBoxes.Push(field, wordBox);
                                texton2 = texton1;
                                texton1 = palavra;

                                paragrafo.Palavras.Add(palavra);
                            }

                            bloco.Paragrafos.Add(paragrafo);
                        }
                        blocos.Add(bloco);
                    }
                }
                cropBoxes.PopulateBoxes();
                var imageEdit = resizeImageEdit;

                // Salva a imagem no banco
                var attachmentID = this._documentosRepository.AttachFile(resizeImageEdit);
                var editID       = this._documentosRepository.AttachFile(imageEdit);

                Dictionary <DocumentField, string> cropedImages        = ExtractFields(cropBoxes, imageEdit);
                Dictionary <OptionsField, string>  cropedOptionsImages = ExtractOptionsFields(cropBoxes, imageEdit);

                var doc = new Document
                {
                    Status              = StatusDocumento.UPLOAD,
                    AttachmentId        = attachmentID.ToString(),
                    EditedId            = editID.ToString(),
                    DadosOriginais      = blocos.ToArray(),
                    CropedFields        = cropedImages,
                    CropedOptionsFields = cropedOptionsImages
                };

                ProcessarDadosOriginaisAsync(doc);

                // Cria documento
                doc = _documentosRepository.SalvarOuAtualizarDocumento(doc);

                //ProcessarDadosML(doc);
                AtualizarCamposSelecao(doc);
                //doc = _documentosRepository.AtualizarDocumentoAsync(doc).Result;

                if (doc != null && doc.Id != null)
                {
                    return(doc.Id);
                }
                return(ObjectId.Empty.ToString());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw;
            }
        }