Timothy Wood
Timothy Wood

Reputation: 11

Error using Leptonica OCR library from VB.Net

I am writing a OCR app in Visual Basic and everything is going grand (the app installs). But on run I get a Leptonica Library error.

I have downloaded the latest version, however I am unable to find the make file utility. Is there not some sort of compiler that will help to build this file so it can be inserted into a project?

Imports System.Drawing.Printing
Imports Tesseract
Imports System.IO
Imports System.Threading.Tasks

Public Class Orc
    ' Tesseract engine initialization
    Private ReadOnly tesseractEngine As New TesseractEngine("C:\Users\Tim\Documents\POLLY\recontiontraining\", "eng", EngineMode.Default)

    Private Async Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click
        Try
            Using openFileDialog As New OpenFileDialog()
                openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
                openFileDialog.FilterIndex = 1

                If openFileDialog.ShowDialog() = DialogResult.OK Then
                    Dim fileName As String = openFileDialog.FileName
                    RichTextBox1.Text = Await Task.Run(Function() File.ReadAllText(fileName))
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show("Error opening file: " & ex.Message)
        End Try
    End Sub

    Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
        Try
            Dim printDocument As New PrintDocument()
            AddHandler printDocument.PrintPage, AddressOf PrintDocument_PrintPage

            Using printDialog As New PrintDialog()
                printDialog.Document = printDocument

                If printDialog.ShowDialog() = DialogResult.OK Then
                    printDocument.Print()
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show("Error printing document: " & ex.Message)
        End Try
    End Sub

    Private Sub PrintDocument_PrintPage(sender As Object, e As PrintPageEventArgs)
        Try
            e.Graphics.DrawString(RichTextBox1.Text, New Font("Arial", 12), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
        Catch ex As Exception
            MessageBox.Show("Error during print: " & ex.Message)
        End Try
    End Sub

    Private Async Sub BtnOcr_Click(sender As Object, e As EventArgs) Handles Btnpdfextractimagetextfrompdf.Click
        Try
            Using openFileDialog As New OpenFileDialog()
                openFileDialog.Filter = "Image Files (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png|All Files (*.*)|*.*"
                openFileDialog.FilterIndex = 1

                If openFileDialog.ShowDialog() = DialogResult.OK Then
                    Dim imageFilePath As String = openFileDialog.FileName
                    Dim ocrResult As String = Await PerformOcrAsync(imageFilePath)
                    RichTextBox1.Text = ocrResult

                    ' Place the image in PictureBox1
                    PictureBox1.Image = Image.FromFile(imageFilePath)
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show("Error performing OCR: " & ex.Message)
        End Try
    End Sub


    Private Async Function PerformOcrAsync(imageFilePath As String) As Task(Of String)
        Return Await Task.Run(Function()
                                  Try
                                      Using img = Pix.LoadFromFile(imageFilePath)
                                          Using page = tesseractEngine.Process(img)
                                              Return page.GetText()
                                          End Using
                                      End Using
                                  Catch ex As Exception
                                      MessageBox.Show("Error during OCR processing: " & ex.Message)
                                      Return String.Empty
                                  End Try
                              End Function)
    End Function



    Private Sub BtnOCR2_Click(sender As Object, e As EventArgs) Handles BtnOCR2.Click
        Try
            Using openFileDialog As New OpenFileDialog()
                openFileDialog.Filter = "Image Files (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png|All Files (*.*)|*.*"
                openFileDialog.FilterIndex = 1

                If openFileDialog.ShowDialog() = DialogResult.OK Then
                    Dim imageFilePath As String = openFileDialog.FileName

                    ' Place the image in PictureBox1
                    PictureBox1.Image = Image.FromFile(imageFilePath)
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show("Error loading image: " & ex.Message)
        End Try
    End Sub

Upvotes: 1

Views: 55

Answers (0)

Related Questions