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();
|
||||
}
|
||||
|
||||
void IFileRead.Callback(object state) => throw new Exception(string.Concat("Not ", nameof(_IsDummy)));
|
||||
|
||||
protected static List<Description> GetDescriptions(JsonElement[] jsonElements)
|
||||
{
|
||||
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.Callback(object state) => Callback(state);
|
||||
|
||||
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
|
||||
{
|
||||
if (reportFullPath is null)
|
||||
|
@ -105,41 +105,56 @@ public class ProcessData : IProcessData
|
||||
|
||||
#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();
|
||||
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));
|
||||
MemoryStream memoryStream = new();
|
||||
int middle = (int)(endY - startY * .5);
|
||||
Bitmap selectedBitmap = new(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))
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
|
||||
for (int x = startX; x < endX; x++)
|
||||
{
|
||||
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)));
|
||||
for (int y = startY; y < endY; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
if (y == middle)
|
||||
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);
|
||||
bytes = memoryStream.GetBuffer();
|
||||
return new(bytes, colors.ToArray());
|
||||
selectedBitmap.Save(memoryStream, imageFormat);
|
||||
return new(memoryStream, 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
|
||||
|
||||
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)
|
||||
{
|
||||
int thresHold = 76;
|
||||
(MemoryStream memoryStream, Color[] colors) = Get(fileRead, thresHold);
|
||||
byte[] bytes = memoryStream.GetBuffer();
|
||||
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();
|
||||
@ -150,13 +165,14 @@ public class ProcessData : IProcessData
|
||||
int red = 0;
|
||||
int green = 0;
|
||||
_Log.Debug("Looking by color");
|
||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, ".png");
|
||||
(bytes, colors) = Get(fileRead, saveFileName);
|
||||
string extension = ".png";
|
||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension);
|
||||
SaveToFile(memoryStream, extension, saveFileName);
|
||||
foreach (Color color in colors)
|
||||
{
|
||||
if (color.R > 127)
|
||||
if (color.R > thresHold)
|
||||
red += 1;
|
||||
if (color.G > 127)
|
||||
if (color.G > thresHold)
|
||||
green += 1;
|
||||
}
|
||||
if (red > green)
|
||||
@ -165,6 +181,8 @@ public class ProcessData : IProcessData
|
||||
text = "Green*";
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
}
|
||||
if (memoryStream is not null)
|
||||
memoryStream.Dispose();
|
||||
_Details.Add(text);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user