Added sample images

Added ClearImage for barcode reading
This commit is contained in:
Ray 2024-08-05 15:24:13 +01:00
parent e81691ff53
commit be4f973a9f
10 changed files with 208 additions and 58 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 KiB

46
Form1.Designer.cs generated
View File

@ -29,23 +29,60 @@
private void InitializeComponent() private void InitializeComponent()
{ {
button1 = new Button(); button1 = new Button();
richTextBox1 = new RichTextBox();
button2 = new Button();
button3 = new Button();
SuspendLayout(); SuspendLayout();
// //
// button1 // button1
// //
button1.Location = new Point(574, 237); button1.Location = new Point(549, 15);
button1.Name = "button1"; button1.Name = "button1";
button1.Size = new Size(75, 23); button1.Size = new Size(208, 59);
button1.TabIndex = 0; button1.TabIndex = 0;
button1.Text = "button1"; button1.Text = "Tesseract";
button1.UseVisualStyleBackColor = true; button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click; button1.Click += button1_Click;
// //
// richTextBox1
//
richTextBox1.DetectUrls = false;
richTextBox1.HideSelection = false;
richTextBox1.Location = new Point(9, 15);
richTextBox1.Name = "richTextBox1";
richTextBox1.Size = new Size(511, 402);
richTextBox1.TabIndex = 1;
richTextBox1.Text = "";
richTextBox1.WordWrap = false;
//
// button2
//
button2.Location = new Point(549, 122);
button2.Name = "button2";
button2.Size = new Size(208, 59);
button2.TabIndex = 0;
button2.Text = "ClearImage";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// button3
//
button3.Location = new Point(549, 249);
button3.Name = "button3";
button3.Size = new Size(208, 59);
button3.TabIndex = 2;
button3.Text = "button1";
button3.UseVisualStyleBackColor = true;
button3.Click += button3_Click;
//
// Form1 // Form1
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(button3);
Controls.Add(richTextBox1);
Controls.Add(button2);
Controls.Add(button1); Controls.Add(button1);
Name = "Form1"; Name = "Form1";
Text = "Form1"; Text = "Form1";
@ -55,5 +92,8 @@
#endregion #endregion
private Button button1; private Button button1;
private RichTextBox richTextBox1;
private Button button2;
private Button button3;
} }
} }

217
Form1.cs
View File

