Exemplo n.º 1
0
        public ProcessingResult <ICroppedArea> CleanUp(ICroppedArea areaToClean)
        {
            if (areaToClean.CroppedParts == null || areaToClean.CroppedParts.Count == 0)
            {
                return(ProcessingResult <ICroppedArea> .Failure(new EmptyInput()));
            }

            using (areaToClean)
            {
                try
                {
                    var cleanedParts = new List <OrderedBitmap>();

                    foreach (var croppedPart in areaToClean.CroppedParts)
                    {
                        Bitmap formattedImage = Grayscale.CommonAlgorithms.RMY.Apply(croppedPart.Bitmap);
                        new BradleyLocalThresholding().ApplyInPlace(formattedImage);
                        cleanedParts.Add(new OrderedBitmap(croppedPart.Order, formattedImage));
                    }
                    return(ProcessingResult <ICroppedArea> .Success(
                               new CroppedArea(areaToClean.AreaUsedForCropping, cleanedParts, areaToClean.DocumentId)));
                }
                catch (UnsupportedImageFormatException formatException)
                {
                    return(ProcessingResult <ICroppedArea> .Failure(new UnsupportedImageFormat()));
                }
                catch (Exception generalException)
                {
                    return(ProcessingResult <ICroppedArea> .Failure(new UncaughtException(generalException)));
                }
            }
        }
        public ProcessingResult <ICollection <ICroppedArea> > CropUserInput(Bitmap bitmapToCropIntoParts, Guid documentId,
                                                                            ITemplatePageDefinition definitionForCropping)
        {
            if (definitionForCropping == null)
            {
                return(ProcessingResult <ICollection <ICroppedArea> > .Failure(new PageDefinitionNotProvided()));
            }

            if (documentId == Guid.Empty)
            {
                return(ProcessingResult <ICollection <ICroppedArea> > .Failure(new MissingDocumentId()));
            }

            //Can this be more granular without downloading bitmap all over again?
            //Maybe parent holding bitmap and just passing it over to all children?
            using (bitmapToCropIntoParts)
            {
                var results = new List <ICroppedArea>();
                foreach (var templateArea in definitionForCropping.DefinedAreas)
                {
                    try
                    {
                        var croppedAreaParts = new List <OrderedBitmap>();

                        if (templateArea.InterestPoints == null || templateArea.InterestPoints.Count == 0)
                        {
                            croppedAreaParts.Add(
                                new OrderedBitmap(0, new Crop(templateArea.AreaDimension).Apply(bitmapToCropIntoParts)));
                        }
                        else
                        {
                            foreach (var areaPart in templateArea.InterestPoints)
                            {
                                croppedAreaParts.Add(
                                    new OrderedBitmap(areaPart.OrderInArea, new Crop(areaPart.Dimension).Apply(bitmapToCropIntoParts)));
                            }
                        }

                        results.Add(new CroppedArea(templateArea, croppedAreaParts, documentId));
                    }
                    catch (UnsupportedImageFormatException)
                    {
                        return(ProcessingResult <ICollection <ICroppedArea> > .Failure(new UnsupportedImageFormat()));
                    }
                    catch (Exception ex)
                    {
                        ProcessingResult <ICollection <ICroppedArea> > .Failure(new UncaughtException(ex));
                    }
                }
                return(ProcessingResult <ICollection <ICroppedArea> > .Success(results));
            }
        }
        public ProcessingResult <Bitmap> ExtractHandwrittenInput(Bitmap userInput, ITemplatePageDefinition templatePage)
        {
            if (userInput == null || templatePage == null)
            {
                return(ProcessingResult <Bitmap> .Failure(new EmptyInput()));
            }

            using (userInput)
            {
                Bitmap canvasBitmap = null;

                try
                {
                    canvasBitmap = _canvasBitmapQueryHandler.Handle(
                        new GetCanvasBitmapForTemplatePage {
                        TemplatePageId = templatePage.Id
                    }).FileData.AsBitmap();

                    if (canvasBitmap == null)
                    {
                        //TODO: ResultMessage for failed HTTP request or pipe from ResourceQueryHandler
                        return(new ProcessingResult <Bitmap>(StepOutcome.Failure, null, null));
                    }

                    if (canvasBitmap.Size != userInput.Size)
                    {
                        using (var oldCanvas = canvasBitmap)
                        {
                            canvasBitmap = new ResizeBilinear(userInput.Width, userInput.Height).Apply(oldCanvas);
                        }
                    }

                    var result = new Invert().Apply(new Difference(userInput).Apply(canvasBitmap));

                    return(ProcessingResult <Bitmap> .Success(result));
                }
                catch (UnsupportedImageFormatException)
                {
                    return(ProcessingResult <Bitmap> .Failure(new UnsupportedImageFormat()));
                }
                finally
                {
                    canvasBitmap?.Dispose();
                }
            }
        }
Exemplo n.º 4
0
        public ProcessingResult <IRecognizedPart <bool> > RecognizeFrom(ICroppedArea croppedArea)
        {
            if (croppedArea.CroppedParts.Count != 1)
            {
                //TODO: ResultDetails for - expected only one element
                return(ProcessingResult <IRecognizedPart <bool> > .Failure(null));
            }

            var croppedElement = croppedArea.CroppedParts.Single();

            if (croppedElement == null)
            {
                //TODO: ResultDetails for - empty element in CroppedArea
                return(ProcessingResult <IRecognizedPart <bool> > .Failure(null));
            }

            return(ProcessingResult <IRecognizedPart <bool> > .Success(
                       new RecognizedChoicePart(croppedArea.AreaUsedForCropping.Id,
                                                croppedArea.DocumentId,
                                                //Empty field - no input; otherwise - is checked via handwriting
                                                croppedElement.Bitmap != null)));
        }