Only digits from tesseract - OCR on VB? - vb.net

I needed an app that observed numbers in my screen and then make calculations with it, so after some days on researching the best and easiest method i found this video
(https://www.youtube.com/watch?v=Kjdu8SjEtG0) that leaded me to OCR and EMGU-Tesseract on Visual Basic 2010 express. I understanded the video and I made my own variation of the code on the description of the video.
I imported:
Imports Emgu.CV
Imports Emgu.Util
Imports Emgu.CV.OCR
Imports Emgu.CV.Structure
then i make this based on the original code:
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
Dim picStc1 As Bitmap = New Bitmap(149, 28)
Dim gfxSTK1 As Graphics = Graphics.FromImage(picStc1)
Dim picNam1 As Bitmap = New Bitmap(149, 28)
Dim gfxNAM1 As Graphics = Graphics.FromImage(picNam1)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
gfxSTK1.CopyFromScreen(New Point(Me.Location.X + Stk1.Location.X + 5, Me.Location.Y + Stk1.Location.Y + 24), New Point(0, 0), picStc1.Size)
Stk1.Image = picStc1
gfxNAM1.CopyFromScreen(New Point(Me.Location.X + Nome1.Location.X + 5, Me.Location.Y + Nome1.Location.Y + 24), New Point(0, 0), picNam1.Size)
Nome1.Image = picNam1
And when i pressed the button i get this :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OCRz.Recognize(New Image(Of Bgr, Byte)(picStc1))
BOXSTK1.Text = OCRz.GetText
OCRz.Recognize(New Image(Of Bgr, Byte)(picNam1))
BoxNAME1.Text = OCRz.GetText
I now have the text read from the PictureBoxes (picStc1) and (picNam1) thru the OCR engine and its writen on the RichTextBoxes (BoxSTK1) and (NAME1) after i pressed the button.
The numbers on the RichTextBox (BoxSTK1) come with commas and other simbols but i just want to grab the numbers. So i found this (https://code.google.com/p/tesseract-ocr/wiki/FAQ#How_do_I_recognize_only_digits?) but i cant implement it on the project, any help on this?
(I´m using Emgu 2.9.0.1922, dont know how to see the version of Tesseract)

This digit-based "whitelist" appears to be something you'd set when you initialize the object.
Check out this question
So you will need to change,
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
To something like this,
Dim OCRz As Tesseract = New Tesseract()
OCRz.SetVariable("tessedit_char_whitelist", "0123456789")
OCRz.init("tessdata", "eng", false)

Ok people, this problem is solved! Thanks to Mr.Jimmy Smith!
Now we dont need to train any tesseract.
By converting the OCR value to a string!
First define the whitelist by using this:
OCRz.SetVariable("tessedit_char_whitelist", ",$0123456789")
Then convert the string like this and print it:
RichTextBox1.Text = Convert.ToString(OCRz.GetText).Replace("$", "").Replace(",", "")
At the end we get this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OCRz.SetVariable("tessedit_char_whitelist", ",$0123456789")
OCRz.Init("tessdata", "eng", False)
OCRz.Recognize(New Image(Of Bgr, Byte)(pic))
RichTextBox1.Text = Convert.ToString(OCRz.GetText).Replace("$", "").Replace(",", "")
I will thank Jimmy Smith again for his fast answers and really useful, mind yourselves to up vote this guy ;)

On fix and download:
Dim OCRz As Tesseract =
New Tesseract("tessdata", "eng",Tesseract.OcrEngineMode.OEM_DEFAULT)
Dim pic As Bitmap = New Bitmap(270, 100)
Dim gfx As Graphics = Graphics.FromImage(pic)

Related

Printing a sharp barcode using Picturebox

I have a question and i hope somebody can help me.
Im using the ZXing.Net plugin to generate a barcode from a number.
All is working well and i get a barcode in a picture box.
Now i would like to print this barcode but the image is all fuzzy.
I've been trying to get it more sharp this is my code now:
Private Sub PrintDocument_BO_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument_BO.PrintPage
Dim myBitmap1 As Bitmap = New Bitmap(PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height)
PrintBackorder.picBarcode.DrawToBitmap(myBitmap1, New Rectangle(0, 0, PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height))
e.Graphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
e.Graphics.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
e.Graphics.DrawImage(myBitmap1, 625, 50)
End sub
Is there something i could add or am i doing something wrong?
Thank you!

VB.net How to convert image file to pdf file and then save it without using SaveFileDialog?

I already manage to convert the image file to pdf file.. but i want to make it automatically save to specific location and without it asking me where to save it. Please help me, any help would be appreciated. Beginner here. Here is the code that im currently trying.
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Text
Imports System.Windows.Forms
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System.IO
Public Class Form1
Private Sub captureScreen()
'Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim graph As Graphics = Nothing
Try
' gets the upper left hand coordinate of the form
Dim frmleft As System.Drawing.Point = Me.Bounds.Location
'use the commented out version for the full screen
'Dim bmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
'this version get the size of the form1 The + 8 adds a little to right and bottom of what is captured.
Dim bmp As New Bitmap(Me.Bounds.Width + 8, Me.Bounds.Height + 8)
'creates the grapgic
graph = Graphics.FromImage(bmp)
'Gets the x,y coordinates from the upper left start point
'used below
Dim screenx As Integer = frmleft.X
Dim screeny As Integer = frmleft.Y
' The - 5 here allows more of the form to be shown for the top and left sides.
graph.CopyFromScreen(screenx - 5, screeny - 5, 0, 0, bmp.Size)
' Save the Screenshot to a file
bmp.Save("I:\eQA\temp.png")
bmp.Dispose()
graph.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
captureScreen()
' Create new pdf document and page
Dim doc As New PdfDocument()
Dim oPage As New PdfPage()
' Add the page to the pdf document and add the captured image to it
doc.Pages.Add(oPage)
'Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage)
Dim img As XImage = XImage.FromFile("I:\eQA\temp.png")
'xgr.DrawImage(img, 0, 0)
'Create XImage object from file.
Using xImg = PdfSharp.Drawing.XImage.FromFile("I:\eQA\temp.png")
'Resize page Width and Height to fit image size.
oPage.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
oPage.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution
'Draw current image file to page.
Dim xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(oPage)
xgr.DrawImage(xImg, 0, 0, oPage.Width, oPage.Height)
End Using
' instantiate a Bitmap object
Dim filesaveas As New SaveFileDialog
filesaveas.Filter = ("PDF File|*.pdf")
Dim btnSave As DialogResult = filesaveas.ShowDialog()
If btnSave.Equals(DialogResult.OK) Then
doc.Save(filesaveas.FileName)
doc.Close()
End If
' I used the Dispose() function to be able to
' save the same form again, in case some values have changed.
' When I didn't use the function, an GDI+ error occurred.
img.Dispose()
End Sub
End Class

Visual Basic drawpolygon method using a do-while loop?

I am looking for help with the drawpolygon method. I haven't had any luck getting anything to work. Looking to do 5 triangles, same size, next to each other. Problem is I have to use a do-while loop. Thanks for taking the time to help me!!
Just define the points in an array an then write them with a pen:
Dim blackPen As New Pen(Color.Black, 3)
Dim point1 As New Point(50, 50)
Dim point2 As New Point(100, 25)
Dim curvePoints As Point() = {point1, point2}
Me.CreateGraphics.DrawPolygon(blackPen, curvePoints)
Take a look at the MSDN Documentation about it.
It doesn't matter if you do anything in a loop, depends how. If this didn't solve your problem post your code in order to help you more.
Example with a loop:
Do While i < 3
point1 As New Point(50 + i * 10, 50)
point2 As New Point(100 + i * 7, 25)
curvePoints = {point1, point2}
Me.CreateGraphics.DrawPolygon(blackPen, curvePoints)
i += 1
Loop
I haven't actually tested this construction but it's all snipped out of a working project; I suspect it'll work and some of this GDI+ stuff is really grungy to pick up the first time.
Public Class Form1
Private subject As Image
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Not subject Is Nothing Then
Dim g As Graphics = e.Graphics
g.DrawImage(subject, New Point(1, 1))
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tempBM As New Bitmap(subject)
tempBM.SetResolution(subject.HorizontalResolution, subject.VerticalResolution)
Using g As Graphics = Graphics.FromImage(tempBM)
g.DrawPolygon(OutlinePen, Polygon.GetPoints)
End Using
subject = tempBM
Invalidate()
End Sub
End Class
Oh, Polygon is a class in my code that you won't have. But just replace Polygon.GetPoints with whatever array of points you want to use.

Static Gmaps in winform

Im trying to load a google static map in a winform. I found a control online that would helpme but i cant get it working. i got the control from Here. i was not able to drag on the control on to the form like others. i have also email the dev of the control but have not heard back yet.
Any help would be Great.
Thank
here is my code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gmap As New CtpGglMap.Impl.StaticGMap
gmap.Center.Address = "New york ,NY"
gmap.Zoom = 14
gmap.Height = 400
gmap.Width = 600
gmap.Sensor = False
PictureBox1.Image = gmap
End Sub
Got it working.
Dim gmap As New CtpGglMap.Impl.StaticGMap
Dim temp As New GMarker
Dim temp2 As New GMarker
gmap.Center = New GeoPointImpl(41.5442847, -73.8732391)
gmap.Zoom = 14
gmap.Height = 334
gmap.Width = 362
gmap.Sensor = False
TextBox1.Text = GetDrivingDirectionFromGoogle("fishkill ,ny", "41.5442847, -73.8732391")
temp.Point = New GeoPointImpl(41.5442847, -73.8732391)
temp.SetMap(gmap)
'CREATE A BITMAP FROM THE MEMORY STREAM
PictureBox1.Image = New System.Drawing.Bitmap(gmap.Fetch)

how do i print my form?

I need to print an image of my VB.Net Windows.Form when the user clicks a button. Is there a good method for doing this?
You need to use the PrintForm component. Please see How to: Print a Form by Using the PrintForm Component:
The PrintForm component enables you to
quickly print an image of a form
exactly as it appears on screen
without using a PrintDocument
component. The following procedures
show how to print a form to a printer,
to a print preview window, and to an
Encapsulated PostScript file.
If you want to amend the image the following code will capture the bitmap and send it to the printer (On the Button1 Click)
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lheight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Copy the form's image into a bitmap.
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
' Make a PrintDocument and print.
mPrintDocument = New PrintDocument
mPrintDocument.Print()
End Sub
End Class
If you really want a snapshot of the form, you'll have to simulate a screen capture. Here's a sample that'll help. It's going to be pretty ugly though, so you may want to consider making it a report using a PDF or report builder.