-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCRHelper.cs
More file actions
109 lines (95 loc) · 4.04 KB
/
Copy pathOCRHelper.cs
File metadata and controls
109 lines (95 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using Tesseract;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public static class OCRHelper
{
public static Bitmap PreprocessImage(Bitmap original)
{
Bitmap gray = new Bitmap(original.Width, original.Height);
using (Graphics g = Graphics.FromImage(gray))
{
var colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][] {
new float[] { .3f, .3f, .3f, 0, 0 },
new float[] { .59f, .59f, .59f, 0, 0 },
new float[] { .11f, .11f, .11f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
});
var attributes = new System.Drawing.Imaging.ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(original, new Rectangle(0, 0, gray.Width, gray.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
}
for (int y = 0; y < gray.Height; y++)
{
for (int x = 0; x < gray.Width; x++)
{
Color pixel = gray.GetPixel(x, y);
int brightness = (pixel.R + pixel.G + pixel.B) / 3;
gray.SetPixel(x, y, brightness < 130 ? Color.Black : Color.White);
}
}
return gray;
}
public static string ExtractTextFromImage(Bitmap image)
{
try
{
Bitmap processed = PreprocessImage(image);
string tessDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessdata");
if (!Directory.Exists(tessDataPath) || !File.Exists(Path.Combine(tessDataPath, "eng.traineddata")))
{
MessageBox.Show("❌ لم يتم العثور على tessdata أو eng.traineddata!", "OCR Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "[OCR error: Missing tessdata or traineddata]";
}
using (var engine = new TesseractEngine(tessDataPath, "eng", EngineMode.Default))
{
using (var pix = PixConverter.ToPix(processed))
{
using (var page = engine.Process(pix))
{
string result = page.GetText();
if (string.IsNullOrWhiteSpace(result) || result.Trim().Length < 5)
{
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
processed = PreprocessImage(image);
using (var pix2 = PixConverter.ToPix(processed))
using (var page2 = engine.Process(pix2))
{
result = page2.GetText();
}
}
return result;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"OCR error during image processing: {ex.Message}");
return $"[OCR error: {ex.Message}]";
}
}
public static List<string> ExtractTIDsFromText(string text)
{
List<string> tids = new List<string>();
var matches = Regex.Matches(text, @"TID[\s:]*([A-Z0-9\-]{6,15})|(?<!\d)(\d{8})(?!\d)", RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
if (match.Groups.Count > 1)
{
tids.Add(match.Groups[1].Value);
}
}
return tids;
}
}
}