2021-01-16 18:45:20 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Serwer.Core.Domain;
|
|
|
|
|
using Serwer.Infrastructure.Framework;
|
2020-12-20 18:03:20 +01:00
|
|
|
|
using Serwer.Infrastructure.Settings;
|
|
|
|
|
using System;
|
2020-12-12 14:18:31 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-01-16 18:45:20 +01:00
|
|
|
|
using System.Net;
|
2020-12-12 14:18:31 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-12-20 18:03:20 +01:00
|
|
|
|
using Tesseract;
|
2020-12-12 14:18:31 +01:00
|
|
|
|
|
|
|
|
|
namespace Serwer.Infrastructure.Services
|
|
|
|
|
{
|
2020-12-20 18:03:20 +01:00
|
|
|
|
public class ImageHandler : IImageHandler
|
2020-12-12 14:18:31 +01:00
|
|
|
|
{
|
2020-12-20 18:03:20 +01:00
|
|
|
|
private readonly ISet<File> _files = new HashSet<File>();
|
|
|
|
|
private readonly string _env;
|
|
|
|
|
public ImageHandler(IHostEnviroment hostEnviroment)
|
|
|
|
|
{
|
|
|
|
|
_env = hostEnviroment.RootPath;
|
|
|
|
|
}
|
|
|
|
|
public async Task<string> Process(string name, string contentType, byte[] bytes)
|
|
|
|
|
{
|
2021-01-16 18:45:20 +01:00
|
|
|
|
string license_code = "FC935C59-F248-48A3-9970-8A6BDB66FA86";
|
|
|
|
|
string user_name = "poszukiwacz";
|
|
|
|
|
|
|
|
|
|
string ocrURL = @"http://www.ocrwebservice.com/restservices/processDocument?gettext=true&outputformat=txt&language=polish";
|
|
|
|
|
byte[] uploadData = bytes;
|
|
|
|
|
|
|
|
|
|
HttpWebRequest request = CreateHttpRequest(ocrURL, user_name, license_code, "POST");
|
|
|
|
|
request.ContentLength = uploadData.Length;
|
|
|
|
|
|
|
|
|
|
// Send request
|
|
|
|
|
using (System.IO.Stream post = request.GetRequestStream())
|
|
|
|
|
{
|
|
|
|
|
post.Write(uploadData, 0, (int)uploadData.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Get response
|
|
|
|
|
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
|
|
|
|
|
{
|
|
|
|
|
// Parse JSON response
|
|
|
|
|
string strJSON = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
|
|
|
OCRResponseData ocrResponse = JsonConvert.DeserializeObject<OCRResponseData>(strJSON);
|
|
|
|
|
|
|
|
|
|
return await Task.FromResult(ocrResponse.OCRText.First().First());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (WebException wex)
|
|
|
|
|
{
|
|
|
|
|
var x = string.Format("OCR API Error. HTTPCode:{0}", ((HttpWebResponse)wex.Response).StatusCode);
|
|
|
|
|
throw wex;
|
|
|
|
|
}
|
|
|
|
|
/*
|
2020-12-20 18:03:20 +01:00
|
|
|
|
var engine = new TesseractEngine(System.IO.Path.Combine(_env, "tessdata"),"eng+equ", EngineMode.Default);
|
|
|
|
|
var img = Pix.LoadFromMemory(bytes);
|
|
|
|
|
var res = engine.Process(img);
|
|
|
|
|
var txt = res.GetText();
|
|
|
|
|
|
|
|
|
|
return await Task.FromResult(txt);
|
2021-01-16 18:45:20 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private static HttpWebRequest CreateHttpRequest(string address_url, string user_name, string license_code, string http_method)
|
|
|
|
|
{
|
|
|
|
|
Uri address = new Uri(address_url);
|
|
|
|
|
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
|
|
|
|
|
|
|
|
|
|
byte[] authBytes = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", user_name, license_code).ToCharArray());
|
|
|
|
|
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
|
|
|
|
|
request.Method = http_method;
|
|
|
|
|
request.Timeout = 600000;
|
|
|
|
|
|
|
|
|
|
// Specify Response format to JSON or XML (application/json or application/xml)
|
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
|
|
|
|
|
return request;
|
2020-12-20 18:03:20 +01:00
|
|
|
|
}
|
2020-12-12 14:18:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|