Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will demonstrate how to create a barcode scanner with assistance from a third-party library. Multiple libraries are available for this purpose, but some are paid, some provide less functionality, and some are difficult to implement. It is a challenging task to find a very comprehensive and useful library that is free, efficient, and easy to implement.
For this reason, we will be using IronBarcode, as it is the best fit for developing a .NET barcode scanner. It also has the added benefit of being free for development, highly efficient, and easy to implement. IronBarcode allows developers to read and write barcodes and QR codes within .NET applications and websites. Reading or writing barcodes only requires a single line of code with this library.
The .NET barcode library reads and writes most barcode and QR code standards. The supported barcode types include code 39/93/128, UPC A/E, EAN 8/13, ITF, RSS 14 / Expanded, Databar, Codabar, Aztec, Data Matrix, MaxiCode, PDF417, MSI, Plessey, USPS, and QR. The barcode result data includes type, text, binary data, page, and image files.
The barcode writing API checks and verifies format, length, number, and checksum to automatically avoid encoding errors. The barcode writer allows for styling, resizing, margins, borders, recoloring, and adding text annotations. The barcode writer can write to image files including BMP, PNG, GIF, TIFF, and JPG. It can also write to PDF or HTML files.
Let's create our barcode scanner to understand it better.
First of all, create a Visual Studio project for developing a demonstration app. You can also open an existing one.
Follow the steps below for creating a new Console Application project to focus on core functionalities. The same code can be implemented in web API, MVC, Web Forms, or Windows Forms Applications.
A new Project will be created as shown below:
Console Application
The next step is to install the IronBarcode NuGet package to use its functionalities.
You can install the library using the Package Manager Console, NuGet Package Manager Solutions, or directly install it from the NuGet BarCode package page.
Follow the steps below:
Click on Tools > NuGet Package Manager > Package Manager Console.
Package Manager Console UI
Install-Package BarCode
The package will be installed.
Now let's write a code for scanning a barcode image.
Add the following namespace
using IronBarCode;
using IronBarCode;
Imports IronBarCode
Next write the following code inside the main function to read barcode data from an image.
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
Console.WriteLine(myBarcode);
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
Console.WriteLine(myBarcode);
Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg")
Console.WriteLine(myBarcode)
The BarcodeReader
class provides a Read
function that takes a file path as an argument. This function reads the image and returns barcode data. This method reads barcodes from a BMP, PNG, GIF, TIFF, or JPG with granular settings for developer control to balance performance against accuracy for their given use case.
The path contains the following barcode image which the application will scan.
The barcode image used in this Console Application
Let's read this barcode image to see if this program produces the correct result.
The Console output on running application
It can be seen that the program has generated an accurate output.
There are many circumstances where there is a need to scan a barcode from a PDF invoice. In this example, we will scan the barcode from the following invoice.
An invoice in PDF format
Consider the following code snippet to scan the barcode from a PDF document:
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
foreach(var barcodeData in myBarcode)
{
Console.WriteLine(barcodeData.Value);
}
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
foreach(var barcodeData in myBarcode)
{
Console.WriteLine(barcodeData.Value);
}
Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf")
For Each barcodeData In myBarcode
Console.WriteLine(barcodeData.Value)
Next barcodeData
The BarcodeReader
class provides the ReadPdf
function which takes a file path as an argument. This function looks for a barcode image in a PDF file, scans the whole barcode, and returns its data in the form of an array. This function reads barcodes from every image embedded in the document.
a foreach
loop used to print the data of the barcode in the console.
The invoice number is printed on the console.
The Console output displays the invoice number
This example will demonstrate how to scan multiple barcodes from image files simultaneously.
Barcode images used in the sample below
Consider the following code snippets which read multiple barcodes and scan their result using multithreading.
List<string> barcodeList = new List<string>();
barcodeList.Add(@"D:\Barcode Images\barcode1.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode2.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode3.jpg");
var batchResults = BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
string barcodeValue = result.Text;
}
List<string> barcodeList = new List<string>();
barcodeList.Add(@"D:\Barcode Images\barcode1.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode2.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode3.jpg");
var batchResults = BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
string barcodeValue = result.Text;
}
Dim barcodeList As New List(Of String)()
barcodeList.Add("D:\Barcode Images\barcode1.jpg")
barcodeList.Add("D:\Barcode Images\barcode2.jpg")
barcodeList.Add("D:\Barcode Images\barcode3.jpg")
Dim batchResults = BarcodeReader.ReadAsync(barcodeList)
' Work with the results
For Each result In batchResults
Dim barcodeValue As String = result.Text
Next result
Firstly, a list is created to hold the file path of all the barcode images. Next, the ReadAsync
function is called, which takes a List<string>
type as an argument and returns the data. This method reads barcodes from multiple images in parallel. Multiple threads will start and be automatically managed to improve performance for batch barcode reading tasks.
The usage of QR codes is rapidly increasing. Therefore, this section will show how to scan QR codes using C#.
The QR code used in this demonstration
Consider the following code sample:
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
foreach (var result in qrCodeResult)
{
Console.WriteLine(result.Text);
}
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
foreach (var result in qrCodeResult)
{
Console.WriteLine(result.Text);
}
Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg")
For Each result In qrCodeResult
Console.WriteLine(result.Text)
Next result
The same Read
function as discussed above is used to read the QR Code. This is the simplicity provided by the IronBarcode library. Such that the same function and code can be used for different image sources.
The Console out of QR code reader
This tutorial demonstrated a very easy way to scan barcodes from a single image, scan barcodes from a PDF document, and scan barcodes from multiple images and multiple documents in parallel. We have used the same function for different functionalities. It provides performance and usability both at the same time. IronBarcode provides the functionality of generating barcodes and QR codes with different configurations. There are so many features that cannot be discussed in the same article. Please click on the documentation page for further exploration of IronBarcode.
IronBarcode is part of the Iron Software Suite. This suite contains other very useful libraries, such as IronPDF for reading and writing PDF files, IronXL for manipulating Excel files, IronOCR for reading text from images, and IronWebScraper for extracting data from different websites. You can purchase the complete Iron Suite for the price of two individual libraries.
9 .NET API products for your office documents