@ -1,88 +1,197 @@
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Forms;
using Inlite.ClearImageNet;
using Tesseract; using Tesseract;
namespace WinFormsApp3 namespace WinFormsApp3
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
private readonly string _basePath;
private readonly string _tessDataPath;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
_basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\");
_tessDataPath = Path.Combine(_basePath, "#research", "tessdata");
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
richTextBox1.Clear();
} }
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
var basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\"); var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg");
var testImagePath = Path.Combine(basePath, "Scan_ADFPA_Letter_page-0001.jpg"); richTextBox1.Text += ParseOCR(testImagePath);
richTextBox1.Text += Environment.NewLine;
}
private void button2_Click(object sender, EventArgs e)
{
var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg");
var barcodes = ReadBarcode(testImagePath);
foreach (var barcode in barcodes)
{
richTextBox1.Text += $"Barcode Type = {barcode.Type} | Value = {barcode.Text} | Page = {barcode.Page}";
richTextBox1.Text += Environment.NewLine;
}
}
private void button3_Click(object sender, EventArgs e)
{
var testImagePath = Path.Combine(_basePath, "#research", "Passport Demand Study Sample Letter.jpg");
richTextBox1.Text += Environment.NewLine;
}
////var basePath = Path.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath) + "\\..\\..\\..\\");
////var tt2 = File.Exists(testImagePath);
//try
//{
// using (var engine = new TesseractEngine(_tessDataPath, "eng", EngineMode.Default))
// {
// using (var img = Pix.LoadFromFile(testImagePath))
// {
// using (var page = engine.Process(img))
// {
// Console.WriteLine("Mean Confidence = {0}", page.GetMeanConfidence());
// //var tt1 = page.GetLSTMBoxText(0);
// //var tt1 = page.GetAltoText(0);
// //var tt1 = page.GetBoxText(0);
// //var tt1 = page.GetHOCRText(0);
// //var tt1 = page.GetMeanConfidence();
// var tt1 = page.GetText();
// //var tt1 = page.GetUNLVText();
// //Console.WriteLine("Text (GetText): \r\n{0}", text);
// //Console.WriteLine("Text (iterator):");
// //using (var iter = page.GetIterator())
// //{
// // iter.Begin();
// // do
// // {
// // do
// // {
// // do
// // {
// // do
// // {
// // if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
// // {
// // Console.WriteLine("<BLOCK>");
// // }
// // Console.Write(iter.GetText(PageIteratorLevel.Word));
// // Console.Write(" ");
// // if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
// // {
// // Console.WriteLine();
// // }
// // } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
// // if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
// // {
// // Console.WriteLine();
// // }
// // } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
// // } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
// // } while (iter.Next(PageIteratorLevel.Block));
// //}
// }
// }
// }
//}
//catch (Exception exc)
//{
// //Trace.TraceError(exc.ToString());
// Console.WriteLine("Unexpected Error: " + exc.Message);
// //Console.WriteLine("Details: ");
// //Console.WriteLine(exc.ToString());
//}
//}
private string ParseOCR(string filename)
{
string response = null;
try try
{ {
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) using (var engine = new TesseractEngine(_tessDataPath, "eng", EngineMode.Default))
{ {
using (var img = Pix.LoadFromFile(testImagePath)) using (var img = Pix.LoadFromFile(filename))
{ {
using (var page = engine.Process(img)) using (var page = engine.Process(img))
{ {
Debug.WriteLine("Mean Confidence = {0}", page.GetMeanConfidence());
//var tt2 = page.GetAltoText(0);
//var tt3 = page.GetBoxText(0);
//var tt4 = page.GetHOCRText(0);
//var tt1 = page.GetLSTMBoxText(0); //var tt1 = page.GetLSTMBoxText(0);
//var tt1 = page.GetAltoText(0); //var tt5 = page.GetMeanConfidence();
//var tt1 = page.GetBoxText(0); //var tt7 = page.GetTsvText(0);
//var tt1 = page.GetHOCRText(0); //var tt6 = page.GetUNLVText();
//var tt1 = page.GetMeanConfidence();
//var tt1 = page.GetText();
var tt1 = page.GetUNLVText(); response = page.GetText();
//Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence());
//Console.WriteLine("Text (GetText): \r\n{0}", text);
//Console.WriteLine("Text (iterator):");
//using (var iter = page.GetIterator())
//{
// iter.Begin();
// do
// {
// do
// {
// do
// {
// do
// {
// if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
// {
// Console.WriteLine("<BLOCK>");
// }
// Console.Write(iter.GetText(PageIteratorLevel.Word));
// Console.Write(" ");
// if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
// {
// Console.WriteLine();
// }
// } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
// if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
// {
// Console.WriteLine();
// }
// } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
// } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
// } while (iter.Next(PageIteratorLevel.Block));
//}
} }
} }
} }
} }
catch (Exception exc) catch (Exception exc)
{ {
Trace.TraceError(exc.ToString()); // error
Console.WriteLine("Unexpected Error: " + exc.Message);
Console.WriteLine("Details: ");
Console.WriteLine(exc.ToString());
} }
return response;
}
private Barcode[] ReadBarcode(string filename)
{
var result = new Barcode[0];
try
{
using (BarcodeReader reader = new BarcodeReader())
{
reader.Code39 = true;
reader.Code128 = true;
reader.Upca = true;
reader.Upce = true;
reader.QR = true;
result = reader.Read(filename, 0);
//foreach (var barcode in result)
//{
// Debug.WriteLine($"Barcode type: {barcode.Type} Barcode page: {barcode.Page} Text: {barcode.Text}");
//}
}
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
}
return result;
} }
} }

View File

@ -2,13 +2,14 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework> <TargetFramework>net8.0-windows8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ClearImage.BarcodeReader.IP" Version="12.0.7675" />
<PackageReference Include="Tesseract" Version="5.2.0" /> <PackageReference Include="Tesseract" Version="5.2.0" />
</ItemGroup> </ItemGroup>