Skip to main content

Read Barcodes in C#

Photo from Pexels

Originally Posted On: https://ironsoftware.com/csharp/barcode/tutorials/reading-barcodes/

 

  • How to Read Barcodes in C# and VB.NET
    1. Install IronBarcode from Nuget or the DLL download
    2. Use the BarcodeReader.QuicklyReadOneBarcode method to read any barcode or QR
    3. Read Multiple Barcodes or QRs in a single scan, PDF, or a multiframe Tiff file
    4. Allow Iron Barcode to read from imperfect scans and photos
    5. Download the tutorial project and get scanning now
    Install the Barcode Library to your Visual Studio Project

    Iron Barcode provides a versatile, advanced, and efficient library for reading barcodes in .NET.

    The first step will be to install Iron Barcode, and this is most easily achieved using our NuGet package, although you may also choose to manually install the DLL to your project or to your global assembly cache. IronBarcode works well to produce a C# Barcode Scanner application.

     PM > Install-Package Barcode
    Read your First Barcode

    Reading a Barcode or QR Code in .NET is incredibly easy using the Iron Barcode class library with .NET Barcode Reader. In our first example, we can see how to read this Barcode with one line of code.

    We can extract its value, its image, its encoding type, its binary data (if any), and we can then output that to the console.

    1. using IronBarCode;
    2. using System;
    3. BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");
    4. if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
    5. {
    6. Console.WriteLine("GetStarted was a success. Read Value: " + Result.Text);
    7. }

       Copy code to clipboardVB 

     C#

    Try Harder and Be Specific

    In this next example, we will add our TryHarder variable to the QuicklyReadOneBarcode method. This makes it try harder, literally taking more time, but scanning deeper for a QR code that might be obscured, corrupted, or at a skewed angle.

    1. BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128 , true);

       Copy code to clipboardVB 

     C#

    Which will read this skewed QR Code:

    In our example, you can see that we can specify the barcode encoding(s) that we are looking for or specify multiple formats. Doing so greatly improves barcode reading performance and acuracy. The | pipe character or ‘Bitwize OR’ is used to specify multiple formats simultaneously.

    The same can be achieved but with a higher degree of specificity if we move forwards to use the BarcodeReader.ReadASingleBarcode Method.

    1. BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);

       Copy code to clipboardVB 

     C#

    Reading Multiple BarcodesPDF Documents

    In our next example, we are going to look at reading a scanned PDF document and find all of the barcodes of one-dimensional format in very few lines of code.

    As you can see, it is very similar to reading a single barcode from a single document, except we now have new information about which page number the barcode was found on.

    1. using IronBarCode;
    2. using System;
    3. using System.Drawing;
    4. // Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
    5. PagedBarcodeResult[] PDFResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
    6. // Work with the results
    7. foreach (var PageResult in PDFResults)
    8. {
    9. string Value = PageResult.Value;
    10. int PageNum = PageResult.PageNumber;
    11. System.Drawing.Bitmap Img = PageResult.BarcodeImage;
    12. BarcodeEncoding BarcodeType = PageResult.BarcodeType;
    13. byte[] Binary = PageResult.BinaryValue;
    14. Console.WriteLine(PageResult.Value+" on page "+ PageNum);
    15. }

       Copy code to clipboardVB 

     C#

    We find the following barcodes on different pages.

     

    Scans and TIFFs

    In our next example, we can see that the same result may be found from a multi-frame TIFF, which will be treated similar to a PDF in this respect.

     

    1. // Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
    2. PagedBarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
    3. foreach (var PageResult in MultiFrameResults)
    4. {
    5. //...
    6. }

       Copy code to clipboardVB 

     C#

    MultiThreading

    To read multiple documents, we get better results from Iron Barcode by creating a list of the documents and using the BarcodeReader.ReadBarcodesMultithreaded method. This uses multiple threads and potentially all cores of your CPU for the barcode scanning process and can be exponentially faster than reading barcodes one at a time.

    1. // The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarCode.
    2. var ListOfDocuments = new[] { "Image1.png", "image2.JPG", "image3.pdf" };
    3. PagedBarcodeResult[] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
    4. // Work with the results
    5. foreach (var Result in BatchResults)
    6. {
    7. string Value = Result.Value;
    8. //...
    9. }

       Copy code to clipboardVB 

     C#

    Reading Barcodes from Imperfect Images

    In real world use cases, we may wish to read barcodes which are not perfect screenshots. They may be imperfect images, scans, or photographs and contain digital noise or be skewed. With most conventional open source .net barcode generator and reader libraries, this would be impossible. However, this Barcode Reader in C# makes this incredibly straightforward.

    In our next example, we will look at the TryHarder method of QuicklyReadOneBarcode. This single parameter causes Iron Barcode to try to de-skew and read barcodes from imperfect digital samples.

    Photographs

    In the photographs example, we will set specific barcode rotation correction and barcode image correction to correct for the digital noise and for the skew and perspective and rotation that we might reasonably expect from a cellphone camera.

     

    1. using IronBarCode;
    2. using System;
    3. using System.Drawing;
    4. // All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
    5. // * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
    6. // * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates Barcodes from background imagery and digital noise.
    7. // * BarcodeEncoding e.g. BarcodeEncoding.Code128 Setting a specific Barcode format improves speed and reduces the risk of false positive results
    8. // Example with a photo image
    9. var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
    10. string Value = PhotoResult.Value;
    11. System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
    12. BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
    13. byte[] Binary = PhotoResult.BinaryValue;
    14. Console.WriteLine(PhotoResult.Value);

       Copy code to clipboardVB 

     C#

    Scans

    The next example shows us how we might approach reading QR codes and PDF-417 barcodes from a scanned PDF. Note that we have set an appropriate level of barcode rotation correction and barcode image correction to lightly clean the document, but not to incur a huge performance penalty by over-egging our needs.

     

    1. // Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
    2. var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);
    3. // Work with the results
    4. foreach (var PageResult in ScanResults)
    5. {
    6. string Value = PageResult.Value;
    7. ///...
    8. }

       Copy code to clipboardVB 

     C#

    Thumbnails

    In the final example, we will see that this C# Barcode Generator is even capable of reading a corrupted thumbnail of a barcode.

    Our barcode reader methods automatically detect barcode images which are too small to reasonably be an actual barcode, and upscale and clean all of the digital noise associated with thumbnailing; allowing them to be readable again.

    1. // Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
    2. BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);

       Copy code to clipboardVB 

     C#

    Summary

    In summary, Iron Barcode is a versatile .Net software library and C# QR Code Generator for reading a wide range of barcode formats, and it can do so whether or not these barcodes are perfect screen grabs or are in fact photographs, scans, or other imperfect real world images.

    Further Reading

    To learn more about working with Iron Barcode, you may wish to look at the other tutorials within this section, and also the examples on our homepage; which most developers find enough to get them started.

    Our API Reference with specific reference to the BarcodeReader class and the BarcodeEncoding Enum will show you in detail what you are capable of achieving using this C# Barcode Library.

    Source Code Downloads

    We also highly encourage you to download this tutorial and run it for yourself. You can do this by downloading the source code, or by forking us on GitHub. The source for this .NET Barcode Reader tutorial are available as Visual Studio 2017 Console Application project written in C#.

Data & News supplied by www.cloudquote.io
Stock quotes supplied by Barchart
Quotes delayed at least 20 minutes.
By accessing this page, you agree to the following
Privacy Policy and Terms and Conditions.