RhSolutions-AddIn/RhSolutions.AddIn/Services/YandexOcrClient.cs

82 lines
2.5 KiB
C#
Raw Permalink Normal View History

2024-11-13 23:42:28 +03:00
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
2024-11-14 00:16:58 +03:00
using System.Threading.Tasks;
2024-11-14 23:51:27 +03:00
using System.Windows.Forms;
2024-11-13 23:42:28 +03:00
using Newtonsoft.Json;
2024-11-14 00:16:58 +03:00
namespace RhSolutions.Services;
2024-11-13 23:42:28 +03:00
public class YandexOcrClient : IOcrClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private readonly string _xFolderId;
public YandexOcrClient(HttpClient httpClient, IAddInConfiguration configuration)
2024-11-13 23:42:28 +03:00
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://ocr.api.cloud.yandex.net/ocr/v1/");
_apiKey = configuration["apiKey"];
_xFolderId = configuration["x-folder-id"];
2024-11-13 23:42:28 +03:00
}
public async Task<IEnumerable<object[,]>> ProcessImage(string base64Image)
2024-11-13 23:42:28 +03:00
{
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);
2024-11-13 23:42:28 +03:00
_httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true");
using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent);
2024-11-14 23:51:27 +03:00
if (response.IsSuccessStatusCode)
2024-11-13 23:42:28 +03:00
{
2024-11-14 23:51:27 +03:00
string jsonResponse = await response.Content.ReadAsStringAsync();
OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
if (deserialized != null)
2024-11-13 23:42:28 +03:00
{
2024-11-14 23:51:27 +03:00
var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>();
if (tables.Any())
2024-11-13 23:42:28 +03:00
{
2024-11-14 23:51:27 +03:00
List<object[,]> result = new();
foreach (var table in tables)
2024-11-13 23:42:28 +03:00
{
2024-11-14 23:51:27 +03:00
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);
2024-11-13 23:42:28 +03:00
}
2024-11-14 23:51:27 +03:00
return result;
2024-11-13 23:42:28 +03:00
}
}
}
2024-11-14 23:51:27 +03:00
else
{
string content = await response.Content.ReadAsStringAsync();
MessageBox.Show($"{response.StatusCode}: {content}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2024-11-13 23:43:27 +03:00
return Enumerable.Empty<object[,]>();
2024-11-13 23:42:28 +03:00
}
}