|
| 1 | +using Aspose.OCR.Cloud; |
| 2 | +using Aspose.OCR.Cloud.SDK.Api; |
| 3 | +using Aspose.OCR.Cloud.SDK.Model; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Aspose.OCR.Cloud.CommandLineTool.API |
| 11 | +{ |
| 12 | + public static class RecognizeImage |
| 13 | + { |
| 14 | + public static void Run(string srcFilePath, |
| 15 | + string dstFilePath, |
| 16 | + Language language = Language.English, |
| 17 | + bool makeBinarization = true, |
| 18 | + bool makeSkewCorrect = false, |
| 19 | + bool makeUpsampling = false, |
| 20 | + DsrMode dsrMode = DsrMode.DsrPlusDetector, |
| 21 | + ResultType resultType = ResultType.Text) |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + |
| 26 | + Config config = Config.GetConfig(); |
| 27 | + |
| 28 | + RecognizeImageApi api = new RecognizeImageApi(config.ClientId, config.ClientSecret); |
| 29 | + |
| 30 | + OCRRecognizeImageBody requestBody = new OCRRecognizeImageBody( |
| 31 | + image: File.ReadAllBytes(srcFilePath), |
| 32 | + settings: new OCRSettingsRecognizeImage( |
| 33 | + language: language, |
| 34 | + makeBinarization: makeBinarization, |
| 35 | + makeSkewCorrect: makeSkewCorrect, |
| 36 | + makeUpsampling: makeUpsampling, |
| 37 | + dsrMode: dsrMode, |
| 38 | + resultType: resultType |
| 39 | + )); |
| 40 | + |
| 41 | + Console.Write($"Sending file {srcFilePath} to API..."); |
| 42 | + string taskId = api.PostRecognizeImage(requestBody); |
| 43 | + Console.WriteLine("done."); |
| 44 | + |
| 45 | + Console.Write($"Waiting for completion of task ID {taskId}..."); |
| 46 | + Utilities.WaitTaskCompletion(taskId); |
| 47 | + Console.WriteLine("done."); |
| 48 | + |
| 49 | + Console.Write($"Saving result to {dstFilePath}..."); |
| 50 | + OCRResponse response = api.GetRecognizeImage(taskId); |
| 51 | + |
| 52 | + if (response == null) throw new Exception("API response is empty"); |
| 53 | + if (response.ResponseStatusCode != ResponseStatusCode.Ok) |
| 54 | + throw new Exception($"Response status code is {response.ResponseStatusCode.Value}"); |
| 55 | + if (response.Error != null) throw new Exception($"API response contains error:{response.Error.Messages.First()}"); |
| 56 | + if (response.TaskStatus != OCRTaskStatus.Completed) throw new Exception($"OCR task returned status {response.TaskStatus.Value}"); |
| 57 | + if (response.Results == null) throw new Exception($"Results is null"); |
| 58 | + |
| 59 | + |
| 60 | + string? directoryPath = Path.GetDirectoryName(dstFilePath); |
| 61 | + if (!string.IsNullOrEmpty(directoryPath) & !Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); |
| 62 | + |
| 63 | + var result = response.Results.First(); |
| 64 | + switch (result.Type) |
| 65 | + { |
| 66 | + case "Text": |
| 67 | + File.WriteAllText($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".txt")? "" : ".txt")}", |
| 68 | + Encoding.UTF8.GetString(result.Data)); |
| 69 | + break; |
| 70 | + case "Pdf": |
| 71 | + File.WriteAllBytes($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".pdf") ? "" : ".pdf")}", |
| 72 | + result.Data); |
| 73 | + break; |
| 74 | + case "Hocr": |
| 75 | + File.WriteAllBytes( |
| 76 | + $"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".html") ? "" : ".html")}", result.Data); |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + Console.WriteLine("done."); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + Console.WriteLine($"Error while processing image recognition: {ex.Message}"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | +} |
0 commit comments