跳至页脚内容
USING IRONBARCODE

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 values, 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

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

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

Alternatively, you can also download it from the Manage NuGet package for the 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)
    ' Save the barcode as an image
    myBarcode.SaveAsImage("myCode128Barcode.jpeg")
End Sub
Sub Main(args As String())
    Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
    ' Save the barcode as an image
    myBarcode.SaveAsImage("myCode128Barcode.jpeg")
End Sub
VB .NET

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)
' Save the resized barcode as an image
myBarcode.SaveAsImage("myCode128Barcode.jpeg")
Dim myBarcode = BarcodeWriter.CreateBarcode("0987654ABCD0987654", BarcodeWriterEncoding.Code128)
myBarcode.ResizeTo(650, 300)
' Save the resized barcode as an image
myBarcode.SaveAsImage("myCode128Barcode.jpeg")
VB .NET

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 a 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
VB .NET

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. With this, we can develop our very own .NET Barcode reader, which reads barcodes and returns the result.

The output is displayed as:

Output

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

Add Advanced Options to Barcode Scanners

We can add more advanced features to our barcode scanners, such as customizing 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") ' Scan barcodes 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") ' Scan barcodes 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 .NET

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

常见问题解答

我该如何在 VB.NET 中从图像中读取条码?

要在 VB.NET 中从图像中读取条码,可以使用 IronBarcode 库。首先,安装 IronBarcode,然后使用 Read() 方法从图像文件中提取条码数据。

在 VB.NET 中有哪些方法可以从 PDF 中读取条码?

可以通过使用 IronBarcode 的 ReadPdf() 方法在 VB.NET 中从 PDF 文件中读取条码。这允许直接从 PDF 文件中提取条码信息。

使用 IronBarcode 在 VB.NET 中生成条码有哪些优势?

IronBarcode 提供了一个简单的 API 来在 VB.NET 中生成条码。可以使用 CreateBarcode() 方法生成各种格式的条码,如 JPEG、PDF 和 HTML,并支持多种条码类型。

我如何在 VB.NET 中处理单次扫描中多个条码?

要在 VB.NET 中处理单次扫描中的多个条码,可以在 IronBarcode 中配置 BarcodeReaderOptions,启用 ExpectMultipleBarcodes 选项,以便在一次扫描中检测多个条码。

在 VB.NET 中条码扫描有哪些高级配置选项?

IronBarcode 通过 BarcodeReaderOptions 提供高级配置选项,允许您调整读取速度,指定预期的条码类型,启用多线程,并定义特定的裁剪区域以进行目标扫描。

我如何在 VB.NET 中调整条码图像的大小?

在 VB.NET 中,可以使用 IronBarcode 的 ResizeTo 方法调整条码图像的大小,指定条码图像的最大宽度和高度。

如何在 VB.NET 中以不同格式保存条码?

在 VB.NET 中使用 IronBarcode,可以以 JPEG、PDF、HTML、TIFF 和 PNG 等多种格式保存条码。这种灵活性允许轻松集成到不同的系统和应用程序中。

构建 VB.NET 条码扫描器应用程序需要什么?

要构建 VB.NET 条码扫描器应用程序,首先安装 IronBarcode 库。然后,利用 BarcodeReader 方法扫描并提取来自图像或 PDF 的数据,并使用 BarcodeReaderOptions 的选项自定义扫描过程。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。