Creating a Barcode Scanner in VB.NET Tutorial

Barcodes are a powerful means to represent data in a visible, computer-readable format. In this tutorial, we’ll explore how to generate and read barcodes using IronBarcode in Visual Basic. IronBarcode provides a robust and efficient solution, whether you’re building an inventory management system, a point-of-sale application, or any other project involving VB.NET barcode reader handling.

IronBarcode is a powerful C# library that seamlessly integrates with VB.NET (Visual Basic) projects. It provides robust functionality for reading and writing barcode images, making it an excellent choice for developers working with VB.NET barcode reader component applications. This guide will cover the basics of reading barcodes, configuring options, and handling multiple barcodes in a single scan.

How to Read Barcodes in VB.NET?

  1. Install IronBarcode Library.
  2. Read the Barcode scanner image from a file using the Read() method.
  3. Read the Barcode Image from the PDF using the ReadPdf() method.

How to Generate Barcodes in VB.NET?

  1. Install IronBarcode Library
  2. Create a Barcode using the CreateBarcode() method with specified Barcode Encoding
  3. Save the Barcode as an image, or PDF

Introduction to IronBarcode

IronBarcode is a powerful C# barcode library that simplifies working with barcodes in .NET applications. Whether you need to create barcodes or read existing ones, IronBarcode provides an intuitive and efficient solution.

Here are some key features and use cases of IronBarcode:

Barcode Generation: IronBarcode allows developers to easily generate various types of barcodes, including 1D barcodes like Code 39, Code 128, and UPC, as well as 2D barcodes like QR codes and Data Matrix.

Barcode Reading: The library includes functionality to read barcodes from images, PDFs, or other sources. This can be useful for applications that need to process barcode values from scanned documents or camera captures.

Encoding and Decoding: IronBarcode supports encoding and decoding of barcode value, providing developers with the ability to manipulate barcode scanning information programmatically.

Supported Barcode Types: IronBarcode supports a wide range of barcode symbologies, making it versatile for different application requirements.

Ease of Use: The library is designed to be user-friendly and easy to integrate into .NET applications. It provides comprehensive documentation and examples to assist developers in implementing barcode-related functionality.

Getting Started

The first step is to open or create a new project. The project can be of any type. The same code works for all project types. The next step is to install the IronBarcode library in our project.

Install IronBarcode NuGet Package

You can install the IronBarcode NuGet Package using the Package Manager Console. Enter the following command.

Install-Package Barcode
Install-Package Barcode
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package Barcode
VB   C#

Creating a Barcode Scanner in VB.NET Tutorial: Figure 1 - VB NET Read Barcode Scanner

The above command will download and install IronBarcode Library with all necessary dependencies.

Alternatively, you can also download it from the Manage NuGet package for solution by browsing it.

Creating a Barcode Scanner in VB.NET Tutorial: Figure 2 - IronBarcode

Let's proceed further to generate a barcode image using VB.NET.

Generate Barcode Image

Creating barcodes is straightforward. You can use the BarcodeWriter class to generate various types of barcodes. Once we have our barcode, we can save it as an image. The barcode image can be accessed as an Image or converted to a Bitmap. We’ll create a simple Code128 barcode with the value “0987654ABCD0987654”. Here’s how you can do it:

Sub Main(args As String())
     Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
     ' And save our barcode as an image:
     myBarcode.SaveAsImage("myCode128Barcode.jpeg")
 End Sub
Sub Main(args As String())
     Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
     ' And save our barcode as an image:
     myBarcode.SaveAsImage("myCode128Barcode.jpeg")
 End Sub
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Sub Main(args @As String()) @Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128) ' @And save our TryCast(barcode, an) image: myBarcode.SaveAsImage("myCode128Barcode.jpeg") @End @Sub
VB   C#

The above code snippet utilizes the IronBarcode library to generate a Code 128 barcode with the data "0987654ABCD0987654". The BarcodeWriter.CreateBarcode method is employed, specifying the Code128 encoding. Subsequently, the created barcode is saved as a JPEG image file named "myCode128Barcode.jpeg" using the SaveAsImage method. The ability to change the BarcodeWriterEncoding parameter allows for flexibility in selecting different barcode symbologies according to specific needs, with options such as EAN13, EAN8, Code Bar, MSI, ITF, PDF417, QR codes, or Data Matrix codes offered by the IronBarcode library.

If you wish to save the generated barcode in formats other than JPEG, the IronBarcode library provides various methods for different file formats. You can use methods like SaveAsHtmlFile, SaveAsPDF, SaveAsTiff, SaveAsPng, and others, depending on your requirements. For example, if you want to save the barcode as an HTML file, you can replace the SaveAsImage line with myBarcode.SaveAsHtmlFile("myCode128Barcode.html"). Similarly, for other formats, you can use the corresponding methods, providing the desired file name and extension. This flexibility enables the adaptation of the generated barcode to different file formats to suit specific application needs.

The generated barcode image is as:

Creating a Barcode Scanner in VB.NET Tutorial: Figure 3 - Generate Barcode Output

Resizing and Saving

After creating a barcode, you can resize it easily. The following code will set its max width to 650 Pixels and max height to 300 pixels.

Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
 myBarcode.ResizeTo(650, 300)
 ' And save our barcode as an image:
 myBarcode.SaveAsImage("myCode128Barcode.jpeg")
Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
 myBarcode.ResizeTo(650, 300)
 ' And save our barcode as an image:
 myBarcode.SaveAsImage("myCode128Barcode.jpeg")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128) myBarcode.ResizeTo(650, 300) ' @And save our TryCast(barcode, an) image: myBarcode.SaveAsImage("myCode128Barcode.jpeg")
VB   C#

The ResizeTo method is used to resize a barcode image. In the provided example, myBarcode.ResizeTo(650, 300) is applied to the myBarcode object, suggesting that the barcode image is being resized to a width of 650 pixels and a height of 300 pixels.

This method is beneficial when you need to adjust the dimensions of the generated barcode image to meet specific requirements or to ensure it fits appropriately within a given layout or display area. Resizing can be useful in scenarios where you need to control the visual presentation of the barcode in terms of its size without altering the encoded data.

The output is as:

Creating a Barcode Scanner in VB.NET Tutorial: Figure 4 - Resize Barcode Output

Barcode Reader

IronBarcode makes reading barcodes simple. You can extract barcode values from different sources:

  1. From a file.
  2. From a bitmap object.
  3. From image files.
  4. From a PDF (using ReadPdf).
  5. From memory stream

The following code will scan the barcode image, and print its value in the console.

Sub Main(args As String())
     Dim resultFromImage = BarcodeReader.Read("myCode128Barcode.jpeg") // scan barcodes
     For i As Integer = 0 To resultFromImage.Count - 1
         Console.WriteLine("Barcode Value: {0}", resultFromImage(i))
     Next i
 End Sub
Sub Main(args As String())
     Dim resultFromImage = BarcodeReader.Read("myCode128Barcode.jpeg") // scan barcodes
     For i As Integer = 0 To resultFromImage.Count - 1
         Console.WriteLine("Barcode Value: {0}", resultFromImage(i))
     Next i
 End Sub
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Sub Main(args @As String()) @Dim resultFromImage = BarcodeReader.Read("myCode128Barcode.jpeg") @For i @As @Integer = 0 @To resultFromImage.Count - 1 Console.WriteLine("Barcode Value: {0}", resultFromImage(i)) @Next i @End @Sub
VB   C#

The above code utilizes the IronBarcode library to read barcode data from an image file, "myCode128Barcode.jpeg." The BarcodeReader.Read method extracts the barcode information, and a loop is used to iterate through the results. The barcode values are then printed to the console using Console.WriteLine.

Additionally, you can also scan barcode data from various sources such as Bitmaps, AnyBitmaps, image files, and streams. These options provide flexibility in handling barcode data from different input formats, ranging from conventional image files like BMP and JPG to more generic representations like AnyBitmaps and streams. In this, we can develop our very own .NET Barcode reader, which reads bar codes and returns the result.

The output is as:

Output

Creating a Barcode Scanner in VB.NET Tutorial: Figure 5 - Barcode Reader Output

Add advanced option to Barcode Scanners

We can add more advanced features to our barcode scanners such as customize barcode scanning using BarcodeReaderOptions. Set parameters such as:

  1. Reading speed (Faster, Balanced, Detailed, ExtremeDetail).
  2. Whether to expect more than 1 barcode.
  3. Specific barcode types to scan.
  4. Multithreading for parallel processing.
  5. Crop area to focus on relevant parts of the image.

We will read the following PDF file having three different barcode images.

Creating a Barcode Scanner in VB.NET Tutorial: Figure 6 - Barcodes Input

The following code will add advanced features to our VB.NET barcode reader.

Sub Main(args As String())
        Dim resultFromPdf = BarcodeReader.ReadPdf("Barcode.pdf") ' From a PDF
        Dim myOptionsExample As New BarcodeReaderOptions() With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True,
    .ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
    .Multithreaded = True,
    .MaxParallelThreads = 2,
    .CropArea = New System.Drawing.Rectangle(),
    .UseCode39ExtendedMode = True
    }
        For i As Integer = 0 To resultFromPdf.Count - 1
            Console.WriteLine("Barcode Value - {0} = {1}", i, resultFromPdf(i))
        Next i
    End Sub
Sub Main(args As String())
        Dim resultFromPdf = BarcodeReader.ReadPdf("Barcode.pdf") ' From a PDF
        Dim myOptionsExample As New BarcodeReaderOptions() With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True,
    .ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
    .Multithreaded = True,
    .MaxParallelThreads = 2,
    .CropArea = New System.Drawing.Rectangle(),
    .UseCode39ExtendedMode = True
    }
        For i As Integer = 0 To resultFromPdf.Count - 1
            Console.WriteLine("Barcode Value - {0} = {1}", i, resultFromPdf(i))
        Next i
    End Sub
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Sub Main(args @As String()) @Dim resultFromPdf = BarcodeReader.ReadPdf("Barcode.pdf") ' From a PDF @Dim myOptionsExample @As @New BarcodeReaderOptions() @With { .Speed = ReadingSpeed.Balanced, .ExpectMultipleBarcodes = @True, .ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional, .Multithreaded = @True, .MaxParallelThreads = 2, .CropArea = @New System.Drawing.Rectangle(), .UseCode39ExtendedMode = @True } @For i @As @Integer = 0 @To resultFromPdf.Count - 1 Console.WriteLine("Barcode Value - {0} = {1}", i, resultFromPdf(i)) @Next i @End @Sub
VB   C#

The above code scans barcodes from a PDF file, "Barcode.pdf." The BarcodeReader.ReadPdf method extracts barcode information from the PDF, and a loop is utilized to iterate through the results. Additionally, the code introduces a BarcodeReaderOptions object, myOptionsExample, configured with various settings such as reading speed, the expectation of multiple barcodes, accepting all one-dimensional barcode types, enabling multithreading with a maximum of two parallel threads, specifying a crop area, and using Code 39 extended mode. These options demonstrate the flexibility of the IronBarcode library in customizing the barcode reading process based on specific requirements and environmental considerations. The barcode values are then printed to the console, providing insights into the decoded data from the PDF file.

The output is as:

Creating a Barcode Scanner in VB.NET Tutorial: Figure 7 - Barcode Values Output

In this way, we can create our own .NET barcode scanner dll.

Conclusion

In conclusion, this tutorial demonstrated how to create a barcode scanner and generator in VB.NET using the IronBarcode library. IronBarcode offers a robust solution for handling barcode-related tasks, whether generating options for purchase, making it a flexible and accessible tool for incorporating barcode capabilities into VB.NET projects.