using Newtonsoft.Json; using Serwer.Core.Domain; using Serwer.Infrastructure.Framework; using Serwer.Infrastructure.Settings; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using Tesseract; namespace Serwer.Infrastructure.Services { public class ImageHandler : IImageHandler { private readonly ISet _files = new HashSet(); private readonly string _env; public ImageHandler(IHostEnviroment hostEnviroment) { _env = hostEnviroment.RootPath; } public async Task Process(string name, string contentType, byte[] bytes) { 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(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; } /* 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); */ } 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; } } }