Removed IFileRead.Callback

This commit is contained in:
2022-02-24 17:11:43 -07:00
parent 9ceed5b5a4
commit 18033a355a
7 changed files with 56 additions and 45 deletions

View File

@ -40,7 +40,7 @@ public class ProcessData : IProcessData
_Details = new List<object>();
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead);
Parse(fileRead, fileInfoCollection);
}
private static string Get(string value, bool useSplitForMID)
@ -105,9 +105,10 @@ public class ProcessData : IProcessData
#pragma warning disable CA1416
private static byte[] GetBytes(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality)
private static (byte[], Color[]) Get(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality, string saveFileName)
{
byte[] results;
byte[] bytes;
List<Color> colors = new();
Point startPoint = new(startX, startY);
using MemoryStream memoryStream = new();
EncoderParameters encoderParameters = new(1);
@ -117,27 +118,53 @@ public class ProcessData : IProcessData
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);
//clonedBitmap.Save(Path.ChangeExtension(fileRead.ReportFullPath, ".png"), System.Drawing.Imaging.ImageFormat.Png);
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);
results = memoryStream.GetBuffer();
return results;
bytes = memoryStream.GetBuffer();
return new(bytes, colors.ToArray());
}
#pragma warning restore CA1416
private static byte[] GetBytes(IFileRead fileRead) => GetBytes(fileRead, 68, 1687, 32, 1094, 95);
private static (byte[], Color[]) Get(IFileRead fileRead, string saveFileName) => Get(fileRead, 68, 1559, 32, 1094, 95, saveFileName);
private void Parse(IFileRead fileRead)
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection)
{
string text;
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
//using Pix img = Pix.LoadFromFile(fileRead.ReportFullPath);
byte[] bytes = GetBytes(fileRead);
(byte[] bytes, Color[] colors) = Get(fileRead, saveFileName: string.Empty);
using Pix img = Pix.LoadFromMemory(bytes);
using Page page = engine.Process(img);
text = page.GetText();
if (string.IsNullOrEmpty(text))
throw new Exception("OCR Failure!");
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);
}