I am using Tesseract OCR to extract text from images. The code is working fine on my laptop whether in debug or release mode. But on other machines/laptops it not working. The program runs but the text is not extracted. Below is my code
private void Capturebutton_Click(object sender, EventArgs e)
{
ScannedText.Text = string.Empty;
pictureBox3.Image = null;
this.Hide();
using (var screenSelectionForm = new ScreenSelectionForm())
{
if (screenSelectionForm.ShowDialog() == DialogResult.OK)
{
Rectangle selectedArea = screenSelectionForm.SelectedArea;
if (selectedArea.Width > 0 && selectedArea.Height > 0)
{
Bitmap screenshot = new Bitmap(selectedArea.Width, selectedArea.Height);
using (Graphics graphics = Graphics.FromImage(screenshot))
{
graphics.CopyFromScreen(selectedArea.Location, Point.Empty, selectedArea.Size);
}
string tessdataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tesssdata");
if (!Directory.Exists(tessdataPath))
{
MessageBox.Show($"Tesseract data directory not found: {tessdataPath}");
return;
}
try
{
using (var engine = new TesseractEngine(tessdataPath, "eng", Tesseract.EngineMode.Default))
{
using (var page = engine.Process(screenshot))
{
string extractedText = page.GetText().Trim();
if (!string.IsNullOrEmpty(extractedText))
{
MessageBox.Show($"Extracted Text: {extractedText}");
if (extractedText.Length == 15 && long.TryParse(extractedText, out _))
{
pictureBox1.Image = screenshot;
pictureBox1.Size = screenshot.Size;
ScannedText.Text = extractedText;
pictureBox3.Image = null;
}
else
{
MessageBox.Show("The captured text is not a valid 15-digit number. Please try again.", "Invalid Text", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("No text was extracted from the image. Please try again.", "No Text Extracted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during OCR processing: {ex.Message}");
}
}
else
{
MessageBox.Show("No area was selected. Please select a valid area.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
this.Show();
}
The string extractedText = page.GetText().Trim();
is empty always. I am stuck and don’t know what to do
Any help would be highly appreciated.