Take the following steps to start a project:
Start Visual Studio.
Choose File -> New -> Project... from the menu.
In the New Project dialog box, choose either Visual C# Projects or VB Projects in the Projects Type list. Then, choose Windows Forms Application in the templates list.
Name the project "ReadingBarcodes". If desired, provide a new location for your project. Then, click OK.
In the Solution Explorer panel, right-click on the References node and select Add Reference... from the context menu. In the Add Reference dialog box, browse to the "C:\LEADTOOLS 19\Bin\DotNet4\Win32" folder and select the following DLLs:
Click the Select button and then click the OK button to add the above DLLs to the application.
Drag and drop two buttons from the Toolbox onto the Form1
UI designer. Change the following properties:
Property | Value |
---|---|
Name | loadImageButton |
Text | Load image |
Name | readBarcodesButton |
Text | Read barcodes |
Switch to Form1 code view (right-click Form1 in the Solution Explorer and select View Code) and add the following lines at the beginning of the file:
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms
Imports Leadtools.Barcode
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Barcode;
Add the following private variables to the Form1
class
Private barcodeEngineInstance As BarcodeEngine ' The barcode engine
Private theImage As RasterImage ' The current loaded image
Private imageFileName As String ' Last file name we loaded; this is used in the "Writing Barcodes" tutorial
private BarcodeEngine barcodeEngineInstance; // The barcode engine
private RasterImage theImage; // The current loaded image
private string imageFileName; // Last file name we loaded; this is used in the "Writing Barcodes" tutorial
Add the following initialization code to the Form1
class
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
' Requires a license file that unlocks 1D barcode read functionality.
Dim MY_LICENSE_FILE As String = "C:\LEADTOOLS 19\Common\License\leadtools.lic"
Dim MY_DEVELOPER_KEY As String = System.IO.File.ReadAllText("C:\LEADTOOLS 19\Common\License\leadtools.lic.key")
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY)
' Create the BarcodeEngine instance
barcodeEngineInstance = New BarcodeEngine()
MyBase.OnLoad(e)
End Sub
protected override void OnLoad(EventArgs e)
{
// Requires a license file that unlocks 1D barcode read functionality.
string MY_LICENSE_FILE = @"C:\LEADTOOLS 19\Common\License\leadtools.lic";
string MY_DEVELOPER_KEY = System.IO.File.ReadAllText(@"C:\LEADTOOLS 19\Common\License\leadtools.lic.key");
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY);
// Create the BarcodeEngine instance
barcodeEngineInstance = new BarcodeEngine();
readBarcodesButton.Click += readBarcodesButton_Click;
loadImageButton.Click += loadImageButton_Click;
base.OnLoad(e);
}
Add the following clean up code to the Form1
class
Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
' Dispose of the image
If Not theImage Is Nothing Then
theImage.Dispose()
End If
MyBase.OnFormClosed(e)
End Sub
protected override void OnFormClosed(FormClosedEventArgs e)
{
// Dispose of the image
if(theImage != null)
{
theImage.Dispose();
}
base.OnFormClosed(e);
}
Add the following event to the Form1
class:
Private Sub loadImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadImageButton.Click
Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Barcode1.tif"
' Or uncomment the following to load your own file
' Using dlg As New OpenFileDialog
' If dlg.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
' fileName = dlg.FileName
' Else
' Return
' End If
' End Using
' Load the image
Using codecs As New RasterCodecs
Dim newImage As RasterImage = codecs.Load(fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
' Dispose of the image
If Not theImage Is Nothing Then
theImage.Dispose()
End If
theImage = newImage
imageFileName = fileName
End Using
MessageBox.Show("Image loaded")
End Sub
private void loadImageButton_Click(object sender, EventArgs e)
{
string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Barcode1.tif";
// Or uncomment the following to load your own file
//using(OpenFileDialog dlg = new OpenFileDialog())
//{
// if(dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
// {
// fileName = dlg.FileName;
// }
// else
// {
// return;
// }
//}
// Load the image
using(RasterCodecs codecs = new RasterCodecs())
{
RasterImage newImage = codecs.Load(fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);
// Dispose of the image
if(theImage != null)
{
theImage.Dispose();
}
theImage = newImage;
imageFileName = fileName;
}
MessageBox.Show("Image loaded");
}
Add the following event to the Form1
class:
Private Sub readBarcodesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readBarcodesButton.Click
If theImage Is Nothing
loadImageButton_Click(sender, e)
End If
Try
' Read all the barcodes
' The first parameter is the image from which to read the barcodes.
' The second parameter is the search rectangle. Pass an empty rectangle to search the entire image.
' The third parameter is the maximum number of barcodes to read. Pass 0 for all found in the image.
' The last parameter is an array of the BarcodeSymbology that we are interested in finding. Pass null (or Nothing)
' to find all available barcodes found in the image and supported by the current license.
Dim dataArray() As BarcodeData = barcodeEngineInstance.Reader.ReadBarcodes(theImage, LogicalRectangle.Empty, 0, Nothing)
Dim sb As New StringBuilder()
sb.AppendFormat("{0} barcode(s) found", dataArray.Length)
sb.AppendLine()
For i As Integer = 0 To dataArray.Length - 1
Dim data As BarcodeData = dataArray(i)
sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value)
sb.AppendLine()
Next
MessageBox.Show(sb.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
private void readBarcodesButton_Click(object sender, EventArgs e)
{
if(theImage == null)
{
loadImageButton_Click(sender, e);
}
try
{
// Read all the barcodes
// The first parameter is the image from which to read the barcodes.
// The second parameter is the search rectangle. Pass an empty rectangle to search the entire image.
// The third parameter is the maximum number of barcodes to read. Pass 0 for all found in the image.
// The last parameter is an array of the BarcodeSymbology that we are interested in finding. Pass null (or Nothing)
// to find all available barcodes found in the image and supported by the current license.
BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(theImage, LogicalRectangle.Empty, 0, null);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} barcode(s) found", dataArray.Length);
sb.AppendLine();
for(int i = 0; i < dataArray.Length; i++)
{
BarcodeData data = dataArray[i];
sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value);
sb.AppendLine();
}
MessageBox.Show(sb.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Build and run the program to test it. Click the Load Image button to load the image. Click the Read Barcodes button to search for, read, and indicate the 1D barcodes.
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET