Barcode scanning is a critical part of many industries, from inventory management to retail and logistics. In this article, I'll demonstrate how to create a Windows Forms Application in C# for barcode scanning using IronBarcode. It will use webcam to scan the barcode and then utilize the IronBarcode to reveal the information. IronBarcode is a powerful C# barcode generation and scanning library that simplifies working with all type of barcodes.
IronBarcode is a popular .NET barcode generation and scanning library. It provides a simple way to generate barcodes and scan them from saved images or captured frames from live video feeds. With IronBarcode, you can read and generate various barcode types, including QR codes, UPC, and more. It simplifies barcode-related tasks in C# applications, making it a valuable tool for developers.
To work with the camera and perform barcode scanning, we will need to install OpenCVSharp4 and IronBarcode using the NuGet Package Manager from either tools menu or by right-clicking Solution Explorer:
After setting up everything, now its time to write the code for our Barcode reader application. Open Form1.cs file and follow the steps to correctly initialize camera and scan barcode formats easily using Iron software.
Add the following imports at the the top of the file:
using OpenCvSharp; using IronBarCode; using IronSoftware.Drawing;
Enter fullscreen mode
Exit fullscreen mode
Let's first declare the following fields in our Form1 class:
private VideoCapture videoCapture; private Mat frame; private AnyBitmap imageBitmap;
Enter fullscreen mode
Exit fullscreen mode
In the Form1_Load event, initialize the camera feed using OpenCVSharp.VideoCapture class. The (0) argument specifies that the default camera should be used. Check if the camera is available, and if not, display an error message.
private void Form1_Load(object sender, EventArgs e) < videoCapture = new VideoCapture(0); if (!videoCapture.IsOpened()) < MessageBox.Show("Camera not found. Make sure the camera is connected.", "Camera Error"); Close(); >Application.Idle += new EventHandler(ProcessFrame); >
Enter fullscreen mode
Exit fullscreen mode
The code Application.Idle += new EventHandler(ProcessFrame); connects the ProcessFrame method to the Application.Idle event. When the application is idle, the method is automatically executed, making it suitable for continuous actions like real-time video frame processing.
Now, lets create an event handler ProcessFrame to continuously capture frames from the camera which will be triggered in the Form1_Load method when .Net barcode scanner application is idle. This handler is attached to the Application.Idle event as shown in previous step.
private void ProcessFrame(object sender, EventArgs e) < frame = new Mat(); videoCapture.Read(frame); if (!frame.Empty()) < imageBitmap = new AnyBitmap(frame.ToBytes()); cameraFrame.Image = imageBitmap; >>
Enter fullscreen mode
Exit fullscreen mode
In this above code, we're doing a few things:
On successful execution of the application, the camera will initialize when the Form is loaded. Here's how it looks:
The following code will help read barcodes from the captured frame and display it on screen:
BarcodeReaderOptions barcodeReaderOptions = new BarcodeReaderOptions(); barcodeReaderOptions.ExpectMultipleBarcodes = true; barcodeReaderOptions.ExpectBarcodeTypes = BarcodeEncoding.All; BarcodeResults barcodeReader = BarcodeReader.Read(imageBitmap, barcodeReaderOptions); if (barcodeReader.Values().Length != 0) < foreach (var value in barcodeReader.Values()) < barcodeInfoBox.Text = value; Clipboard.SetText(value); MessageBox.Show("Barcode Information: " + value + "\nNote: Clicking Ok will also copy the information to clipboard.", "Barcode Detected!", MessageBoxButtons.OK, MessageBoxIcon.Information); >> elseEnter fullscreen mode
Exit fullscreen mode
In the above source code:
This code will be placed in the ProcessFrame action method. Here is the complete code:
private void ProcessFrame(object sender, EventArgs e) < frame = new Mat(); videoCapture.Read(frame); if (!frame.Empty()) < imageBitmap = new AnyBitmap(frame.ToBytes()); cameraFrame.Image = imageBitmap; BarcodeReaderOptions barcodeReaderOptions = new BarcodeReaderOptions(); barcodeReaderOptions.ExpectMultipleBarcodes = true; barcodeReaderOptions.ExpectBarcodeTypes = BarcodeEncoding.All; BarcodeResults barcodeReader = BarcodeReader.Read(imageBitmap, barcodeReaderOptions); if (barcodeReader.Values().Length != 0) < foreach (var value in barcodeReader.Values()) < barcodeInfoBox.Text = value; Clipboard.SetText(value); MessageBox.Show("Barcode Information: " + value + "\nNote: Clicking Ok will also copy the information to clipboard.", "Barcode Detected!", MessageBoxButtons.OK, MessageBoxIcon.Information); >> else < barcodeInfoBox.Text = "No Barcode Detected"; >> >
Enter fullscreen mode
Exit fullscreen mode
To further enhance barcode scanners .NET applications for faster barcode scanning and to read barcodes from PDF document, imperfect scans, automatically managed multiple documents, please visit this read barcode tutorial link.
Release the camera feed and dispose of the frame when the application is closing.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)Enter fullscreen mode
Exit fullscreen mode
The above screenshot shows the application is working correctly and IronBarcode didn't detected any Barcode data.
The above screenshot displays the Barcode images information in the box as well as MessageBox. This ensures the accuracy and efficiency of IronBarcode and QR code library.
This clearly shows IronBarcode is versatility enough for barcode reading from data matrix, binary data, either from camera feed or multiple images.
Creating a C# Barcode Scanner using IronBarcode and OpenCVSharp4 allows you to efficiently scan barcodes from live video feeds. By following the steps outlined above, you can build a Windows Forms Application that captures camera frames, processes them for barcodes, and provides user-friendly feedback.
This application can be a valuable tool for various industries, including retail, logistics, and inventory management. IronBarcode simplifies the process of reading barcodes, making it accessible to C# developers.
IronBarcode is free for development purposes but it can be licensed for commercial use. A 30-day free trail is also available to test out its complete functionality. You can download the software from here.