Click here to Skip to main content
Click here to Skip to main content

Tagged as

Scan Barcode from a Webcam in a WinForm/WPF Application

, 5 Nov 2012 CPOL
Embed Dynamic .NET TWAIN to your .NET application. Capture images from your webcams and read the barcode information.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

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 introduce Dynamic .NET TWAIN and its Barcode Reader SDK add-on provided by Dynamsoft, and show you how to implement the above mentioned features in your .NET application. If you are interested in customizing the functions, both the sample code and the 30-day free trial of the SDK are provided.

Key Features

Here is a list of features available in the SDKs.

  • Easy integration with your .NET application.
  • Support comprehensive image sources, including webcams, scanners, local folders and more.
  • Support reading dozens of different barcode types and sub-types.
    1-D barcode: Code 93, Code 128, EAN – 8, EAN -13, ITF (Interleaved 2 of 5), UPC-A, UPC-E, Codabar, RSS-14
    2-D barcode: Aztec, DataMatrix, MaxiCode, QR Code, PDF417
  • Great 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 the barcode image itself. It includes database, web server, FTP site, SharePoint and more.

Sample Code

In this section, I’ll share with you the sample code on how to capture images from webcams and read barcode symbols embedded.

  1. Embed Dynamic .NET TWAIN to your .NET application.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Dynamsoft.DotNet.TWAIN.Enums;
    using Dynamsoft.DotNet.TWAIN.Enums.Barcode;
    using Dynamsoft.DotNet.TWAIN.Barcode;
    using Dynamsoft.DotNet.TWAIN.WebCamera;
  2. Capture images embedding barcode symbols from a webcam.

The first step is to detect the webcams available on the machine. The SelectSource method enables you to get a list of drivers for selection. The SDK also allows you to hardcode 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 hardcode these properties or set IfShowUI to true to show the user interface of the driver, and provide your customers the most flexibility.

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.chkIfThrowException.Checked)
                    dynamicDotNetTwain1.IfThrowException = true;
                else
                    dynamicDotNetTwain1.IfThrowException = false;

                dynamicDotNetTwain1.SelectSource();
                EnumSupportedDeviceType en = dynamicDotNetTwain1.SupportedDeviceType;
                dynamicDotNetTwain1.IfShowUI = true;
                dynamicDotNetTwain1.SetVideoContainer(this.pictureBox1);
                dynamicDotNetTwain1.OpenSource();
                int count = dynamicDotNetTwain1.ResolutionForCamList.Count;
                for (int j = 0; j < count; j++)
                {
                    string tempHeight = dynamicDotNetTwain1.ResolutionForCamList[j].Height.ToString();
                    string tempWidth = dynamicDotNetTwain1.ResolutionForCamList[j].Width.ToString();
                    string tempResolution = tempWidth + "X" + tempHeight;
                    comboResolution.Items.Insert(j, tempResolution);
                    comboResolution.SelectedIndex = 0;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);

            }
        }

Once everything is ready for image acquisition, you can call EnableSource() 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)
        {

            try
            {
                if (this.chkIfThrowException.Checked)
                    dynamicDotNetTwain1.IfThrowException = true;
                else
                    dynamicDotNetTwain1.IfThrowException = false;
                dynamicDotNetTwain1.EnableSource();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
  1. Read barcode info.

Now we are going to read the barcode info from the captured images. The ReadBarcode method enables you to do that. In the method, barcodeFormat.All means the application will read the embedded barcode symbol and check with all the supported barcode types, including Code 93, Code 128, EAN – 8, EAN -13, ITF (Interleaved 2 of 5), UPC-A, UPC-E, Codabar, RSS-14, Aztec, DataMatrix, MaxiCode, QR Code and PDF417.

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)
        {
            this.txtBarcode.Text = "";
            Result[] aryResult = this.dynamicDotNetTwain1.ReadBarcode(this.dynamicDotNetTwain1.CurrentImageIndexInBuffer, BarcodeFormat.All);
            StringBuilder strText = new StringBuilder();
            strText.AppendFormat(aryResult.Length + " total barcode" + (aryResult.Length == 1 ? "" : "s") + " found.\r\n");
            for (int i = 0; i < aryResult.Length; i++)
            {
                Result objResult = aryResult[i];
                strText.AppendFormat("      Result " + (i + 1) + "\r\n");
                strText.AppendFormat("      BarcodeFormat: " + objResult.BarcodeFormat.ToString() + "\r\n");
                strText.AppendFormat("      Text read: " + objResult.Text + "\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.

Get Samples

To try out the above mentioned features by yourself, you can download the 30-day free trial of Dynamic .NET TWAIN below.
Dynamic .NET TWAIN 30-day Free Trial

Besides the sample code provided in the article, you can also try out more samples at the following page:
Dynamic .NET TWAIN Demo

If you have any questions, you can contact our support team at nettwain@dynamsoft.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Windy Liu

United States United States
No Biography provided

Comments and Discussions

 
QuestionCompiles cleanly but no barcodes recognized Pinmemberarmstrpd22-Jul-13 14:01 
AnswerRe: Compiles cleanly but no barcodes recognized PinmemberWindy Liu23-Jul-13 1:28 
GeneralRe: Compiles cleanly but no barcodes recognized Pinmemberarmstrpd23-Jul-13 7:41 
GeneralRe: Compiles cleanly but no barcodes recognized [modified] Pinmemberarmstrpd23-Jul-13 8:54 
GeneralRe: Compiles cleanly but no barcodes recognized PinmemberWindy Liu24-Jul-13 23:28 
QuestionMessage Removed PinmemberMember 984580012-Jun-13 18:35 
AnswerRe: difference with normal .NET barcode image reading sdk PinmemberWindy Liu13-Jun-13 16:50 
Questiongreat Pinmemberkiquenet.com14-Nov-12 4:09 
AnswerRe: great PinmemberWindy Liu14-Nov-12 15:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Nov 2012
Article Copyright 2012 by Windy Liu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid