C# QR Code Reader (Step by Step Tutorial)

The barcode system is a method of encoding data and representing it visually so that it can be only be read by a machine. Barcodes/QR codes represent data by varying the widths and spacing of parallel lines. These lines are only recognized by a special device called a barcode reader. There are multiple types of barcodes available, such as 1D and 2D barcodes. Barcodes are mostly used on product labels so that they can be easily accessed by supermarkets as they are billing the product. There are a lot of QR code libraries/SDKs available on the market. Here we are going to use the Iron Barcode reader SDK. This SDK allows us to read encoded QR code barcodes and create/encode QR codes or barcodes into images/HTML files. It is extremely easy to use and offers a high level of performance when compared to other SDK libraries for the dot net framework.

1. Creating a New Project

Open Visual Studio and go to the File menu. Select "new project", and then select Console Application/ Windows Forms/WPF Application. Barcodes can also be used on all types of applications. Further, you can use them with apps such as Webform/MVC/MVC Core.

Enter the project name and select the file path in the appropriate text box in Visual Studio. Next, click the "Create" button, and also select the required dot net framework. The project will now generate the structure for the selected application. If you have selected the "console application", it will now open the program.cs file where you can enter the code and build/run the application.

2. Install NuGet Package BarCode

Now go to the solution explorer in Visual Studio and left-click the project. From the pop-up menu select the NuGet Package Manager from the menu and search for "Barcode" as a keyword. Select the first result in the NuGet package dialog and click the install option.

Alternatively, in Visual Studio follow these instructions: Go to Tools-> NuGet Package manager -> Package manager console

Open the package manager console tab. Type the line below:

Install-Package BarCode

Go to the following link to learn more about the latest version of the Barcode Reader SDK.

https://www.nuget.org/packages/barcode

Next, the package will download all the DLL files and also add the reference of the DLL file in the current project as a net project reference.

The Visual Studio project is now ready to use on the code.

3. Adding Reference

Add the references “IronBarCode” library to the code as shown below. This allows us to use the functions available in IronBarCode in net code. To know more about the barcode reader control, reading QR code, components, support for QR code types, and the combability of the barcode reader SDK, please refer to this link:

https://ironsoftware.com/csharp/barcode/

using IronBarCode;

4. Creating a Barcode Instance

Barcode reader is the static class that is used to read/create QR codes. It can be used without creating any object instance for the static class Barcode Reader. We are able to access all the components without creating any object for the main class. The barcode code reader control allows us to read 15 types of barcodes using the single function QuicklyReadOneBarcode. Using the IronBarCode library we are able to read QR codes/barcodes from the following four values:

  • String
  • Stream
  • Image
  • Bitmap

5. Read Barcode Using String

QR codes and barcode images can be read easily using the net barcode reader library. The net barcode reader library gets the file name as an input parameter and decodes the given QR code into text. The QuicklyReadOneBarcode is a function that will scan QR code images and decode QR codes and barcodes into text. It accepts various types of image files like bitmap, PNG files, JPEG, Tiff, etc.

QuicklyReadOneBarcode accepts the following three parameters:

  • Inputimagepath
  • Barcode format
  • TryHarder

Barcode format and Tryharder are the optional parameters that are not necessary to read/decode QR codes and barcodes, and they have the default value as BarcodeEncoding.All for barcode formats, which allows us to include all the QR codes and barcode images. The default value for Tryharder is false, which specifies the QR code reader to do the normal scanning. If we set the Tryharder values as true, then this will tell the code to do deep scanning on the QR code/barcode.

This function helps us to read QR code/barcode data from the image and retrieve the result as the object BarcodeResult. If the image file is not recognized by the code, it will return null. Inputimagepath is required to read barcodes. We need to add the image file path as a string, and this then enables us to read QR codes and barcodes.

The BarcodeResult has the following values:

  • Barcodetype
  • Value
  • Text
  • Binary value

BarcodeType is an enum data type and it returns the type of the barcode of the given input image. There are twenty-two barcode types supported by IronBarcode. It will return the type of barcode, which is applied to the given input image.

Value and text are also the object type values available on the BarcodeResult. Both value and text are string data types. That returns the string value of the barcode. QuicklyReadOneBarcode will read QR codes and barcodes and get the string value of the given barcode image into value/text. The binary value is the byte data type and returns the byte of the given image. Below is the quick response code which can be copy-coded to any net windows forms application or net standard applications.

BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("test.bmp");
// Assert that IronBarCode Works :-)
if (Result != null && Result.Text == "https://ironsoftware.com/csharp/barcode")
{
 System.Console.WriteLine("Success");
}
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("test.bmp");
// Assert that IronBarCode Works :-)
if (Result != null && Result.Text == "https://ironsoftware.com/csharp/barcode")
{
 System.Console.WriteLine("Success");
}
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("test.bmp")
' Assert that IronBarCode Works :-)
If Result IsNot Nothing AndAlso Result.Text = "https://ironsoftware.com/csharp/barcode" Then
 System.Console.WriteLine("Success")
End If
VB   C#

The above code shows how we can read QR code barcodes using the net barcode reader library. The code is very simple to use. In the above example, we are using a bitmap image file from the system which scans the QR codes/barcodes from the image file and reads QR codes from the image file. Also, it will decode the QR code/barcode from the image and show the result in the object. Below are the QR codes/barcodes given in code to scan for the encoded data.

6. Read Multiple Barcodes from the Image

The .NET barcode reader control allows the reading of multiple QR codes/barcodes from an image file. ReadBarcodesFromMultiFrameTiff is the function that helps us to read QR codes/barcodes from a TIFF image. This function only allows the reading of QR codes/barcodes from a TIFF image file. Other image formats such as JPEG, BMP, PNG, etc. are not supported by this function.

Below is the sample code for reading multiple QR codes from the image:

BarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
BarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
Dim MultiFrameResults() As BarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None)
VB   C#

In the above code, we are reading QR codes from TIFF QR code images. The net barcode reader scans QR codes from the image from the system and returns the result as an array. In the above code, we are only scanning the barcode for code128. If there are any other barcodes present on the image, they will be ignored.

7. Reading QR Code Barcodes from PDF

We can also able to read the QRcode from PDF documents. The barcode/QR code reader allows us to scan QR codes/barcodes from all the available pages on a PDF file. The QR code library will scan all the available pages and files for all the available QR codes, or we can specify the type of barcode we need to scan from the PDF page. When using Ironbarcode, we have already inherited all the components of the barcode reader, so we do not need to include any other DLL as a net project reference.

Below is the sample copy code which can be used to read QR codes/barcodes from PDF.

   PagedBarcodeResult[] PagedResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
            // Work with the results
            foreach (PagedBarcodeResult PageRes in PagedResults)
            {
                int pagenumber = PageRes.PageNumber;
                string Value = PageRes.Value;
                Bitmap Img = PageRes.BarcodeImage;
                BarcodeEncoding BarcodeType = PageRes.BarcodeType;
                byte[] Binary = PageRes.BinaryValue;
                Console.WriteLine(PageRes.Value);
            }
   PagedBarcodeResult[] PagedResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
            // Work with the results
            foreach (PagedBarcodeResult PageRes in PagedResults)
            {
                int pagenumber = PageRes.PageNumber;
                string Value = PageRes.Value;
                Bitmap Img = PageRes.BarcodeImage;
                BarcodeEncoding BarcodeType = PageRes.BarcodeType;
                byte[] Binary = PageRes.BinaryValue;
                Console.WriteLine(PageRes.Value);
            }
Dim PagedResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf")
			' Work with the results
			For Each PageRes As PagedBarcodeResult In PagedResults
				Dim pagenumber As Integer = PageRes.PageNumber
				Dim Value As String = PageRes.Value
				Dim Img As Bitmap = PageRes.BarcodeImage
				Dim BarcodeType As BarcodeEncoding = PageRes.BarcodeType
				Dim Binary() As Byte = PageRes.BinaryValue
				Console.WriteLine(PageRes.Value)
			Next PageRes
VB   C#

The above is the sample code to read QR codes/barcodes from PDF files. It also shows that we are reading QR codes/barcodes from all the pages of the PDF documents. Also, we can specify the type of barcode/QR code which needs to be read from a PDF file. ReadBarcodesFromPdf will scan all the pages available in PDF.

Conclusion

.NET Barcode readers provide a simple and easy way to read QR codes or decode/create QR codes and barcodes with simple steps. IronBarcode DLL can be used on various environments like net windows forms applications, mobile apps, and web apps using the dot net framework. IronBarcode offers a 30-day trial key, or you can currently buy five products from Iron Software for the price of just two.

https://ironsoftware.com/csharp/barcode/licensing/

You can download a file project from this link.