How to Read Barcodes and QR Codes

by Chaknith Bin

Reading barcodes and QR codes with OCR technology can be useful in scenarios where these codes are part of printed or digital documents and need to be automatically processed. It allows for automation and data extraction from a wide range of sources, making it a versatile solution for businesses and developers.

IronOcr makes barcode and QR code detection automatic with only one additional setting needed to enable the detection.


C# NuGet Library for OCR

Install with NuGet

Install-Package IronOcr
or
C# OCR DLL

Download DLL

Download DLL

Manually install into your project

Read Barcode Example

Construct the IronTesseract object to perform the reading. Enable barcode reading by setting the ReadBarCodes property to true. Import the PDF document by passing it into the OcrPdfInput constructor. Then, use the Read method to perform OCR on the imported PDF document.

Now, let's perform OCR on the following PDF document:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-barcodes.cs
using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithBarcodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True

' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithBarcodes.pdf")

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
	Console.WriteLine(barcode.Value)
Next barcode
VB   C#
Reading result

As you can see, multiple barcode values that are also included in the extracted text are the barcodes value below the barcodes.

Read QR Code Example

Similar to reading barcode the ReadBarCodes property has to be set to true. Beside the change in file path there is no other changes in the code. Now, let's perform OCR on the PDF document that has QR codes:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-qr-codes.cs
using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True

' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithQrCodes.pdf")

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
	Console.WriteLine(barcode.Value)
Next barcode
VB   C#
Reading result

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.