如何使process.standardinput位图流在tesseract中工作?

问题描述

我试图捕获一部分桌面映像,然后使用tesseract.exe运行它,而不必将捕获的映像作为文件写到磁盘上。 但是,以下代码给我一个错误,否则一个过程将不会结束。请帮忙吗?

Bitmap bmp=capture_and_crop();
       using (Process process = new Process())
                        {                
                            process.StartInfo.FileName = ts5path+"tesseract.exe";
                        process.StartInfo.Arguments = "stdin" + " stdout -l " + language + " --psm " + PSM_setting + " -c preserve_interword_spaces=1";
                                                    process.StartInfo.UseShellExecute = false;
                            process.StartInfo.RedirectStandardInput = true;
                            process.StartInfo.RedirectStandardOutput = true;
                            process.StartInfo.RedirectStandardError = true;
                            process.StartInfo.CreateNoWindow = true;
                            process.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
        
                            process.Start();
                            
                            BinaryWriter writer = new BinaryWriter(process.StandardInput.BaseStream);//line1. I get error here : standardIn has not been redirected,when I move these 2 lines above process.Start().
                            bmp.Save(writer.BaseStream,System.Drawing.Imaging.ImageFormat.Bmp);//line2.
    //If these lines stay here there will be no redirection exception but this process will never end...
        
                            StreamReader reader = process.StandardOutput;
                            string output = reader.ReadToEnd();
                            addtr1.str = output;
                        
                        process.WaitForExit();
                        }

解决方法

BinaryWriter writer = new BinaryWriter(process.StandardInput.BaseStream);                                        
                    bmp.Save(writer.BaseStream,System.Drawing.Imaging.ImageFormat.Bmp);
    process.StandardInput.BaseStream.Close();

非常感谢jdweng。 process.StandardInput.BaseStream.Close();在将位图写入stdin基本流中以关闭流后立即需要该位,以便ReadtoEnd可以关闭并且程序可以在之后继续运行。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...