How to Use Webcam as Barcode Reader in C#

Last Updated on 2020-08-09

Use webcam as barcode scanner in C#

Barcode is undoubtedly a welcomed way to record and track data. It can represent data including pricing, inventory, user info and a variety of others. Because of this, barcode reading became one of the essential features for an image processing application, for instance, reading the enrollment info of a student, and tracking the book lending history in a library. Similar usages can also be found in banks, hospitals and other organizations and companies.

In this article, I’ll show you how to implement barcode reading from web camera in your .NET Desktop application using Dynamic .NET TWAIN Webcam Module and Barcode Reader .NET API. Check how to implement online barcode reading from web camera if you want to implement it in a web application.

Key Features:

Here is a list of features available in the Dynamsoft’s .NET imaging SDKs:

  • Easy integration with your .NET desktop application.
  • Support comprehensive image sources, including webcams, TWAIN scanners, local folders and more
  • Support reading dozens of different barcode types and sub-types.
    1-D barcode: Code 39, Code 93, Code 128, EAN-8, EAN-13, ITF (Interleaved 2 of 5), UPC-A, UPC-E, Codabar;
    2-D barcode: QR Code, Datamatrix, and PDF417, MaxiCode, Aztec Code, DotCode, DataMatrix;
  • Great barcode recognition accuracy. It is capable of reading barcode symbols at any orientation and rotation angle.
  • Support variety of channels to store the attracted barcode info as well as the barcode image itself. It includes database, web server, FTP site, SharePoint and more.

Barcode Reader through Webcam Sample (C#)

Download sample code

Here I’ll share with you the code snippets in C# on how to capture images from webcams and read barcode symbols embedded using Dynamic .NET TWAIN and barcode reader SDK. It’s similar if you are using VB.NET.

1. Detect available webcam devices

The first step is to detect the webcams available on the machine. The GetCameraNames method enables you to get a list of drivers for selection. The SDK also allows you to hard code the selection and enforce the end users to capture images from a required webcam.
Based on your requirements you can adjust the image properties (resolution, pixel type, etc.), either to hard code these properties or set IfShowUI to true to show the user interface of the driver and provide your customers the most flexibility.

public Form1()
            m_ImageCore = new ImageCore();
            m_CameraManager = new CameraManager(m_StrProductKey);
            m_PDFRasterizer = new Dynamsoft.PDF.PDFRasterizer(m_StrProductKey);

            if (m_CameraManager.GetCameraNames().Count > 0)
                List<String> tempCameraNames = m_CameraManager.GetCameraNames();
                foreach(string temp in tempCameraNames)
                Camera tempCamera = m_CameraManager.SelectCamera(0);
                comboBox1.SelectedIndex = 0;

                List<CamResolution> listCamResolutions = tempCamera.SupportedResolutions;
                foreach (CamResolution temp in listCamResolutions)
                    string tempHeight = temp.Height.ToString();
                    string tempWidth = temp.Width.ToString();
                    string tempResolution = tempWidth + " X " + tempHeight;
                    comboResolution.SelectedIndex = 0;
                btnAcquireSource.Enabled = true;
                btnAcquireSource.Enabled = false;

2. Capture images embedding barcode symbols from a webcam

Once everything is ready for image acquisition, you can call GrabImage method to capture images or video streams from the device. It’s always neat to display the error messages when any exception occurs.

private void btnAcquireSource_Click(object sender, EventArgs e)
            Camera tempCamera = m_CameraManager.SelectCamera(m_CameraManager.CurrentSourceName);
            Bitmap tempBit = tempCamera.GrabImage();

3. Decode barcode info from the image from webcam capture

Now we are going to read the barcode info from the captured images.
The DecodeBitmap method enables you to do that. In the method, barcodeFormat can be set to All, which means the application will read the embedded barcode symbol and check with all the supported barcode types. This is perfect if you are not sure what kind of barcode you’ve already got. However, matching through the above list to determine the barcode type significantly reduces the detection performance. Thus, if you are sure that your customers are using, for instance, QR Code, it’s better to specify the barcode format in your source code.

private void btnReadBarcode_Click(object sender, EventArgs e)
            BarcodeReader m_BarcodeReader = new BarcodeReader();
            m_BarcodeReader.LicenseKeys = m_StrProductKey;
            m_BarcodeReader.ReaderOptions.MaxBarcodesToReadPerPage = 100;
            m_BarcodeReader.ReaderOptions.BarcodeFormats = Dynamsoft.Barcode.BarcodeFormat.OneD |
                Dynamsoft.Barcode.BarcodeFormat.PDF417 |
                Dynamsoft.Barcode.BarcodeFormat.QR_CODE |
            Bitmap bmp = null;
            if (m_ImageCore.ImageBuffer.CurrentImageIndexInBuffer < 0)
                MessageBox.Show("Please acquire or load an image before reading barcode.");
            bmp = (Bitmap)m_ImageCore.ImageBuffer.GetBitmap(this.m_ImageCore.ImageBuffer.CurrentImageIndexInBuffer);
            BarcodeResult[] aryResult = null;
            aryResult = m_BarcodeReader.DecodeBitmap(bmp);
            StringBuilder strText = new StringBuilder();
            if (aryResult == null)
                //this.txtBarcode.Text = "No barcode found.";
                MessageBox.Show("No barcode found.");
                strText.AppendFormat(aryResult.Length +" total barcode" + (aryResult.Length==1?".\r\n":"s"+" found.\r\n"));
                for (int i = 0; i < aryResult.Length; i++)
                    BarcodeResult objResult = aryResult[i];
                    strText.AppendFormat("      Result " + (i + 1) + ":\r\n");
                    strText.AppendFormat("      BarcodeFormat: " + objResult.BarcodeFormat.ToString() + "\r\n");
                    strText.AppendFormat("      Text read: " + objResult.BarcodeText + "\r\n");
                //this.txtBarcode.Text = strText.ToString();

The Barcode Reader SDK is also capable of detecting multiple barcode symbols in one image. By adding Error Correcting Codes (ECC), barcodes can be read even when they are partially damaged. To try out the above-mentioned features by yourself, you can download the 30-day free trial of Dynamic .NET TWAIN.

Try the SDK for Free

Let us know if you have any questions on embedding webcam capture and barcode reading in your .NET application.

The article was originally posted on Codeproject.

If you are interested in embedding webcam capture in a browser-based application, you may take a look at Dynamsoft Camera SDK.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.