public async Task <HttpResponseMessage> ExtractCheck([FromUri] LeadWebRequest request) { try { AuthenticateRequest(); if (!VerifyCommonParameters(request)) { throw new MalformedRequestException(); } using (var stream = await GetImageStream(request.fileUrl)) { int lastPage = request.LastPage; ValidateFile(stream, ref lastPage); LoadDocumentOptions options = new LoadDocumentOptions() { FirstPageNumber = request.FirstPage, LastPageNumber = lastPage }; RecognitionEngine recognitionEngine = new RecognitionEngine(); recognitionEngine.OcrEngine = ocrEngine; var micrResults = recognitionEngine.ExtractMicr(stream, options, Leadtools.Forms.Commands.BankCheckMicrFontType.Unknown); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(micrResults)) }); } } catch (Exception e) { return(GenerateExceptionMessage(e)); } }
/// <summary> /// Method to verify the common parameters that are sent with every request to the service. This method also double checks to make sure that the LEADTOOLS license is set appropriately. /// </summary> internal bool VerifyCommonParameters(LeadWebRequest request) { try { if (request == null) { throw new Exception(); } //Passing a last page value of -1 indicates that we want to process every page in the file. if ((request.FirstPage > request.LastPage && request.LastPage != -1) || request.FirstPage < 1) { throw new Exception(); } if (!DemoConfiguration.UnlockSupport()) { throw new RasterException("Your license file is missing, invalid or expired. LEADTOOLS will not function. Please contact LEAD Sales for information on obtaining a valid license"); } return(true); } catch (RasterException) { throw; } catch { return(false); } }
private async Task <HttpResponseMessage> ParseText([FromUri] LeadWebRequest request, bool additionalInfo) { try { AuthenticateRequest(); if (!VerifyCommonParameters(request)) { throw new MalformedRequestException(); } using (var stream = await GetImageStream(request.fileUrl)) { int lastPage = request.LastPage; ValidateFile(stream, ref lastPage); LoadDocumentOptions options = new LoadDocumentOptions() { FirstPageNumber = request.FirstPage, LastPageNumber = lastPage }; RecognitionEngine recognitionEngine = new RecognitionEngine(); recognitionEngine.OcrEngine = ocrEngine; recognitionEngine.WorkingDirectory = Path.GetTempPath(); var documentPageText = recognitionEngine.ExtractText(stream, options); List <ExtractTextData> PageDataList = new List <ExtractTextData>(); int currentPage = options.FirstPageNumber; if (!additionalInfo) { foreach (var page in documentPageText) { for (int i = 0; i < page.Words.Count; i++) { var word = page.Words[i]; word.Bounds = word.Bounds.ToLeadRect().ToLeadRectD(); page.Words[i] = word; } ExtractTextData pageData = new ExtractTextData { PageNumber = currentPage, PageText = page.Text, Words = page.Words.Select(w => new { w.Value, w.Bounds }).ToList() }; PageDataList.Add(pageData); currentPage++; } } else { foreach (var page in documentPageText) { for (int i = 0; i < page.Words.Count; i++) { var word = page.Words[i]; word.Bounds = word.Bounds.ToLeadRect().ToLeadRectD(); page.Words[i] = word; } for (int i = 0; i < page.Characters.Count; i++) { var character = page.Characters[i]; character.Bounds = character.Bounds.ToLeadRect().ToLeadRectD(); page.Characters[i] = character; } ExtractTextData pageData = new ExtractTextData { PageNumber = currentPage, PageText = page.Text, Words = page.Words, Characters = page.Characters }; PageDataList.Add(pageData); currentPage++; } } using (var ms = new MemoryStream()) { using (TextWriter tw = new StreamWriter(ms)) { tw.Write(JsonConvert.SerializeObject(PageDataList)); tw.Flush(); ms.Position = 0; Guid id = Guid.NewGuid(); string baseName = $"ExtractText-{id}.json"; string urlPath = $"{Url.Request.RequestUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)}/{new DirectoryInfo(DemoConfiguration.OutputFileDirectory).Name}/{baseName}"; string serverFilePath = $"{DemoConfiguration.OutputFileDirectory}{baseName}"; SaveToDisk(ms, serverFilePath); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(urlPath) }); } } } } catch (Exception e) { return(GenerateExceptionMessage(e)); } }