I’ve written a few posts recently on computer vision and optical character recognition. This time, I thought I’d write about a more traditional way of allowing computers to read printed information – barcode scanning.

I’ve run across a few instances in my career where applications have a need for this – for example, scanning stock inventory in and out of a warehouse. The traditional way of doing this would be to use a hardware barcode scanner connected to a computer.  These are basically the same technology as you’d see at your local supermarket – the scanner is pointed at the item’s barcode (usually a 1-D barcode), and when a valid barcode is detected, the textual representation of the code is piped to the computer’s cursor (often finishing with a newline character).

WP_20160402_20_05_41_Pro_LI

In the barcode scanner shown above, I didn’t need to install any software to my Windows 10 computer – not even a driver, or an SDK. Getting this to work was easy – open notepad, point the scanner at the barcode, squeeze the scanner’s trigger and the numeric representation of the barcode appears in notepad, with a newline character at the end.

What about reading and writing barcodes in C#?

A barcode scanner might not be always be suitable for our applications – you may already have a digital image, and want to know what this barcode represents in English text. Also, this scanner only reads 1-D barcodes, which hold a small amount of data. 2-D barcodes (sometimes known as QR codes) are now common, which can hold a lot more data.

There’s several .NET solutions available to allow us to read barcodes from an image- the one I’m going to look at today is ZXing.NET. This is a .NET port of a Java project, and it’s available on Nuget under the Apache 2 licence at a beta status.

Let’s look at some examples and code.

Reading Barcodes with ZXing.NET in C#

First thing is to import the ZXing.NET nuget package into your project.

Install-Package ZXing.Net 

Next, let’s get a barcode – I’ve uploaded a PNG of the QR barcode that I want to decode.

qrimage

We can use the code below to read from this image from my desktop:

static void Main(string[] args)
{
    // create a barcode reader instance
    var barcodeReader = new BarcodeReader();
 
    // create an in memory bitmap
    var barcodeBitmap = (Bitmap)Bitmap.FromFile(@"C:\Users\jeremy\Desktop\qrimage.bmp");
 
    // decode the barcode from the in memory bitmap
    var barcodeResult = barcodeReader.Decode(barcodeBitmap);
 
    // output results to console
    Console.WriteLine($"Decoded barcode text: {barcodeResult?.Text}");
    Console.WriteLine($"Barcode format: {barcodeResult?.BarcodeFormat}");
}

The output on the console shows that this barcode contains a link to my twitter feed, and correctly identifies the format as a QR code:

Decoded barcode text: https://twitter.com/jeremylindsayni
Barcode format: QR_CODE

There’s more about the different barcode formats here.

The code above isn’t an example of best practice – it’s simply just to show how to read a barcode.

Writing Barcodes with ZXing.NET in C#

Let’s suppose we want to programmatically generate a barcode in C# – it’s pretty easy to do this as well.

Say we want to generate a QR code of a link to my blog –

static void Main(string[] args)
{
    // instantiate a writer object
    var barcodeWriter = new BarcodeWriter();
 
    // set the barcode format
    barcodeWriter.Format = BarcodeFormat.QR_CODE;
 
    // write text and generate a 2-D barcode as a bitmap
    barcodeWriter
        .Write("https://jeremylindsayni.wordpress.com/")
        .Save(@"C:\Users\jeremy\Desktop\generated.bmp");
}

The output shown below:

generated

Conclusion

I hope this is helpful to anyone trying to read or generate barcodes – the code is pretty simple. As I mentioned above, ZXing.NET is licenced under the Apache 2 licence and is open sourced at Codeplex. One more thing that is worth mentioning is that at the time of writing, it’s still in beta and the present nuget package – version 0.14.0.1 – hasn’t been updated since April 2014.