How to create a Barcode Scanner using Webcam in C#
By FoxLearn 7/19/2024 2:25:03 AM 18.16K
This is a simple example of how to create a barcode scanner using a webcam in C#. For this example, we'll be using the ZXing.Net library to create a barcode image or decode a barcode image, which is a popular open-source library for reading barcodes.
Next, We will use AForge.NET library to capture frames from the webcam.
How to read barcode using webcam in C#?
Start by creating a new C# Windows Forms project in Visual Studio, then open your form designer.
To create a barcode scanner in c# windows application, you can drag and drop the Label, Combobox, PictureBox, TextBox and Button controls from the Visual Studio toolbox to your form designer.
We will use the PictureBox control to display the webcam feed and possibly a TextBox or Label to display the scanned barcode.
Next, You can design a simple user interface that allows you to a detect webcam or camera, then add the device name to the Combobox control.
Finally, We will decode the barcode image taken from the webcam, then update the image taken from the webcam to the PictureBox control.
After you complete your form design, you need to right-click your project, then select Manage Nuget Packages.
There are several libraries available for barcode scanning in C#. Popular choices include ZXing.Net (an open-source library).
Search for 'AForge.Video.DirectShow', 'ZXing.NET' and install it to your project.
You can install the ZXing.Net library via NuGet Package Manager Console by running the following command.
Install-Package ZXing.Net
Similar, to install the AForge.Video and AForge.Video.DirectShow packages via NuGet Package Manager Console
Install-Package AForge.Video Install-Package AForge.Video.DirectShow
Use AForge.NET or DirectShow.NET libraries to access webcam feed.
Barcode scanner library c#
ZXing.NET is a c# barcode reader open source or qr code reader c# open source.
Add code to display the webcam feed in the PictureBox control on your form.
Declare two variables as shown below.
FilterInfoCollection filterInfoCollection; VideoCaptureDevice videoCaptureDevice;
And don't forget to include the namespaces below.
using AForge.Video.DirectShow; using ZXing;
Open your form designer, then add a Form_Load event handler that allows you to detect your webcam or camera name.
// Set up the video capture device (webcam) private void Form1_Load(object sender, EventArgs e) { filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (FilterInfo device in filterInfoCollection) cboCamera.Items.Add(device.Name); cboCamera.SelectedIndex = 0; }
Adding a click event handler to the Start button that allows you to capture picture from the webcam.
private void btnStart_Click(object sender, EventArgs e) { videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cboCamera.SelectedIndex].MonikerString); videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame; videoCaptureDevice.Start(); }
And don't forget to add a NewFrame event handler allows you to decode barcode from the webcam feed using ZXing.Net
Integrate the barcode scanning functionality into your application as shown below.
private void VideoCaptureDevice_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); // Create a barcode reader instance BarcodeReader reader = new BarcodeReader(); // Decode the barcode var result = reader.Decode(bitmap); if (result != null) { // If a barcode is detected, print its content txtBarcode.Invoke(new MethodInvoker(delegate () { txtBarcode.Text = result.ToString(); })); } pictureBox.Image = bitmap; }
Finally, Add a FormClosing event handler allows you to stop your webcam when closing your form.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (videoCaptureDevice != null) { if (videoCaptureDevice.IsRunning) videoCaptureDevice.Stop(); } }
This is free barcode scanner using webcam in c#. So you're done creating an app that lets you read barcodes from your webcam in C#.
You can do the same way to help you scan qr code using webcam in c#.
You can easily create a free barcode online by acessing this website: Link
Or you can view the post "How to create a barcode image in C#" to know how to create a barcode image in C#.
VIDEO TUTORIAL