Let’s suppose that the Aspose.Cloud AppSid and AppKey are stored in system environment variables ASPOSE_CLOUD_APPSID and ASPOSE_CLOUD_APPKEY.
C#
usingAspose.Ocr.Cloud.Sdk;usingAspose.Ocr.Cloud.Sdk.Model;usingAspose.Ocr.Cloud.Sdk.Model.Requests;usingNewtonsoft.Json.Linq;publicclassAsposeOCRForCloudConnector:IOcrConnector{privatereadonlyConfigurationconfiguration;publicAsposeOCRForCloudConnector(){configuration=newConfiguration();configuration.AppSid=Environment.GetEnvironmentVariable("ASPOSE_CLOUD_APPSID");configuration.AppKey=Environment.GetEnvironmentVariable("ASPOSE_CLOUD_APPKEY");configuration.ApiBaseUrl="https://api.aspose.cloud";configuration.IdentityServerBaseUrl="https://api.aspose.cloud";}publicRecognizedImageRecognize(StreamimageStream){try{OcrApiapi=newOcrApi(configuration);varrequest=newPostOcrFromUrlOrContentRequest(imageStream,resultType:ResultType.Internal,dsrMode:DsrMode.DsrAndFilter);OCRResponseresponse=api.PostOcrFromUrlOrContent(request);returnCreateDtoFromResponse(JToken.Parse(response.Text));}catch(Exceptionex){Console.WriteLine("Aspose.OCR for Cloud Recognition failed: {0}",ex.ToString());}returnnewRecognizedImage(newList<TextLine>());}protectedvirtualRecognizedImageCreateDtoFromResponse(JTokenjToken){returnnewRecognizedImage(LeafRecursion(jToken));}privateTextLine[]LeafRecursion(JTokenjToken){List<TextLine>lines=newList<TextLine>();if(jToken["leaves"].Count()>0){foreach(varnodeinjToken["leaves"]){if(node["leaves"].Count()>0){lines.AddRange(LeafRecursion(node));}else{TextLinetextLine=CreateTextLine(node);if(textLine!=null){lines.Add(textLine);}}}}returnlines.ToArray();}privateTextLineCreateTextLine(JTokenline){stringlineText=line["values"].ToString();RectanglelineRectangle=GetLineRectangle(line);if(!string.IsNullOrEmpty(lineText)&&!lineRectangle.IsEmpty){returnnewTextLine(RegularTextLine.SplitToFragments(lineText,lineRectangle));}else{returnnull;}}privateRectangleGetLineRectangle(JTokenline){varrect=line["rect"];returnnewRectangle(rect[0].Value<int>(),rect[1].Value<int>(),rect[2].Value<int>()-rect[0].Value<int>(),rect[3].Value<int>()-rect[1].Value<int>());}}
The PostOcrFromUrlOrContent() method with ResultType.Internal returns a JSON-serialized tree of text regions and recognized lines, with bounding rectangles. The text line is represented by a single object.
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to perform redactions for various document formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Redaction App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.