The Data Guy
Junior Member
- Mar 24, 2010
- 153
- 45
Code for client.
Code for server. Note that the server is a console application. You need to import the omni libraries written by a friend and make sure the tesseract.exe and tessdata folders are in the same folder as the server.
Code:
Imports Omni
Imports System.IO
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class Form1
Dim WithEvents C As New Nexus.Client
Const FILE_EXTENSION As String = ".txt"
Private m_images As Image
Private dpiX As Integer
Private dpiY As Integer
Private m_index As Integer
Private m_rect As Rectangle
Private lang As [String]
Private ocr_text As String
Dim Image As Bitmap = Bitmap.Fromfile(FILE)
Public ReadOnly Property ClonedImages As Image
Get
Return Clone(Image)
End Get
End Property
Public ReadOnly Property ImageFile As String
Get
Return CreateImageFiles(Image)
End Get
End Property
Public Property Index() As Integer
Get
Return m_index
End Get
Set(ByVal value As Integer)
m_index = value
End Set
End Property
Public Property Rect() As Rectangle
Get
Return m_rect
End Get
Set(ByVal value As Rectangle)
m_rect = value
End Set
End Property
Private Sub C_Incoming(ByVal c As Omni.Nexus.Client, ByVal d() As Byte) Handles C.Incoming
Dim Data As General.Storage = Shift.Deserialize(d)
Select Case Data.Data(0)
Case 1
RichTextBox1.Text = Data.Data(1)
Case 2
End Select
End Sub
Private Sub client1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
C.Connect("127.0.0.1", 4003)
End Sub
Private Function Clone(ByVal images As Bitmap)
Dim clonedImages As IList(Of Bitmap) = New List(Of Bitmap)()
If dpiX = 0 OrElse dpiY = 0 Then
If m_rect = Nothing OrElse m_rect = Rectangle.Empty Then
clonedImages.Add(images)
Else
clonedImages.Add(ImageHelper.Crop(images, m_rect))
m_rect = Rectangle.Empty
End If
Else
If m_rect = Nothing OrElse m_rect = Rectangle.Empty Then
clonedImages.Add(ImageHelper.Rescale(images, dpiX, dpiY))
Else
clonedImages.Add(ImageHelper.Rescale(ImageHelper.Crop(images, m_rect), dpiX, dpiY))
m_rect = Rectangle.Empty
End If
End If
Return clonedImages
End Function
Private Function CreateImageFiles(ByVal images As Bitmap) As String
Dim Files As String
Dim tempImageFile As String = Path.GetTempFileName()
File.Delete(tempImageFile)
tempImageFile = Path.ChangeExtension(tempImageFile, ".tif")
images.Save(tempImageFile, ImageFormat.Tiff)
Files = tempImageFile
Return Files
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Clone(Image)
CreateImageFiles(Image)
C.Send(Shift.Serialize(New General.Storage(1, ImageFile)))
End Sub
End Class
Class ImageHelper
Public Shared Function Rescale(ByVal image As Image, ByVal dpiX As Integer, ByVal dpiY As Integer) As Image
Dim bm As New Bitmap(CInt(image.Width * dpiX / image.HorizontalResolution), CInt(image.Height * dpiY / image.VerticalResolution))
bm.SetResolution(dpiX, dpiY)
Dim g As Graphics = Graphics.FromImage(bm)
g.InterpolationMode = InterpolationMode.Bicubic
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.DrawImage(image, 0, 0)
g.Dispose()
Return bm
End Function
Public Shared Function GetClipboardImage() As Image
If Clipboard.ContainsImage() Then
Return Clipboard.GetImage()
End If
Return Nothing
End Function
Public Shared Function Crop(ByVal image As Image, ByVal cropArea As Rectangle) As Image
Dim bmp As New Bitmap(cropArea.Width, cropArea.Height)
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution)
Dim gfx As Graphics = Graphics.FromImage(bmp)
gfx.DrawImage(image, 0, 0, cropArea, GraphicsUnit.Pixel)
gfx.Dispose()
Return bmp
End Function
Public Shared Function Rotate(ByVal image As Image, ByVal angle As Double) As Bitmap
Dim bm As New Bitmap(image.Width, image.Height)
bm.SetResolution(image.HorizontalResolution, image.VerticalResolution)
Dim g As Graphics = Graphics.FromImage(bm)
'move rotation point to center of image
g.TranslateTransform(CSng(image.Width) / 2, CSng(image.Height) / 2)
'rotate
g.RotateTransform(CSng(angle))
'move image back
g.TranslateTransform(-CSng(image.Width) / 2, -CSng(image.Height) / 2)
'draw passed in image onto graphics object
g.DrawImage(image, 0, 0)
g.Dispose()
Return bm
End Function
End Class
Code for server. Note that the server is a console application. You need to import the omni libraries written by a friend and make sure the tesseract.exe and tessdata folders are in the same folder as the server.
Code:
Imports Omni
Imports System.Text
Imports System.Drawing
Imports System.IO
Module Server1
Dim WithEvents S As New Nexus.Server
Const FILE_EXTENSION As String = ".txt"
Private lang As [String]
Private ocr_text As String
Public Property Language() As [String]
Get
Return lang
End Get
Set(ByVal value As [String])
lang = value
End Set
End Property
Sub Main()
S.Listen("4003")
Console.Read()
End Sub
Private Sub S_Incoming(ByVal c As Omni.Nexus.Client, ByVal d() As Byte) Handles S.Incoming
Dim Data As General.Storage = Shift.Deserialize(d)
Select Case Data.Data(0)
Case 1
c.Send(Shift.Serialize(New General.Storage(1, RecognizeText(Data.Data(1), "eng"))))
Console.WriteLine(Data.Data(1))
Console.WriteLine(RecognizeText(Data.Data(1), "eng"))
Case 2
End Select
End Sub
Public Function RecognizeText(ByVal ImageFile As String, ByVal lang As String) As String
Dim tempTessOutputFile As String = Path.GetTempFileName()
File.Delete(tempTessOutputFile)
tempTessOutputFile = Path.ChangeExtension(tempTessOutputFile, FILE_EXTENSION)
Dim outputFileName As String = tempTessOutputFile.Substring(0, tempTessOutputFile.Length - FILE_EXTENSION.Length)
Dim p As New Process()
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.FileName = "tesseract.exe"
Dim result As New StringBuilder()
p.StartInfo.Arguments = String.Format("""{0}"" ""{1}"" -l {2} +arc", ImageFile, outputFileName, lang)
p.Start()
Dim [error] As String = p.StandardError.ReadToEnd()
p.WaitForExit()
If p.ExitCode = 0 Then
Using sr As New StreamReader(tempTessOutputFile, Encoding.UTF8, True)
result.Append(sr.ReadToEnd())
End Using
Else
If [error].Trim().Length = 0 Then
File.OpenRead(tempTessOutputFile)
Console.WriteLine([error])
End If
End If
File.Delete(tempTessOutputFile)
Return result.ToString()
End Function
End Module