Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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 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.
// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
// Print the barcode data to the console
Console.WriteLine(myBarcode);
// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
// Print the barcode data to the console
Console.WriteLine(myBarcode);
' Read the barcode from an image file
Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg")
' Print the barcode data to the console
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:
// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
Console.WriteLine(barcodeData.Value);
}
// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
Console.WriteLine(barcodeData.Value);
}
' Read barcodes from a PDF file
Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf")
' Iterate through each barcode found and print its value
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 is 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.
// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
@"D:\Barcode Images\barcode1.jpg",
@"D:\Barcode Images\barcode2.jpg",
@"D:\Barcode Images\barcode3.jpg"
};
// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
string barcodeValue = result.Text;
Console.WriteLine(barcodeValue);
}
// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
@"D:\Barcode Images\barcode1.jpg",
@"D:\Barcode Images\barcode2.jpg",
@"D:\Barcode Images\barcode3.jpg"
};
// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
string barcodeValue = result.Text;
Console.WriteLine(barcodeValue);
}
' Create a list of file paths containing barcode images
Dim barcodeList As New List(Of String) From {"D:\Barcode Images\barcode1.jpg", "D:\Barcode Images\barcode2.jpg", "D:\Barcode Images\barcode3.jpg"}
' Read barcodes asynchronously from multiple files
Dim batchResults = BarcodeReader.ReadAsync(barcodeList)
' Work with the results
For Each result In batchResults
Dim barcodeValue As String = result.Text
Console.WriteLine(barcodeValue)
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:
// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
Console.WriteLine(result.Text);
}
// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
Console.WriteLine(result.Text);
}
' Read the QR code from an image file
Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg")
' Iterate through each result and print its text
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, allowing the same function and code to 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, providing 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.
IronBarcode is a .NET library that allows developers to read and write barcodes and QR codes within .NET applications and websites. It is highly efficient, easy to implement, and free for development.
To create a barcode scanner in C#, you can use the IronBarcode library. Start by creating a Visual Studio project, install the IronBarcode NuGet package, and use the BarcodeReader class to read barcodes from image or PDF files.
IronBarcode supports various barcode types including 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 codes.
You can install the IronBarcode library using the Package Manager Console in Visual Studio. Use the command 'Install-Package Barcode' to install it from the NuGet BarCode package page.
Yes, IronBarcode can read barcodes from PDF files using the ReadPdf function, which scans the entire PDF document for barcode images and returns their data.
You can scan multiple barcodes from different files using the ReadAsync function, which allows asynchronous barcode reading from multiple image files, improving performance through multithreading.
Yes, you can scan QR codes using the same Read function used for barcodes, making it simple to use IronBarcode for various image sources.
IronBarcode offers ease of use, high efficiency, and adaptability. It supports a wide range of barcode types and provides simple APIs for both reading and writing barcodes and QR codes.
The Iron Software Suite includes libraries such as IronPDF for PDF manipulation, IronXL for Excel files, IronOCR for text recognition from images, and IronWebScraper for web data extraction.