Reading Barcodes Tutorial

Take the following steps to start a project:

  1. Start Visual Studio.

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

  3. 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.

  4. Name the project "ReadingBarcodes". If desired, provide a new location for your project. Then, click OK.

  5. 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 20\Bin\DotNet4\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Forms.Common.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 click the OK button to add the above DLLs to the application.

  6. 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
  7. 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:

    VB
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.Forms.Common 
    Imports Leadtools.Barcode 
    C#
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Forms.Common; 
    using Leadtools.Barcode; 

  8. Add the following private variables to the Form1 class

    VB
    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 the Form1 class

    VB
    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 20\Common\License\leadtools.lic" 
       Dim MY_DEVELOPER_KEY As String = System.IO.File.ReadAllText("C:\LEADTOOLS 20\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 
    C#
    protected override void OnLoad(EventArgs e) 
    {                
       // Requires a license file that unlocks 1D barcode read functionality. 
       string MY_LICENSE_FILE = @"C:\LEADTOOLS 20\Common\License\leadtools.lic"; 
       string MY_DEVELOPER_KEY = System.IO.File.ReadAllText(@"C:\LEADTOOLS 20\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); 
    } 

  10. Add the following clean up code to the Form1 class

    VB
    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 
    C#
    protected override void OnFormClosed(FormClosedEventArgs e) 
    { 
       // Dispose of the image 
       if(theImage != null) 
       { 
          theImage.Dispose(); 
       } 
                     
       base.OnFormClosed(e); 
    } 

  11. Add the following event to the Form1 class:

    VB
    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 
    C#
    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"); 
    } 

  12. Add the following event to the Form1 class:

    VB
    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, LeadRect.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) 
       { 
          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, LeadRect.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 the Load Image button to load the image. Click the Read Barcodes button to search for, read, and indicate the 1D barcodes.

Help Version 20.0.2018.9.5
Products | Support | Contact Us | Copyright Notices
© 1991-2018 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document