Beispiel #1
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;
            }
        }
Beispiel #2
0
 public Document SalvarOuAtualizarDocumento(Document documento)
 {
     return(_documentosRepository.SalvarOuAtualizarDocumento(documento));
 }