using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace RhSolutions.Services; public class YandexOcrClient : IOcrClient { private readonly HttpClient _httpClient; private readonly string _apiKey; private readonly string _xFolderId; public YandexOcrClient(HttpClient httpClient, IAddInConfiguration configuration) { _httpClient = httpClient; _httpClient.BaseAddress = new Uri("https://ocr.api.cloud.yandex.net/ocr/v1/"); _apiKey = configuration["apiKey"]; _xFolderId = configuration["x-folder-id"]; } public async Task> ProcessImage(string base64Image) { using StringContent jsonContent = new( JsonConvert.SerializeObject(new { mimeType = "PNG", languageCodes = new string[] { "ru", "en" }, model = "table", content = base64Image }), Encoding.UTF8, "application/json"); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Api-Key", _apiKey); _httpClient.DefaultRequestHeaders.Add("x-folder-id", _xFolderId); _httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true"); using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent); response.EnsureSuccessStatusCode(); string jsonResponse = await response.Content.ReadAsStringAsync(); OcrResponse deserialized = JsonConvert.DeserializeObject(jsonResponse); if (deserialized != null) { var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty(); if (tables.Any()) { List result = new(); foreach (var table in tables) { if (table.Cells == null || table.Cells.Count == 0) { continue; } int columnCount = int.Parse(table.ColumnCount); int rowCount = int.Parse(table.RowCount); object[,] cells = new object[rowCount, columnCount]; foreach (Cell cell in table.Cells) { int rowIndex = int.Parse(cell.RowIndex); int columnIndex = int.Parse(cell.ColumnIndex); cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ? v : cell.Text ?? string.Empty; } result.Add(cells); } return result; } } return Enumerable.Empty(); } }