How to Read QR Code from Webcam in C#
By FoxLearn 12/10/2024 2:20:12 AM 34.75K
In this guide, we’ll explore how to use a webcam to scan QR codes in a C# application. For this, you'll need to install the following NuGet packages in Visual Studio: AForge, AForge.Video, AForge.Video.DirectShow, and ZXing.Net.
How to Read QR Code from Webcam in C#?
Open Visual Studio, then create a new Windows Forms Application.
Next, You need to drag and drop the TextBox, Label, PictureBox, Combobox and Button from the Visual Studio Toolbox into your form designer, then design a simple UI that allows you to read data QR Code using webcam camera as shown below.
C# Read QR Code from Webcam
You need to install the AForge, AForge.Video, AForge.Video.DirectShow, ZXing.Net packages from the Nuget Manage Packages in your Visual Studio.
The AForge.Video library contains interfaces and classes to help you access different video sources, such as webcame, IP video cameras. The library defines types used by other video related libraries from AForge.NET framework.
The AForge.Video.DirectShow library contains classes, which allow to access video sources using DirectShow interface (USB web cameras, capture devices, video files, etc.).
ZXing.Net is a port of ZXing. It's an open-source, multi-format 1D/2D barcode image processing library originally implemented in Java.
It has been ported by hand with a lot of optimizations and improvements.
It's compatible with .Net 2.0, .Net 3.5, .Net 4.0, .Net 4.5, .Net 4.6, .Net 4.7, .Net 4.8, Windows RT Class Library and Components, UWP, .Net Standard 1.0, 1.1, 1.3 and 2.0, Silverlight 4, Silverlight 5, Windows Phone 7.x and Windows Phone 8.x and Xamarin.Android
In your Form’s code, declare the following variables to handle the webcam input:
FilterInfoCollection filterInfoCollection; VideoCaptureDevice videoCaptureDevice;
In the Form_Load
event, detect and list available video input devices.
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; videoCaptureDevice = new VideoCaptureDevice(); }
Add a Start
button click event to start capturing video from the selected webcam.
private void btnStart_Click(object sender, EventArgs e) { videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cboCamera.SelectedIndex].MonikerString); videoCaptureDevice.NewFrame += FinalFrame_NewFrame; videoCaptureDevice.Start(); }
Use the NewFrame
event to display the live webcam feed in a PictureBox
control.
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) { pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); }
Open the form designer, then drag the timer control from the visual studio toolbox to your winform, then set Interval property to 1000 (1 second). Next, Double click on timer control to add the timer tick event handler as the following c# code.
private void timer1_Tick(object sender, EventArgs e) { // zxing read qr code from image c# BarcodeReader Reader = new BarcodeReader(); Result result = Reader.Decode((Bitmap)pictureBox1.Image); if (result != null) txtResult.Text = result.ToString(); }
Start the timer when the Decode
button is clicked.
private void btnDecode_Click(object sender, EventArgs e) { timer1.Start(); }
To clean up, stop the webcam when the form is closed.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (videoCaptureDevice.IsRunning == true) videoCaptureDevice.Stop(); }
Ensure you include the necessary namespaces.
using AForge.Video; using AForge.Video.DirectShow; using ZXing;
Run your application, and after selecting the webcam, click the "Start" button to begin scanning for QR codes. If a QR code is detected, the result will be displayed in a text box.
For testing, you can either generate a qr code online or use a printed version to scan.
This tutorial delves into integrating webcam support with QR code scanning functionality in C# using AForge.NET and ZXing.Net. It provides the ultimate guide for creating a simple, effective QR code scanner using a webcam and .NET libraries.
VIDEO TUTORIAL