Visual Studio x86工具,普通cmd.exe,Process.Start()

时间:2013-10-24 07:40:25

标签: c# visual-studio command tesseract

我需要编写一些代码来调用C#中的tesseract OCR。我安装它并使用以下代码。但它不起作用:

  ProcessStartInfo startInfo = new ProcessStartInfo();
  startInfo.FileName = "cmd.exe";
  startInfo.WorkingDirectory = tempDir.FullName;

  // doesn't work
  startInfo.Arguments = string.Format("/C tesseract {0} {1}", imgName, textName);
  // this works
  //startInfo.Arguments = string.Format("/C copy {0} {1}", imgName, textName);

  Process p = new Process();
  p.StartInfo = startInfo;
  p.Start();
  p.WaitForExit();
  p.Close();

不会抛出任何异常或错误。我只是无法获取目录中的输出文件。我尝试了一个内置的命令,如copy,它评论并且有效。我试图得到进程的标准输出,但它总是抛出“进程退出”异常。

之后我尝试在命令窗口中调用tesseract。我进入临时目录,运行tesseract img.png output,这里感兴趣的事情发生了:

  1. 通过Start-> Run-> cmd启动命令窗口时,它可以正常工作。
  2. 在Visual Studio解决方案资源管理器中启动命令窗口时,右键单击 - >打开命令提示符(这是VS Productivity Power Tools的一项功能),它显示“tesseract未被识别为内部或外部命令”。
  3. 我在环境变量中检查PATH,这是正确的。我能看到的唯一区别是VS提示符显示“使用Microsoft Visual Studio 2010 x86工具设置环境”。在顶端。它不搜索PATH变量来查找命令吗?他们不是一回事吗?它是否与我的C#代码失败有关?

    我的操作系统是Windows Server 2008 64位。

1 个答案:

答案 0 :(得分:1)

我使用它的方式如下:

Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "tesseract.exe";
p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" -l {2} -psm {3} {4}", imageFile, outputFileName, language, PageSegMode, Hocr ? "hocr" : string.Empty);
p.Start();
p.WaitForExit();
if (p.ExitCode == 0)
{
   // read output text file
}
p.Close();