How To Read A Barcode From An Image In C#
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.
- Microsoft Windows 7 or greater
- Microsoft Visual Studio 2015 or greater
- Accusoft Barcode Xpress .NET (download a free trial here)
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.
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:
- 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.
- If the file exists, read the barcode image file.
- Scan for barcodes in the file based on the type of barcodes we’re looking for.
- Display what the current barcode is that’s been scanned, what type is it, and its value(s).
- Download and install the trial version of Barcode Xpress .NET.
- 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.
- 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
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:
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.
Let’s run our program against it. You see – Barcode Xpress includes the capacity to deal with damaged barcodes and still get a result.
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:
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?
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?
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:
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?