I am trying to run gocr from my program, and use the output.
I have done this with success befor, with a workaround command line tool.:
Private irfanview As String = "C:\program files\IrfanView\i_view32.exe"
Private startuppath As String = Environment.CurrentDirectory & "\"
Private pngimage As String = startuppath & "verify.png"
Private png2pnm As String = startuppath & "png2pnm.exe"
Private gocr As String = startuppath & "gocr.exe"
Dim batfile As String = "@ECHO OFF" & vbCrLf & """C:\program
files\IrfanView\i_view32.exe"" verify.png /swap_bw /convert=flip.png" &
vbCrLf & "png2pnm.exe flip.png verify.pnm" & vbCrLf & "gocr verify.pnm"
Sub Main()
Try
downloadFile(args(1), pngimage)
Dim sw As System.IO.StreamWriter = New
System.IO.StreamWriter(startuppath & "process.bat")
sw.Write(batfile)
sw.Flush()
sw.Close()
Catch
End Try
Dim cmgProc As New System.Diagnostics.Process
Dim i As New System.Diagnostics.ProcessStartInfo
i.FileName = "process.bat"
i.Arguments = Nothing
i.RedirectStandardError = False
i.RedirectStandardInput = True
i.RedirectStandardOutput = True
i.CreateNoWindow = True
i.UseShellExecute = False
cmgProc.StartInfo = i
Try
cmgProc.Start()
Console.WriteLine(cmgProc.StandardOutput.ReadToEnd )
Catch
End Try
End Sub
but since this is just stupid and annoying, i wanted to do it properly.
I have tried the following, but this locks up the app:
Private Function gocr()
Dim p As Process = New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = False
p.StartInfo.RedirectStandardError = True
p.Start()
Dim swIn As System.IO.StreamWriter = p.StandardInput
Dim swOut As System.IO.StreamReader = p.StandardOutput()
swIn.AutoFlush = True
swIn.WriteLine("cd " & STARTUPPATH & vbCrLf)
swIn.WriteLine("gocr c:\image.png" & vbCrLf)
swIn.WriteLine("exit" & vbCrLf)
Return swOut.ReadToEnd
End Function
I have also tried, but is unpredictable in output for some reason:
Private Function gocr() As String
System.Diagnostics.Process.Start(STARTUPPATH & "bin\gocr.bat")
System.Threading.Thread.Sleep(333)
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(STARTUPPATH & "\bin\pwnd.ViRi")
Dim strOcrResult As String = sr.ReadToEnd.Trim
sr.Close()
Return strOcrResult
End Function
I have tried a dozen other things, and seem unable to get a solution :(
GOCR homepage: http://jocr.sourceforge.net/