我已经安装了测试服务器,我正在使用按钮点击来设置tesseract.exe文件的位置。我还使用了另一个单击按钮来设置图像文件的位置。现在,我想要点击第三个按钮,用tesseract处理图像,因为我已经存储了它们各自的位置。我正在使用一些基本的粗略方法,但它适合我。我的代码如下:
private void B8_Click(object sender, EventArgs e)
{
q = z + "\\" + "output.txt";
if (k != null)
{
Process pr = new Process();
pr.StartInfo.FileName = j;
pr.StartInfo.Arguments = k + " " + q;
pr.Start();
pr.WaitForExit();
}
else
{
MessageBox.Show("No Image File Selected.");
}
var filetext = File.ReadAllText(q);
tb5.Text = filetext;
//File.Delete(q);
}
private void B10_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
j = "\"" + openFileDialog1.FileName + "\"";
MessageBox.Show("Tesseract Location Set: " + j);
}
}
private void B9_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
k = "\"" + openFileDialog1.FileName + "\"";
MessageBox.Show("Image File Location Set: " + k);
}
}到目前为止,我的三键点击故事:
我已经成功地使用1个按钮来设置tesseract.exe路径,用2个按钮来设置图像路径,但是3个按钮(参见B-8)有一个问题。它提取文本并存储到一个"output.txt“文件中。但是,我无法将此文本导入到我的textbox tb5中,然后销毁此文件。
我得到的错误是Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Could not find file 'C:\Users\ambij\Desktop\try\output.txt'.,我不明白这一点,但实际上output.txt文件驻留在文件夹中。
发布于 2019-08-04 02:26:58
以下内容适用于Tesseract 3.05.02 -可在更高版本中使用
private void RunIt()
{
string tessDataPath = yourTessDataPath; // Your Tesseract Location Set
string imagePath = yourImagePath; // The Image File Location
string theTextFromTheImage = DoOCR(yourTessDataPath, yourImagePath);
// Some formatting may be required - OCR isn't perfect
MessageBox.Show(theTextFromTheImage);
}
private string DoOCR(string tessdataPath, string filePath)
{
string returnText = "";
using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default))
{
// engine.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); // Only regular letters
string theVersion = engine.Version; // Optional, but useful
using (var img = Pix.LoadFromFile(filePath))
{
using (var page = engine.Process(img))
{
returnText = page.GetText();
}
}
}
return returnText;
}https://stackoverflow.com/questions/57340763
复制相似问题