LEAD Technologies, Inc

Reading Barcodes

Take the following steps to start a project:

  1. Start Visual Studio .NET.

  2. Choose File->New->Project... from the menu.

  3. In the New Project dialog box, Choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" (or "Windows Forms Application" in Visual Studio 2010) in the Templates List.

  4. Type the project name as "ReadingBarcodes" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.

  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the Add Reference dialog box, select the ".NET" tab and browse to the "C:\LEADTOOLS 17.5\Bin\DotNet\Win32" folder (or "C:\LEADTOOLS 17.5\Bin\DotNet4\Win32" if you are going to use .NET 4) and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Forms.dll
    • Leadtools.Barcode.dll
    • Leadtools.Barcode.OneD.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Codecs.Fax.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  6. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    [Visual Basic]

            
    
                 Imports Leadtools
                 Imports Leadtools.Codecs
                 Imports Leadtools.Forms
                 Imports Leadtools.Barcode
                 
    
    
          
    

    [C#]

            
    
                 using Leadtools;
                 using Leadtools.Codecs;
                 using Leadtools.Forms;
                 using Leadtools.Barcode;
                 
    
    
          
    

  7. Drag and drop two buttons in Form1. Change the following properties:

    Property Value
    Name loadImageButton
    Text Load image
    Name readBarcodesButton
    Text Read barcodes
  8. Add the following private variables to Form1

    [Visual Basic]

            
    
                 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
                 
    
    
          
    

    [C#]

            
    
                 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
                 
    
    
          
    

  9. Add the following initialization code to Form1

    [Visual Basic]

            
    
                 Protected Overrides Sub OnLoad(ByVal e As EventArgs)
                    ' Use the developer and license file by LEADTOOLS
                    ' RasterSupport.SetLicense(PathToLEADTOOLSLicenseFile, "MyLicenseKey")
                
                    ' Create the BarcodeEngine instance
                    barcodeEngineInstance = New BarcodeEngine()
                
                    MyBase.OnLoad(e)
                 End Sub
                 
    
    
          
    

    [C#]

            
    
                 protected override void OnLoad(EventArgs e)
                 {
                    // Replace the keys with unlock support string provided by LEADTOOLS, or use the evaluation kernel
                
                    // Unlock reading 1D barcodes
                    // RasterSupport.SetLicense(PathToLEADTOOLSLicenseFile, "MyLicenseKey");
                
                    // Create the BarcodeEngine instance
                    barcodeEngineInstance = new BarcodeEngine();
                
                    base.OnLoad(e);
                 }
                 
    
    
          
    

  10. Add the following clean up code to Form1

    [Visual Basic]

            
    
                 Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
                    ' Delete our resources
                
                    If Not theImage Is Nothing Then
                       theImage.Dispose()
                    End If
                
                    MyBase.OnFormClosed(e)
                 End Sub
                 
    
    
          
    

    [C#]

            
    
                 protected override void OnFormClosed(FormClosedEventArgs e)
                 {
                    // Delete our resources
                
                    if(theImage != null)
                    {
                       theImage.Dispose();
                    }
                
                    base.OnFormClosed(e);
                 }
                 
    
    
          
    

  11. Add the following code to loadImageButton click procedure:

    [Visual Basic]

            
    
                 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 comment out 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)
                
                       ' Delete old image if any
                       If Not theImage Is Nothing Then
                          theImage.Dispose()
                       End If
                
                       theImage = newImage
                       imageFileName = fileName
                    End Using
                 End Sub
                 
    
    
          
    

    [C#]

            
    
                 private void loadImageButton_Click(object sender, EventArgs e)
                 {
                    string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Barcode1.tif";
                    // Or comment out 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())
                    {
                       string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Barcode1.tif"
                       RasterImage newImage = codecs.Load(fileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);
                
                       // Delete old image if any
                       if(theImage != null)
                       {
                          theImage.Dispose();
                       }
                
                       theImage = newImage;
                       imageFileName = fileName;
                    }
                 }
                 
    
    
          
    

  12. Add the following code to readBarcodesButton click procedure:

    [Visual Basic]

            
    
                 Private Sub readBarcodesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readBarcodesButton.Click
                    If theImage Is Nothing
                       MessageBox.Show("Load an image first")
                       Return
                    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 indicate 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 we are interested in. Pass null (or Nothing)
                       ' to indicate that we do not care, and want all available barcodes to be read (found in the image and supported by
                       ' the current unlock support mechanism)
                       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
                 
    
    
          
    

    [C#]

            
    
                 private void readBarcodesButton_Click(object sender, EventArgs e)
                 {
                    if(theImage == null)
                    {
                       MessageBox.Show("Load an image first");
                       return;
                    }
                
                    try
                    {
                       // Read all the barcodes
                       // The first parameter is the image we want to read the barcodes from
                       // The second parameter is the search rectangle. Pass an empty rectangle to indicate the entire image.
                       // The third parameter is the maximum number of barcodes to read. Pass 0 for all barcodes found in the image.
                       // Last parameter is an array of the BarcodeSymbology we are interested in. Pass null (or Nothing)
                       // to indicate that we do not care, and want all barcodes available to be read(that have been found in the image and are supported by
                       // the current unlock support mechanism)
                       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);
                    }
                 }
                 
    
    
          
    

  13. Build, and Run the program to test it. Click "Load Image" to load the image and "Read Barcodes" to search for and show the 1D barcodes that have been read.

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.