using Adaptation.Eaf.Management.ConfigurationData.CellAutomation; using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration; using Adaptation.Shared; using Adaptation.Shared.Deposition; using Adaptation.Shared.Methods; using log4net; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Tesseract; namespace Adaptation.FileHandlers.jpeg; public class ProcessData : IProcessData { private readonly List _Details; private readonly ILog _Log; public string JobID { get; set; } public string MesEntity { get; set; } List Shared.Properties.IProcessData.Details => _Details; public ProcessData(IFileRead fileRead, Logistics logistics, List fileInfoCollection) { if (logistics is null) { } JobID = logistics.JobID; fileInfoCollection.Clear(); _Details = new List(); MesEntity = logistics.MesEntity; _Log = LogManager.GetLogger(typeof(ProcessData)); Parse(fileRead, fileInfoCollection); } private static string Get(string value, bool useSplitForMID) { string result = value; if (useSplitForMID) { if (result.IndexOf(".") > -1) result = result.Split('.')[0].Trim(); if (result.IndexOf("_") > -1) result = result.Split('_')[0].Trim(); if (result.IndexOf("-") > -1) result = result.Split('-')[0].Trim(); } result = string.Concat(result.Substring(0, 1).ToUpper(), result.Substring(1).ToLower()); return result; } string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary reactors) { string result = string.Empty; string filePrefixAsMID; foreach (KeyValuePair keyValuePair in reactors) { foreach (string filePrefix in keyValuePair.Value.Split('|')) { filePrefixAsMID = Get(filePrefix, useSplitForMID: true); if (logistics.MID.StartsWith(filePrefix) || logistics.MID.StartsWith(filePrefixAsMID)) { result = keyValuePair.Key; break; } } } if (string.IsNullOrEmpty(result) && reactors.Count == 1) result = reactors.ElementAt(0).Key; return result; } Tuple> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List fileInfoCollection) { Tuple> results; List tests = new(); foreach (object item in _Details) tests.Add(Test.CDE); List descriptions = fileRead.GetDescriptions(fileRead, tests, this); if (tests.Count != descriptions.Count) throw new Exception(); for (int i = 0; i < tests.Count; i++) { if (descriptions[i] is not Description description) throw new Exception(); if (description.Test != (int)tests[i]) throw new Exception(); } List fileReadDescriptions = (from l in descriptions select (Description)l).ToList(); string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType()); JsonElement[] jsonElements = JsonSerializer.Deserialize(json); results = new Tuple>(logistics.Logistics1[0], tests.ToArray(), jsonElements, fileInfoCollection); return results; } #pragma warning disable CA1416 private static (byte[], Color[]) Get(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality, string saveFileName) { byte[] bytes; List colors = new(); Point startPoint = new(startX, startY); using MemoryStream memoryStream = new(); EncoderParameters encoderParameters = new(1); System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; ImageCodecInfo imageCodecInfo = (from l in ImageCodecInfo.GetImageEncoders() where l.FormatID == imageFormat.Guid select l).First(); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, outputQuality); Rectangle rectangle = new(startPoint, new Size(endX - startX, endY - startY)); using Bitmap bitmap = Image.FromFile(fileRead.ReportFullPath) as Bitmap; using Bitmap clonedBitmap = bitmap.Clone(rectangle, bitmap.PixelFormat); if (!string.IsNullOrEmpty(saveFileName)) { if (!saveFileName.EndsWith(".png")) throw new NotImplementedException("Configured to save as *.png"); clonedBitmap.Save(saveFileName, System.Drawing.Imaging.ImageFormat.Png); for (int i = 0; i < rectangle.Width; i++) colors.Add(clonedBitmap.GetPixel(i, (int)(rectangle.Height * .5))); } clonedBitmap.Save(memoryStream, imageCodecInfo, encoderParameters); bytes = memoryStream.GetBuffer(); return new(bytes, colors.ToArray()); } #pragma warning restore CA1416 private static (byte[], Color[]) Get(IFileRead fileRead, string saveFileName) => Get(fileRead, 68, 1559, 32, 1094, 95, saveFileName); private void Parse(IFileRead fileRead, List fileInfoCollection) { using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default); //using Pix img = Pix.LoadFromFile(fileRead.ReportFullPath); (byte[] bytes, Color[] colors) = Get(fileRead, saveFileName: string.Empty); using Pix img = Pix.LoadFromMemory(bytes); using Page page = engine.Process(img); string text = page.GetText().Trim(); if (!string.IsNullOrEmpty(text)) _Log.Debug(text); else { int red = 0; int green = 0; _Log.Debug("Looking by color"); string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, ".png"); (bytes, colors) = Get(fileRead, saveFileName); foreach (Color color in colors) { if (color.R > 127) red += 1; if (color.G > 127) green += 1; } if (red > green) text = "Red*"; else text = "Green*"; fileInfoCollection.Add(new FileInfo(saveFileName)); } _Details.Add(text); } }