Read Barcode from Image in C#

Looking for an efficient way to read barcodes or QR codes from images programmatically? If you’re a developer building a barcode scanner application, this tutorial is for you. Barcodes play a crucial role in inventory management, logistics, and retail. With Aspose.BarCode for .NET, you can easily detect and extract various barcode types from images in C#. This guide provides a step-by-step approach to barcode recognition.

This article covers:

  1. C# API for Barcode Reading
  2. Steps to Extract Barcode Data
  3. Read Barcode from Bitmap
  4. Read Barcode from Image Stream
  5. Detect and Read Specific Barcode Type
  6. Recognize Multiple Barcode Types
  7. Identify Predefined Barcode Sets
  8. Extract Multiple Barcodes from an Image
  9. Get Barcode Coordinates
  10. Read Barcode from a Specific Image Region
  11. Scan Barcodes from Multiple Image Regions

C# API for Barcode Reading

To recognize barcodes in images, we use Aspose.BarCode for .NET. This API supports reading over 60 barcode symbologies across multiple formats, including JPEG, PNG, BMP, TIFF, and GIF.

The API provides the BarCodeReader class for barcode detection. You can specify symbologies using the DecodeType parameter. The ReadBarCodes() method extracts barcode data.

Install the API via NuGet:

PM> Install-Package Aspose.BarCode

Steps to Extract Barcode Data

Follow these steps to read a barcode from an image:

  1. Load an image using the BarCodeReader class.
  2. Call ReadBarCodes() to extract barcode information.
  3. Iterate through results to get barcode type and value.

Here’s a sample code snippet:

Read Barcode from Bitmap

To read a barcode from a bitmap:

  1. Load the image using the Bitmap class.
  2. Pass it to the BarCodeReader constructor.
  3. Extract barcode data using ReadBarCodes().

Read Barcode from Image Stream

You can also read barcodes from a stream:

  1. Load the image using FileStream.
  2. Pass it to the BarCodeReader.
  3. Call ReadBarCodes() to get barcode data.

Detect and Read Specific Barcode Type

To improve efficiency, specify a target barcode type:

  1. Load the image in BarCodeReader.
  2. Set a specific barcode type (e.g., Code39Standard).
  3. Call ReadBarCodes() and process results.

Recognize Multiple Barcode Types

For scanning multiple barcode types:

  1. Set multiple symbologies in SetBarCodeReadType().
  2. Call ReadBarCodes().
  3. Retrieve barcode data.

Identify Predefined Barcode Sets

Aspose.BarCode supports predefined barcode sets:

  • AllSupportedTypes - All available barcodes.
  • Types1D - All linear barcodes.
  • Types2D - QR Code, Data Matrix, etc.
  • MostCommonTypes - Frequently used formats.

To use predefined sets:

BarCodeReader reader = new BarCodeReader("image.png", DecodeType.Types2D);

Extract Multiple Barcodes from an Image

Detect multiple barcodes from an image:

  1. Load the image using BarCodeReader.
  2. Set the decode type to ALL_SUPPORTED_TYPES.
  3. Iterate through results to extract barcode data.

Get Barcode Coordinates

Retrieve barcode location (X, Y coordinates):

  1. Load the image in BarCodeReader.
  2. Call ReadBarCodes().
  3. Extract region points.

Read Barcode from a Specific Image Region

Limit barcode detection to a specific image region:

  1. Define a rectangular area.
  2. Pass it to BarCodeReader.
  3. Call ReadBarCodes().

Scan Barcodes from Multiple Image Regions

To scan barcodes from multiple regions:

  1. Define multiple rectangular areas.
  2. Pass them to BarCodeReader.
  3. Extract barcode data.

Get a Free License

Request a free temporary license to explore the full capabilities of Aspose.BarCode.

Conclusion

In this article, you learned:

  • How to load and process barcode images.
  • How to extract barcode information from different sources.
  • How to detect and recognize multiple barcode types.