From 9a609e9ef3b4c001aa0d3b5cf04671d49b61f1ec Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Wed, 31 May 2023 04:49:32 +0300
Subject: [PATCH] Added Additional Comments for Code Readability
Win10OcrProvider.cs
This commit introduces more inline comments to the Win10OcrProvider class. These comments aim to improve the understanding of the class functions and the flow of the OCR process. Key areas clarified include:
1. Logging of available OCR languages.
2. Handling of image sizing before OCR process.
3. The OCR process itself and the creation of the OCR result.
---
.../Win10OcrProvider.cs | 30 +++++++++++++++++--
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs b/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
index e33909581..f8269f545 100644
--- a/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
+++ b/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
@@ -41,7 +41,10 @@ namespace Greenshot.Plugin.Win10
///
public class Win10OcrProvider : IOcrProvider
{
+ // Log for debugging and information
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Win10OcrProvider));
+
+ // Minimum width and height for the OCR to work correctly
private const int MinWidth = 130;
private const int MinHeight = 130;
@@ -50,7 +53,10 @@ namespace Greenshot.Plugin.Win10
///
public Win10OcrProvider()
{
+ // Get available languages from the OCR engine and log them
var languages = OcrEngine.AvailableRecognizerLanguages;
+
+ // Log all available languages for OCR.
foreach (var language in languages)
{
Log.DebugFormat("Found language {0} {1}", language.NativeName, language.LanguageTag);
@@ -64,19 +70,26 @@ namespace Greenshot.Plugin.Win10
/// OcrResult sync
public async Task DoOcrAsync(ISurface surface)
{
+ // Will contain the result of the OCR process
OcrInformation result;
+
+ // Using a memory stream to handle the image
using (var imageStream = new MemoryStream())
{
// We only want the background
+ // Output settings for the image before OCR process
var outputSettings = new SurfaceOutputSettings(OutputFormat.png, 0, true)
{
ReduceColors = true,
SaveBackgroundOnly = true
};
- // Force Grayscale output
+ // Force Grayscale output to the image
outputSettings.Effects.Add(new GrayscaleEffect());
+
+ // If the surface is smaller than the minimum dimensions, resize it
if (surface.Image.Width < MinWidth || surface.Image.Height < MinHeight)
{
+ // Calculate dimensions to add
int addedWidth = MinWidth - surface.Image.Width;
if (addedWidth < 0)
{
@@ -95,13 +108,18 @@ namespace Greenshot.Plugin.Win10
{
addedHeight /= 2;
}
+ // Add a resize effect to the image
IEffect effect = new ResizeCanvasEffect(addedWidth, addedWidth, addedHeight, addedHeight);
outputSettings.Effects.Add(effect);
}
+ // Save the surface to the stream and reset position
ImageIO.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
+
+ // Create a random access stream from the memory stream
var randomAccessStream = imageStream.AsRandomAccessStream();
-
+
+ // Perform OCR on the stream
result = await DoOcrAsync(randomAccessStream);
}
@@ -158,8 +176,10 @@ namespace Greenshot.Plugin.Win10
{
var result = new OcrInformation();
+ // Iterate over all lines in the OCR result
foreach (var ocrLine in ocrResult.Lines)
{
+ // Create a new line and add it to the result
var line = new Line(ocrLine.Words.Count)
{
Text = ocrLine.Text
@@ -167,12 +187,16 @@ namespace Greenshot.Plugin.Win10
result.Lines.Add(line);
+ // Loop through each word in the line and process it.
for (var index = 0; index < ocrLine.Words.Count; index++)
{
var ocrWord = ocrLine.Words[index];
+
+ // Create the bounding rectangle for the word
var location = new NativeRect((int) ocrWord.BoundingRect.X, (int) ocrWord.BoundingRect.Y,
(int) ocrWord.BoundingRect.Width, (int) ocrWord.BoundingRect.Height);
+ // Add the word to the line
var word = line.Words[index];
word.Text = ocrWord.Text;
word.Bounds = location;
@@ -182,4 +206,4 @@ namespace Greenshot.Plugin.Win10
return result;
}
}
-}
\ No newline at end of file
+}