Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
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 " in the Templates List.
  4. Type the project name as "GetFastTwainScan" 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 Leadtools For .NET "LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32" folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Barcode.dll
    • Leadtools.Codecs.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.Barcode
    
    [C#]
     
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.Barcode;
    
  7. Drag and drop two buttons in Form1. Change the following properties: Property Value: Name: btnLoadImage Text: Load Image Name: btnReadBarCode Text: Read Bar Code
  8. Add an event handler to the Form1 Load event and add the following code

    Visual Basic

    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ' Note that this is a sample key, which will not work in your toolkit
       RasterSupport.Unlock(Leadtools.RasterSupportType.Barcodes1D, "TestKey")
       BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d)
       RasterCodecs.Startup()
       _barEngine = New BarcodeEngine()
       _codecs = New RasterCodecs()
    End Sub
    

    C#

    
    private void Form1_Load(object sender, EventArgs e)
    {
       // Note that this is a sample key, which will not work in your toolkit
       RasterSupport.Unlock(Leadtools.RasterSupportType.Barcodes1D, "TestKey");
       BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d);
       RasterCodecs.Startup();
       _barEngine = new BarcodeEngine();
       _codecs = new RasterCodecs();
    }
    
  9. Add an event handler to the Form1 FormClosing event and add the following code

    Visual Basic

    
    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
       BarcodeEngine.Shutdown()
    End Sub
    

    C#

    
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       BarcodeEngine.Shutdown();
    }
    
  10. Add the following private variables to Form1 class:

    Visual Basic

    
    Dim _image As RasterImage
    Dim _barEngine As BarcodeEngine
    Dim _codecs As RasterCodecs
    Dim _readBarData As RasterCollection(Of BarcodeData)
    

    C#

    
    RasterImage _image;
    BarcodeEngine _barEngine;
    RasterCodecs _codecs;
    RasterCollection<BarcodeData> _readBarData;
    
  11. Add the following code for the btnLoadImage control’s click procedure:

    Visual Basic

    
    Private Sub btnLoadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadImage.Click
       _image = _codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\barcode1.tif")
    End Sub
    

    C#

    
    private void btnLoadImage_Click(object sender, EventArgs e)
    {
       _image = _codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\barcode1.tif");
    }
    
  12. Add the following code for the btnReadBarCode control’s click procedure:

    Visual Basic

    
    Private Sub btnReadBarCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadBarCode.Click
       Dim searchRect As Rectangle = New Rectangle(0, 0, 0, 0)
       Dim bar1d As Barcode1d = New Barcode1d()
       bar1d.ErrorCheck = True
       bar1d.Granularity = 9
       bar1d.Direction = BarcodeDirectionFlags.LeftToRight
    
       Dim barRPdf As BarcodeReadPdf = New BarcodeReadPdf()
       barRPdf.Direction = BarcodeDirectionFlags.LeftToRight
    
       Dim barColor As BarcodeColor = New BarcodeColor()
       barColor.BarColor = Color.Black
       barColor.SpaceColor = Color.White
    
       Try
          ' The Read method will search for all linear barcodes in the whole bitmap
          _readBarData = _barEngine.Read(_image, _
                                         searchRect, _
                                         BarcodeSearchTypeFlags.Barcode1dReadAnyType, _
                                         BarcodeUnit.ScanlinesPerPixels, _
                                         BarcodeReadFlags.None, _
                                         0, _
                                         bar1d, _
                                         barRPdf, _
                                         barColor)
    
          Dim count As Integer = _readBarData.Count
          Dim i As Integer
          For i = 0 To count - 1
             Dim data As BarcodeData = _readBarData(i)
             Dim barDataMsg As String = String.Format("Barcode Data = {0}\nType = {1}\nLeft = {2}\nTop = {3}\nRight = {4}\nBottom = {5}\n", _
                                               BarcodeData.ConvertToStringArray(data.Data), _
                                               data.SearchType, _
                                               data.Location.Left, _
                                               data.Location.Top, _
                                               data.Location.Right, _
                                               data.Location.Bottom)
             MessageBox.Show(Me, barDataMsg)
          Next
       Catch ex As Exception
          MessageBox.Show(Me, ex.Message)
       End Try
    End Sub
    

    C#

    
    private void btnReadBarCode_Click(object sender, EventArgs e)
    {
       Rectangle searchRect = new Rectangle(0, 0, 0, 0);
       Barcode1d bar1d = new Barcode1d();
       bar1d.ErrorCheck = true;
       bar1d.Granularity = 9;
       bar1d.Direction = BarcodeDirectionFlags.LeftToRight;
    
       BarcodeReadPdf barRPdf = new BarcodeReadPdf();
       barRPdf.Direction = BarcodeDirectionFlags.LeftToRight;
    
       BarcodeColor barColor = new BarcodeColor();
       barColor.BarColor = Color.Black;
       barColor.SpaceColor = Color.White;
    
       try
       {
          // The Read method will search for all linear barcodes in the whole bitmap
          _readBarData = _barEngine.Read(_image,
                                         searchRect,
                                         BarcodeSearchTypeFlags.Barcode1dReadAnyType,
                                         BarcodeUnit.ScanlinesPerPixels,
                                         BarcodeReadFlags.None,
                                         0,
                                         bar1d,
                                         barRPdf,
                                         barColor);
    
          int count = _readBarData.Count;
          for (int i=0; i<count; i++)
          {
             BarcodeData data = _readBarData[i];
             string barDataMsg = string.Format("Barcode Data = {0}\nType = {1}\nLeft = {2}\nTop = {3}\nRight = {4}\nBottom = {5}\n",
                                               BarcodeData.ConvertToStringArray(data.Data),
                                               data.SearchType,
                                               data.Location.Left,
                                               data.Location.Top,
                                               data.Location.Right,
                                               data.Location.Bottom);
             MessageBox.Show(this, barDataMsg);
          }
       }
       catch (Exception ex)
       {
          MessageBox.Show(this, ex.Message);
       }
    }