How to Read Barcodes and QR Codes
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.
Get started with IronOCR
Start using IronOCR in your project today with a free trial.
How to Read Barcodes and QR Codes
- Download a C# library to read barcodes and QR codes
- Import the target image and PDF document
- Enable barcode reading by setting the ReadBarCodes property to true
- Use the
Read
method to perform OCR as usual - Output the detected text and barcode values
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
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