C# Barcode Scanner Step-by-Step Tutorial

In the modern age, we see barcode scanners in every type of software, be it point of sale, hospital management systems, restaurant apps, or delivery apps. So, in view of the rapid increase of barcode usage, it is necessary that every C# programmer must know how to scan barcodes to retrieve their data.

In this tutorial, we will create our own barcode scanner. For developing our barcode scanner, we will need the 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.

Create a Visual Studio Project

First of all, we need to 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. We will be using a console application template to make it more generalized and focused on core functionalities. The same code can be implemented in web API, MVC, web forms, or windows form applications.

  1. Open Visual Studio
  2. Click on Create New Project
  3. Select Template, click on the next button
  4. Name the Project, click on the next button
  5. Select Target Framework, click on the next button
  6. Click on Create Button, click on the next button

A new Project will be created as shown below:

Console Application

Console Application

Now we need to install the IronBarcode NuGet package to use its functionalities in our application.

Install NuGet package of IronBarcode

We will install the library using the Package Manager console. You can also install it using NuGet Package Manager Solutions, or directly install it from here.

Follow the steps below:

  1. Click on Tools > NuGet Package Manager > Package Manager Console.

  2. Write the following command
install-package barcode

The package will be installed.

Now let's write a code for scanning a barcode image.

Scanning a barcode from an Image file

Add the following namespace:

using IronBarCode;
using IronBarCode;
Imports IronBarCode
VB   C#

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

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 our application will scan. This barcode encodes "Net barcode scanner"

Barcode Image

Let's read this barcode image to see if our program produces the correct result.

Output

It can be seen that our program has generated an accurate output.

Scanning a barcode from a PDF

There are many circumstances where we need to scan a barcode from a PDF invoice. In this example, we will scan the barcode from the following invoice.

PDF document

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
VB   C#

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.

We have used a foreach loop to print the data of the barcode in the console.

Output

The invoice number is printed on the console.

Scanning barcodes from multiple files

In this example, we will scan multiple barcodes. Following are the image files that we will read simultaneously.

Barcode images

Consider the following code snippets which read multiple barcodes and scan their result using multi-threading.

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
VB   C#

Firstly, we have created a list that holds the file path of all the barcode images we want to scan. Next, the ReadAsync() function is called, which takes a List 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.

Scan QR Code

The usage of QR codes is rapidly increasing. Therefore, we will learn how to scan QR codes using C#.

We will scan the following QR code from our application for this demonstration.

QR code

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
VB   C#

The same read function as discussed above is used to read the QR Code. This is the simplicity provided by the Iron Barcode library. Such that we can perform every function using just the same line of code.

Output

Summary

In this tutorial, we have learned 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 here 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 IronSuite for the price of two individual libraries.

We hope you have enjoyed the article. Feel free to ask any query in the comment box.