private async Task <GoogleOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    var builder = new ImageAnnotatorClientBuilder {
                        JsonCredentials = File.ReadAllText(_configurations.CredentialsJsonFile)
                    };
                    var client = await builder.BuildAsync();

                    var img = await Image.FromStreamAsync(stream);

                    var textAnnotations = await client.DetectTextAsync(img);

                    var rawGoogleOcrResult = RawGoogleOcrResult.CreateFrom(textAnnotations.ToList());

                    var content = _googleOcrParser.Execute(rawGoogleOcrResult, preprocessedResult.NewImageHeight,
                                                           preprocessedResult.NewImageWidth);
                    return(GoogleOcrResult.CreateSuccessResult(DateTime.Now.Subtract(start), content, rawGoogleOcrResult));
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }
Ejemplo n.º 2
0
        private async Task <GoogleOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    using (var service = VisionService())
                    {
                        var entries = await service.RecognizeTextAsync(stream);

                        var rawGoogleOcrResult = RawGoogleOcrResult.CreateFrom(entries);
                        var content            = _googleOcrParser.Execute(rawGoogleOcrResult, preprocessedResult.NewImageHeight,
                                                                          preprocessedResult.NewImageWidth);
                        return(GoogleOcrResult.CreateSuccessResult(DateTime.Now.Subtract(start), content, rawGoogleOcrResult));
                    }
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }