WinForm Barcode Reader with Webcam and C#

When building a .NET application to read barcodes on Windows via camera, you need two types of SDKs - one for webcam, and the other for barcodes. In this post, I want to share how to use .Net webcam and barcode SDKs to create a simple WinForm barcode reader application in C#.

webcam barcode reader in C#

What You Should Know

WinForm Barcode Reader

.NET Webcam SDK – Touchless

Touchless SDK is a free and open source SDK for .NET applications. It enables developers to create multi-touch based applications using a webcam for input. The SDK simplified the camera interface by wrapping DirectShow API.

.NET Barcode SDK – Dynamsoft Barcode Reader

Dynamsoft Barcode Reader SDK is a commercial barcode SDK developed by Dynamsoft. If you do not have a valid license, you can use the trial version for evaluation. As for free barcode SDK, ZXing.NET is a good option.

How to build a Windows barcode app

Launch Visual Studio 2015 to create a WinForm project.

Add Dynamsoft Barcode Reader to the project via Nuget:

install .NET barcode SDK via nuget

Add TouchlessLib.dll to reference:

touchless sdk

Add WebCamLib.dll to project. Set ‘Copy always’ to ‘Copy to Output Directory’ in order to automatically copy the dll to output directory when building the project.

webcamlib

Initialize Touchless and Dynamsoft Barcode Reader:

// Initialize Dynamsoft Barcode Reader
_barcodeReader = new BarcodeReader();
// Initialize Touchless
_touch = new TouchlessMgr();

Show a system dialog to load an image into PictureBox:

            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bitmap = null;
                    
                    try
                    {
                        bitmap =  new Bitmap(dlg.FileName);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show("File not supported.");
                        return;
                    }

                    pictureBox1.Image = new Bitmap(dlg.FileName);
                }
            }

Start webcam to acquire images and register a callback function for receiving frames:

            // Start to acquire images
            _touch.CurrentCamera = _touch.Cameras[0];
            _touch.CurrentCamera.CaptureWidth = _previewWidth; // Set width
            _touch.CurrentCamera.CaptureWidth = _previewHight; // Set height
            _touch.CurrentCamera.OnImageCaptured += new EventHandler<CameraEventArgs>(OnImageCaptured); // Set preview callback function

The frame event is triggered in camera thread. You have to render results in UI thread:

        private void OnImageCaptured(object sender, CameraEventArgs args)
        {
            // Get the bitmap
            Bitmap bitmap = args.Image;

            // Read barcode and show results in UI thread
            this.Invoke((MethodInvoker)delegate
            {
                pictureBox1.Image = bitmap;
                ReadBarcode(bitmap);
            });
        }

Read bitmap and display results on TextBox:

        private void ReadBarcode(Bitmap bitmap)
        {
            // Read barcodes with Dynamsoft Barcode Reader
            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            BarcodeResult[] results = _barcodeReader.DecodeBitmap(bitmap);
            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");

            // Clear previous results
            textBox1.Clear();

            if (results == null)
            {
                textBox1.Text = "No barcode detected!";
                return;
            }

            // Display barcode results
            foreach (BarcodeResult result in results)
            {                
                textBox1.AppendText(result.BarcodeText + "\\n");
                textBox1.AppendText("\\n");
            }
        }

Build and run the WinForm barcode application:

WinForm barcode reader in C#

How to monitor .NET API performance

When using an algorithm API, you may care about the time cost more than accuracy. To calculate the elapsed time, use the code as follows:

            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            BarcodeResult[] results = _barcodeReader.DecodeBitmap(bitmap);
            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");

Source Code

https://github.com/yushulx/windows-webcam-barcode-reader