Covert to black and white
This commit is contained in:
parent
18033a355a
commit
5475392c3f
@ -117,8 +117,6 @@ public class FileRead : Shared.FileRead, IFileRead
|
|||||||
throw new Exception();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IFileRead.Callback(object state) => throw new Exception(string.Concat("Not ", nameof(_IsDummy)));
|
|
||||||
|
|
||||||
protected static List<Description> GetDescriptions(JsonElement[] jsonElements)
|
protected static List<Description> GetDescriptions(JsonElement[] jsonElements)
|
||||||
{
|
{
|
||||||
List<Description> results = new();
|
List<Description> results = new();
|
||||||
|
@ -108,8 +108,6 @@ public class FileRead : Shared.FileRead, IFileRead
|
|||||||
|
|
||||||
void IFileRead.CheckTests(Test[] tests, bool extra) => throw new Exception(string.Concat("Not ", nameof(_IsDuplicator)));
|
void IFileRead.CheckTests(Test[] tests, bool extra) => throw new Exception(string.Concat("Not ", nameof(_IsDuplicator)));
|
||||||
|
|
||||||
void IFileRead.Callback(object state) => Callback(state);
|
|
||||||
|
|
||||||
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
|
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
|
||||||
{
|
{
|
||||||
if (reportFullPath is null)
|
if (reportFullPath is null)
|
||||||
|
@ -105,41 +105,56 @@ public class ProcessData : IProcessData
|
|||||||
|
|
||||||
#pragma warning disable CA1416
|
#pragma warning disable CA1416
|
||||||
|
|
||||||
private static (byte[], Color[]) Get(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality, string saveFileName)
|
private static (MemoryStream memoryStream, Color[]) Get(IFileRead fileRead, int thresHold, int endY, int endX, int startY, int startX)
|
||||||
{
|
{
|
||||||
byte[] bytes;
|
Color color;
|
||||||
List<Color> colors = new();
|
List<Color> colors = new();
|
||||||
Point startPoint = new(startX, startY);
|
MemoryStream memoryStream = new();
|
||||||
using MemoryStream memoryStream = new();
|
int middle = (int)(endY - startY * .5);
|
||||||
EncoderParameters encoderParameters = new(1);
|
Bitmap selectedBitmap = new(endX - startX, endY - startY);
|
||||||
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 bitmap = Image.FromFile(fileRead.ReportFullPath) as Bitmap;
|
||||||
using Bitmap clonedBitmap = bitmap.Clone(rectangle, bitmap.PixelFormat);
|
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
|
||||||
if (!string.IsNullOrEmpty(saveFileName))
|
for (int x = startX; x < endX; x++)
|
||||||
{
|
{
|
||||||
if (!saveFileName.EndsWith(".png"))
|
for (int y = startY; y < endY; y++)
|
||||||
throw new NotImplementedException("Configured to save as *.png");
|
{
|
||||||
clonedBitmap.Save(saveFileName, System.Drawing.Imaging.ImageFormat.Png);
|
color = bitmap.GetPixel(x, y);
|
||||||
for (int i = 0; i < rectangle.Width; i++)
|
if (y == middle)
|
||||||
colors.Add(clonedBitmap.GetPixel(i, (int)(rectangle.Height * .5)));
|
colors.Add(color);
|
||||||
|
if (color.R > thresHold || color.G > thresHold || color.B > thresHold)
|
||||||
|
selectedBitmap.SetPixel(x - startX, y - startY, Color.Black);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
clonedBitmap.Save(memoryStream, imageCodecInfo, encoderParameters);
|
selectedBitmap.Save(memoryStream, imageFormat);
|
||||||
bytes = memoryStream.GetBuffer();
|
return new(memoryStream, colors.ToArray());
|
||||||
return new(bytes, colors.ToArray());
|
}
|
||||||
|
|
||||||
|
private static void SaveToFile(MemoryStream memoryStream, string extension, string saveFileName)
|
||||||
|
{
|
||||||
|
System.Drawing.Imaging.ImageFormat imageFormat = extension switch
|
||||||
|
{
|
||||||
|
".bmp" => System.Drawing.Imaging.ImageFormat.Bmp,
|
||||||
|
".gif" => System.Drawing.Imaging.ImageFormat.Gif,
|
||||||
|
".jpeg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||||
|
".jpg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||||
|
".png" => System.Drawing.Imaging.ImageFormat.Png,
|
||||||
|
".tiff" => System.Drawing.Imaging.ImageFormat.Tiff,
|
||||||
|
_ => throw new Exception("Extension not mapped"),
|
||||||
|
};
|
||||||
|
using Bitmap bitmap = new(memoryStream);
|
||||||
|
bitmap.Save(saveFileName, imageFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning restore CA1416
|
#pragma warning restore CA1416
|
||||||
|
|
||||||
private static (byte[], Color[]) Get(IFileRead fileRead, string saveFileName) => Get(fileRead, 68, 1559, 32, 1094, 95, saveFileName);
|
private static (MemoryStream, Color[]) Get(IFileRead fileRead, int thresHold) => Get(fileRead, thresHold, 68, 1559, 32, 1094);
|
||||||
|
|
||||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection)
|
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection)
|
||||||
{
|
{
|
||||||
|
int thresHold = 76;
|
||||||
|
(MemoryStream memoryStream, Color[] colors) = Get(fileRead, thresHold);
|
||||||
|
byte[] bytes = memoryStream.GetBuffer();
|
||||||
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
|
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 Pix img = Pix.LoadFromMemory(bytes);
|
||||||
using Page page = engine.Process(img);
|
using Page page = engine.Process(img);
|
||||||
string text = page.GetText().Trim();
|
string text = page.GetText().Trim();
|
||||||
@ -150,13 +165,14 @@ public class ProcessData : IProcessData
|
|||||||
int red = 0;
|
int red = 0;
|
||||||
int green = 0;
|
int green = 0;
|
||||||
_Log.Debug("Looking by color");
|
_Log.Debug("Looking by color");
|
||||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, ".png");
|
string extension = ".png";
|
||||||
(bytes, colors) = Get(fileRead, saveFileName);
|
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension);
|
||||||
|
SaveToFile(memoryStream, extension, saveFileName);
|
||||||
foreach (Color color in colors)
|
foreach (Color color in colors)
|
||||||
{
|
{
|
||||||
if (color.R > 127)
|
if (color.R > thresHold)
|
||||||
red += 1;
|
red += 1;
|
||||||
if (color.G > 127)
|
if (color.G > thresHold)
|
||||||
green += 1;
|
green += 1;
|
||||||
}
|
}
|
||||||
if (red > green)
|
if (red > green)
|
||||||
@ -165,6 +181,8 @@ public class ProcessData : IProcessData
|
|||||||
text = "Green*";
|
text = "Green*";
|
||||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||||
}
|
}
|
||||||
|
if (memoryStream is not null)
|
||||||
|
memoryStream.Dispose();
|
||||||
_Details.Add(text);
|
_Details.Add(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user