Cropped Image

This commit is contained in:
Mike Phares 2022-02-23 18:13:30 -07:00
parent 69f48e9034
commit 9ceed5b5a4

View File

@ -7,6 +7,8 @@ using log4net;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -33,8 +35,6 @@ public class ProcessData : IProcessData
{ {
if (logistics is null) if (logistics is null)
{ } { }
if (fileInfoCollection is null)
{ }
JobID = logistics.JobID; JobID = logistics.JobID;
fileInfoCollection.Clear(); fileInfoCollection.Clear();
_Details = new List<object>(); _Details = new List<object>();
@ -103,51 +103,42 @@ public class ProcessData : IProcessData
return results; return results;
} }
#pragma warning disable CA1416
private static byte[] GetBytes(IFileRead fileRead, int endY, int endX, int startY, int startX, int outputQuality)
{
byte[] results;
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);
//clonedBitmap.Save(Path.ChangeExtension(fileRead.ReportFullPath, ".png"), System.Drawing.Imaging.ImageFormat.Png);
clonedBitmap.Save(memoryStream, imageCodecInfo, encoderParameters);
results = memoryStream.GetBuffer();
return results;
}
#pragma warning restore CA1416
private static byte[] GetBytes(IFileRead fileRead) => GetBytes(fileRead, 68, 1687, 32, 1094, 95);
private void Parse(IFileRead fileRead) private void Parse(IFileRead fileRead)
{ {
List<string> blocks = new(); string text;
StringBuilder stringBuilder = new();
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); //using Pix img = Pix.LoadFromFile(fileRead.ReportFullPath);
byte[] bytes = GetBytes(fileRead);
using Pix img = Pix.LoadFromMemory(bytes);
using Page page = engine.Process(img); using Page page = engine.Process(img);
string text = page.GetText(); text = page.GetText();
_Log.Debug(string.Format("Mean confidence: {0}", page.GetMeanConfidence())); if (string.IsNullOrEmpty(text))
_Log.Debug(string.Format("Text (GetText): \r\n{0}", text)); throw new Exception("OCR Failure!");
_Log.Debug("Text (iterator):"); _Details.Add(text);
using ResultIterator iter = page.GetIterator();
iter.Begin();
do
{
do
{
do
{
do
{
_ = stringBuilder.Append(iter.GetText(PageIteratorLevel.Word)).Append(' ');
if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word) && stringBuilder.Length > 0)
{
blocks.Add(stringBuilder.ToString());
_ = stringBuilder.Clear();
}
} while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
_ = stringBuilder.AppendLine();
} while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
} while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
} while (iter.Next(PageIteratorLevel.Block));
if (stringBuilder.Length > 0)
blocks.Add(stringBuilder.ToString());
if (!blocks.Any())
_Details.Add(text);
else
{
blocks = (from l in blocks where l.Split(':').Length == 3 select l).ToList();
if (!blocks.Any())
_Details.Add(text);
else
_Details.Add(blocks[0]);
}
} }
} }