Home » c# » How to properly build a project in C# to include tessnet2?-Exceptionshub

How to properly build a project in C# to include tessnet2?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I created a Windows Form App in Visual Studio 2019 that utilizes the scanner attached on the local machine, and then extract the text from it then display the text on any web app thru a web socket. App runs as expected by clicking Start on the interface of visual studio.

But when I try to create a Setup Project, install it in a computer, the app was unable to use the tessnet2. It does not extract the text, it just displays its preview.

Here’s a code snippet on how I implemented it:

 if (img != null) {
   Directory.CreateDirectory("tempDir");
   TempFileCollection tfc = new TempFileCollection("tempDir", false);
   string tempFile = tfc.AddExtension("png");
   Console.WriteLine(tempFile);
   img.Save(tempFile, ImageFormat.Png);;
   Console.WriteLine("Image Save");

   try {

     var image = new Bitmap(tempFile);
     var ocr = new Tesseract();

     ocr.Init(@ ".\tessdata", "eng", false);
     List < tessnet2.Word > result = ocr.DoOCR(image, Rectangle.Empty);

     //Obtain the texts from OCR result
     String[] texts = result.ConvertAll < String > (delegate(Word w) {
       return w.Text;
     }).ToArray();
     String Result = String.Join(" ", texts);

     foreach(var socket in allSockets.ToList()) {
       socket.Send(Result);
       PlatformInfo.Current.Log.Info(Result);
       PlatformInfo.Current.Log.Info("extracted text sent");
     }

     tfc.Delete();
     PlatformInfo.Current.Log.Info("Tempfile deleted");
     Console.WriteLine(tfc.Count);

   } catch (Exception exception) {
     Console.WriteLine("error dude");
     Console.WriteLine(exception);
   }
 }

Here is also a screenshot of the project structure.
enter image description here

How to&Answers: