Пример #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "Recognition/ExtractAllBarcodes")] HttpRequestMessage req, TraceWriter log, ExecutionContext context)
        {
            try
            {
                if (!DemoConfiguration.UnlockSupport(log))
                {
                    return(GenerateErrorMessage(ApiError.LicenseNotSet, req));
                }

                var leadParameterObject = ParseLeadWebRequestParameters(req);
                if (!leadParameterObject.Successful)
                {
                    return(GenerateErrorMessage(ApiError.InvalidRequest, req));
                }

                var barcodeList = ExtractBarcodeSymbologies(req);
                if (barcodeList.Count == 0)
                {
                    return(GenerateErrorMessage(ApiError.InvalidRequest, req));
                }

                var imageReturn = await GetImageStreamAsync(leadParameterObject.LeadWebRequest.fileUrl, req, DemoConfiguration.MaxUrlMbs);

                if (!imageReturn.Successful)
                {
                    return(GenerateErrorMessage(imageReturn.ErrorType.Value, req));
                }

                using (imageReturn.Stream)
                {
                    LoadDocumentOptions options = new LoadDocumentOptions()
                    {
                        FirstPageNumber = leadParameterObject.LeadWebRequest.FirstPage,
                        LastPageNumber  = leadParameterObject.LeadWebRequest.LastPage
                    };

                    RecognitionEngine recognitionEngine = new RecognitionEngine();
                    recognitionEngine.WorkingDirectory = Path.GetTempPath();
                    BarcodeEngine            barcodeEngine  = new BarcodeEngine();
                    var                      barcodeResults = recognitionEngine.ExtractBarcode(imageReturn.Stream, options, barcodeEngine, barcodeList.ToArray(), 0, true);
                    List <BarcodeResultData> results        = new List <BarcodeResultData>();
                    foreach (var pageBarcodeData in barcodeResults)
                    {
                        foreach (var d in pageBarcodeData.BarcodeData)
                        {
                            if (d != null && d.Value != null)
                            {
                                var rect = new Rectangle(d.Bounds.X, d.Bounds.Y, d.Bounds.Width, d.Bounds.Height);
                                results.Add(new BarcodeResultData(pageBarcodeData.PageNumber, d.Symbology.ToString(), d.Value, rect, d.RotationAngle));
                            }
                        }
                    }
                    var returnRequest = req.CreateResponse(HttpStatusCode.OK);
                    returnRequest.Content = new StringContent(JsonConvert.SerializeObject(results));
                    return(returnRequest);
                }
            }
            catch (Exception ex)
            {
                log.Error($"API Error occurred for request: {context.InvocationId} \n Details: {JsonConvert.SerializeObject(ex)}");
                return(GenerateErrorMessage(ApiError.InternalServerError, req));
            }
        }
Пример #2
0
        private async Task <HttpResponseMessage> ExtractBarcodes([FromUri] BarcodeWebRequest request, int barcodeAmount)
        {
            try
            {
                AuthenticateRequest();

                if (!VerifyCommonParameters(request))
                {
                    throw new MalformedRequestException();
                }

                var symbologyList = new List <BarcodeSymbology>();
                if (string.IsNullOrEmpty(request.symbologies))//If no symbology string is passed, default to all symbologies.
                {
                    symbologyList = Enum.GetValues(typeof(BarcodeSymbology)).OfType <BarcodeSymbology>().ToList();
                    symbologyList.Remove(BarcodeSymbology.Unknown);
                }
                else
                {
                    symbologyList = ExtractBarcodeSymbologies(request.symbologies);
                    //If the user did supply a list of symbologies, but they could not be parsed, we will deny the request.
                    if (symbologyList.Count == 0)
                    {
                        throw new MalformedRequestException();
                    }
                }


                using (var stream = await GetImageStream(request.fileUrl))
                {
                    int lastPage = request.LastPage;
                    ValidateFile(stream, ref lastPage);
                    ConversionEngine    conversion = new ConversionEngine();
                    LoadDocumentOptions options    = new LoadDocumentOptions()
                    {
                        FirstPageNumber = request.FirstPage,
                        LastPageNumber  = lastPage
                    };
                    RecognitionEngine recognitionEngine = new RecognitionEngine();
                    BarcodeEngine     barcodeEngine     = new BarcodeEngine();

                    var barcodeList = recognitionEngine.ExtractBarcode(stream, options, barcodeEngine, symbologyList.ToArray(), barcodeAmount, false);
                    List <BarcodeResultData> results = new List <BarcodeResultData>();
                    foreach (var obj in barcodeList)
                    {
                        foreach (var d in obj.BarcodeData)
                        {
                            var result = new BarcodeResultData(obj.PageNumber, d.Symbology.ToString(), d.Value, new Rectangle(d.Bounds.X, d.Bounds.Y, d.Bounds.Width, d.Bounds.Height), d.RotationAngle);;
                            results.Add(result);
                        }
                    }


                    return(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(results))
                    });
                }
            }
            catch (Exception e)
            {
                return(GenerateExceptionMessage(e));
            }
        }