Skip to Main Content

How To Read A Barcode From An Image In C#

read barcode from image

Goal:
Create a command line sample program to read a barcode from an image containing different types of barcodes that can handle damaged and low quality barcodes.

Requirements:

  • Microsoft Windows 7 or greater
  • Microsoft Visual Studio 2015 or greater
  • Accusoft Barcode Xpress .NET (download a free trial here)

Programming Skill:
C# Intermediate Level


When it comes to organizing items, barcodes are probably one of the most convenient – they’re simple, can be picked up by lasers and cameras, and are used across a wide range of businesses and organizations.

In this case, let’s assume that you’re going to be using the C# programming language to create your custom application and that you need to read barcodes from an image (eg from a camera or scanner). Perhaps you’re tracking inventory in a warehouse and have your own custom set of barcodes to track items. Or you’re in the medical field and need to scan when medications are checked in and out of storage. Maybe you flag documents with barcodes so everything that goes with a client can be quickly scanned and put into the right database without having some one upload them.

For this project, we’ll use Barcode Xpress SDK, which equips you to deal with a variety of barcode situations:

  • 1-dimensional barcodes
  • 2 dimensional barcodes (including QR codes)
  • Upside down or rotated barcodes
  • Damaged barcodes / low quality images
  • Images with multiple barcodes

Whatever the exact problem you need to tackle, Barcode Xpress is equipped to handle your barcode reading and generating needs. But let’s focus on reading for today. Keep things simple.


Getting Started

Start by downloading and installing the Barcode Xpress trial version. You can use the software under a free trial license (see license instructions here). You can change the evaluation mode to suit your project needs.

For this project, we can use Visual Studio 2017 (though it works just fine on previous versions if you’re not running on the bleeding edge of technology).

The most simple program will be to keep it on the command line. We can feed it file names to scan, tell it what kind of barcodes to look for, then collect the barcodes it detects, and display the information.

Here is how the program will work:

  1. Run the command line program. The first argument will be the type – for simplicity’s sake, we’re narrowing the scope to just two: 1-dimensional barcodes, and QR codes. But there’s a plethora of other types of barcodes you can scan from.
  2. If the file exists, read the barcode image file.
  3. Scan for barcodes in the file based on the type of barcodes we’re looking for.
  4. Display what the current barcode is that’s been scanned, what type is it, and its value(s).

Setting Up

  1. Download and install the trial version of Barcode Xpress .NET.
  2. Add the Accusoft Extension to your project – right click on “References”, click “Add Reference”, click “Extensions” and add “Accusoft Barcode Xpress 12 .NET” to your project. Otherwise – you’re not going to be able to use all of the tasty Barcode Xpress tools we’re trying to use.
    read barcode from image
  3. Add the code for our simple command line barcode reader (below) and compile it as SimpleBarcodeReaderCSharp.exe.

Code for command line barcode reader:


Reading in 1-D

One dimensional barcodes are not, in the literal sense, one dimensional. That discussion goes into realms of physics that while fascinating are just not within the scope of this documentation. But the way they’re read is in one dimension – a line cut horizontally across a 1 dimensional barcode reads the series of light and dark spots, to read the values based on established definitions.
read 1 D barcode from image

 

Let’s see how this works out now. We’ll give it one file for now with one barcode image:

 

And what are the results?

Well. That was easy. Let’s walk through the code and explore what’s going on.

We’ll skip over the introductory code and go right into our function “ReadBarCode”:

All we’re doing here is generating an object that will be able to take in an image file and scan it. If you were using Windows Forms perhaps you’d leverage Accusoft ImagXpress to load the barcode images and process them from there – but we can do this with the Bitmap object in C# for this example.

Since we’re just using two types of barcodes for our example, we’ve only created two switches for how to handle them. It’s either going to be a 1-dimensional, or a QR barcode. But if you wanted to go with Aztec or something else – that’s fine as well. You’ll find the entire list of supported types in our documentation on the on the Accusoft documentation page..

Note: For color images, you can greatly improve the recognition accuracy by using the ImagXpress autobinarize method.

Whatever you do now – don’t blink because it’s going to go by fast – here’s where we load our file into a Bitmap object, then sic the barcodeXpress object into analyzing it:

Did you blink? Yeah. It’s that fast. Barcode Xpress can process up to 1,000 pages per minute. The rest of the code is just walking through the various types of data we can extract from the barcode detected.

That fast, and that easy.


Gotta Catch ‘Em All!

Reading multiple barcodes from one file:

What if you have multiple barcodes within your document? If we look back at the code, the results variable was an array to store the results of every barcode that we were searching for in the document that matched the type of barcode we were interested in. Let’s give it a different file with multiple barcodes in it:

Add the Accusoft Extension

And when we run our program against it, we get five different barcodes, and all of them scanned, identified, encoded, and their value displayed.


Reading damaged barcodes

What about barcodes that have been damaged or overwritten in some way? They’ve been stamped over, written over – the poor things have been abused.

read barcode from image samples

Let’s run our program against it. You see – Barcode Xpress includes the capacity to deal with damaged barcodes and still get a result.

Add the Accusoft Extension

Every single one.


Moving Into the Next Dimension: Reading 2D/QR Codes

Perhaps we have a need for something more complex than 1-dimensional barcodes. 2-dimensional barcodes can give us a greater range of data to transmit and display. There are many different standards, but for the sake of this example let’s stick with QR codes. (For additional supported types the documentation on Supported Barcode Types can provide more information).

Since we already set up our program to be able to handle switching from 1-dimensional barcode to QR codes, no change to the code needed – it’s already taken care of. All we have to do is change our barcode type from 1 to 2 on the command line switch, and the system will handle all of these QR codes:

read barcode from image QR Code Samples

We have multiple codes, in multiple orientations, different sizes. Not only will we get the values, but we can also pick up on the rows and columns that make up the QR code.

Let’s try one more just for kicks. What if you have a document with multiple barcodes – but you only want to scan for one type of barcode?

Add the Accusoft Extension

Here we have a file with both QR codes and a 1-dimensional barcode in it, but we’re going to set out switch to only look for the 1-dimensional barcodes. Survey says?

Add the Accusoft Extension

We just have results for the barcode embedded in the file. Let’s go the other way – same file, but now let’s look just for QR codes:

Add the Accusoft Extension

Imagine the situation – you’re dealing with thousands of documents, scanned in and needing to be organized. Or a system with multiple barcodes to track the information in. Scan every file, find every barcode and use the data quickly and easily.


Integrating Into Your Application

Most likely, you’re looking to integrate barcode reading functionality into a larger application you’re developing, rather than build a stand-alone command line barcode reader. With just a few simple changes, we can use the same code above to easily add barcode reading capability into your C# application.

This function takes our ReadBarCode, and shortens it down to return an array of Accusoft BarcodeXpress Results to be parsed as required.

Here’s the updated code:

And just use this line of code in your application to read the barcode(s) from any image:

The function will return an array of Results. From here, you can easily save the returned results into a database, or pass them along to another part of your application.

The question now becomes – what will you do with this power?

For more information, check out the the Barcode Xpress overview page or the Barcode Xpress How To page.