How to implement barcode reading from the video stream of an USB camera in C#
In this lecture you will find all the necessary information to satisfy your curiosity on the field of barcode reading from the video stream of a USB camera with the help of your C# camera application. To implement this example successfully, Ozeki Camera SDK has to be installed and a reference to OzekiSDK.dll has to be added to your Visual Studio project.
Important: you should study this article in order to find out how to setup your Windows Forms Application correctly.
Getting started
To get started it is recommended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:
Download Ozeki Camera SDK: | https://www.camera-sdk.com/https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html |
Windows forms version: | C:\Users\user\Documents\Ozeki\Ozeki SDK\Examples\Camera\02_USBCamera |
To compile this example you will need Microsoft Visual Studio installed on your computer.
Corporate use of barcode reading
The barcode scanner function is a great help in corporate environments. If you have a smaller office and you want to register your equipment, the USB camera barcode scanner is the best choice for you. It is capable to recognize barcodes and QR-codes and only requires an USB-port to do so. If you want to avoid any chances of a security breach by using this application you can avoid the using of IP cameras to effectively recognize barcodes. We can mention another example, if you use QR-codes and you want to host a lottery, you can use an application developed by using Ozeki Camera SDK to validate the tickets of your winners.
On the first illustration you can find an example for barcode reading and how this function can help you in supervising, detecting and identifying the items and tools which can be found in limitless amounts in your warehouse, shop, etc.
On the second picture you will find the QR code detecting function illustrated. The using of the QR-code is extremely popular in industrial environment, in logistics and in production since it is an effective medium for storing data. Using the QR-code detecting function of the Ozeki Camera SDK you can easily benefit from the advantages of QR-code detecting.
We can mention another fields where the barcode scanning function can be used efficiently: If your company provides VIP-tickets you can use QR-codes as a safety measure to help avoiding frauds. During the registration, every guests receive a personalized QR-code that displays their name upon checking. When they arrive, the security can easily distinguish unwanted guests from the real ones.
Implement barcode reading from the video stream of a USB camera in C#
namespace: Ozeki.Vision
With the help of barcode reader function you can scan and analyse barcodes from local image or from video stream as USB, RTSP, ONVIF cameras. The success of the barcode reading depends on the sharpness of the image, the distance between the barcode and the camera, the format and size of the barcode.
Properties:
- Rotate: Logical type, if true the reader rotate the image by 90, 180 or 270 degrees
- Invert: Logical type, if true the reader inverts the black and white colors
- DeeperAnalysis: Logical type, if true make a deeper look into the image
- Monochrome: Logical type, if true the image is only a monochrome barcode
Event:
- DetectionOccurred: this is an event which is called when a barcode is found. With the SnapShot which can be got from the parameter we can save the incoming image to our computer
How to read barcodes with an USB camera device using C#?
First, you need to declare and initialize an IBarcodeReader and an ImageProcesserHandler type variable.
IBarcodeReader _barcodeReader=ImageProcesserFactory.CreateBarcodeReader();
ImageProcesserHandler _imageProcesserHandler = new ImageProcesserHandler();
After this, we call the AddProcesser method of the _imageProcesserHandler with the help of the _barcodeReader parameter:
_imageProcesserHandler.AddProcesser(_barcodeReader);
The connection to the USB camera takes place in the following way:
_camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL);
_connector.Connect(_camera.VideoChannel, _imageProvider);
_camera.CameraStateChanged += _camera_CameraStateChanged;
_camera.Start();
After the successful connection we can start reading the barcode. With the help of the FrameCapture function we can determine that which frame from the video stream should be processed with the barcode reader so it is recommended to connect the frame capture to the _imageProcesserHandler:
_barcodeReader.DetectionOccurred += _barcodeReadercam_DetectionOccurred;
_connector.Connect(_camera.VideoChannel, _imageProcesserHandler);
After the detection, from the DetectedBarcode (which we got from the _barcodeReaderUSBcam_DetectionOccured method) we can get the format and the content of the detected barcode:
void _barcodeReadercam_DetectionOccurred(object sender, BarcodeDetectedEventArgs e)
{
InvokeGuiThread(() =>
{
if (e == null)
return;
foreach (var barcode in _barcodesList)
{
if (e.DetectedBarcode.Equals(barcode))
return;
}
_counter++;
_barcodesList.Add(e.DetectedBarcode);
textBoxBarcodes.Text += String.Format("{0}, Date: {1}, Barcode format: {2}, Content: {3}",
_counter, DateTime.Now, e.DetectedBarcode.BarcodeFormat, e.DetectedBarcode.Content) + Environment.NewLine;
});
}
Supported BarcodeFormats:
1D: CODABAR, CODE 39, CODE 93, CODE 128, EAN 8, EAN 13, ITF, MSI, PLESSEY, RSS 14, RSS EXPANDED, UPC-A, UPC-E,
2D: AZTEC, DATA MATRIX, MAXI CODE, PDF417, QR CODE
C# code example for barcode reading from an USB camera
Windows Form |
Windows forms version
MainForm.cs
using System; using System.Windows.Forms; using System.Collections.Generic; using Ozeki.Camera; using Ozeki.Media; using Ozeki.Vision; namespace BarcodeDetection_WF { public partial class MainForm : Form { CameraURLBuilderWF _myCameraUrlBuilder; private IBarcodeReader _barcodeReader; private OzekiCamera _camera; private DrawingImageProvider _imageProvider; private MediaConnector _connector; private ImageProcesserHandler _imageProcesserHandler; private FrameCapture _frameCapture; private List<DetectedBarcode> _barcodesList; private int _counter; public MainForm() { InitializeComponent(); } void BarcodeForm_Load(object sender, EventArgs e) { _imageProvider = new DrawingImageProvider(); _connector = new MediaConnector(); _imageProcesserHandler = new ImageProcesserHandler(); _frameCapture = new FrameCapture(); _barcodeReader = ImageProcesserFactory.CreateBarcodeReader(); _barcodesList = new List<DetectedBarcode>(); videoViewer.SetImageProvider(_imageProvider); _myCameraUrlBuilder=new CameraURLBuilderWF(); } private void buttonConnect_Click(object sender, EventArgs e) { if (_camera != null) { _camera.CameraStateChanged -= _camera_CameraStateChanged; _camera.Disconnect(); _connector.Disconnect(_camera.VideoChannel, _imageProvider); _camera.Dispose(); _camera = null; } _camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL); _camera.CameraStateChanged += _camera_CameraStateChanged; buttonConnect.Enabled = false; _connector.Connect(_camera.VideoChannel, _imageProvider); videoViewer.SetImageProvider(_imageProvider); _barcodeReader.DetectionOccurred += _barcodeReadercam_DetectionOccurred; _imageProcesserHandler.AddProcesser(_barcodeReader); _connector.Connect(_camera.VideoChannel, _imageProcesserHandler); _imageProcesserHandler.Start(); videoViewer.Start(); _camera.Start(); } private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { InvokeGuiThread(() => { if (e.State == CameraState.Connecting) buttonConnect.Enabled = false; if (e.State == CameraState.Streaming) buttonDisconnect.Enabled = true; if (e.State == CameraState.Disconnected) { buttonDisconnect.Enabled = false; buttonConnect.Enabled = true; } }); } void buttonDisconnect_Click(object sender, EventArgs e) { if (_camera == null) return; _connector.Disconnect(_camera.VideoChannel, _imageProvider); _camera.Disconnect(); _camera = null; } void _barcodeReadercam_DetectionOccurred(object sender, BarcodeDetectedEventArgs e) { InvokeGuiThread(() => { if (e == null) return; foreach (var barcode in _barcodesList) { if (e.DetectedBarcode.Equals(barcode)) return; } _counter++; _barcodesList.Add(e.DetectedBarcode); textBoxBarcodes.Text += String.Format("{0}, Date: {1}, Barcode format: {2}, Content: {3}", _counter, DateTime.Now, e.DetectedBarcode.BarcodeFormat, e.DetectedBarcode.Content) + Environment.NewLine; }); } void InvokeGuiThread(Action action) { BeginInvoke(action); } private void button_Compose_Click(object sender, EventArgs e) { var result = _myCameraUrlBuilder.ShowDialog(); if (result != DialogResult.OK) return; tb_cameraUrl.Text = _myCameraUrlBuilder.CameraURL; buttonConnect.Enabled = true; } } }
GUI
Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.
MainForm.Designer.cs
namespace BarcodeDetection_WF { partial class MainForm { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.groupBoxUSBCamera = new System.Windows.Forms.GroupBox(); this.videoViewer = new Ozeki.Media.VideoViewerWF(); this.textBoxBarcodes = new System.Windows.Forms.TextBox(); this.buttonConnect = new System.Windows.Forms.Button(); this.buttonDisconnect = new System.Windows.Forms.Button(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.tb_cameraUrl = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.button_Compose = new System.Windows.Forms.Button(); this.groupBoxUSBCamera.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.SuspendLayout(); this.groupBoxUSBCamera.Controls.Add(this.videoViewer); this.groupBoxUSBCamera.Location = new System.Drawing.Point(12, 92); this.groupBoxUSBCamera.Name = "groupBoxUSBCamera"; this.groupBoxUSBCamera.Size = new System.Drawing.Size(423, 283); this.groupBoxUSBCamera.TabIndex = 34; this.groupBoxUSBCamera.TabStop = false; this.groupBoxUSBCamera.Text = "Camera Image"; this.videoViewer.BackColor = System.Drawing.Color.Black; this.videoViewer.FlipMode = Ozeki.Media.FlipMode.None; this.videoViewer.ForeColor = System.Drawing.Color.Black; this.videoViewer.FrameStretch = Ozeki.Media.FrameStretch.Uniform; this.videoViewer.FullScreenEnabled = true; this.videoViewer.Location = new System.Drawing.Point(9, 19); this.videoViewer.Name = "videoViewer"; this.videoViewer.RotateAngle = 0; this.videoViewer.Size = new System.Drawing.Size(399, 258); this.videoViewer.TabIndex = 0; this.videoViewer.Text = "videoViewerWF3"; this.textBoxBarcodes.Location = new System.Drawing.Point(9, 19); this.textBoxBarcodes.Multiline = true; this.textBoxBarcodes.Name = "textBoxBarcodes"; this.textBoxBarcodes.Size = new System.Drawing.Size(399, 128); this.textBoxBarcodes.TabIndex = 30; this.buttonConnect.Enabled = false; this.buttonConnect.Location = new System.Drawing.Point(83, 39); this.buttonConnect.Name = "buttonConnect"; this.buttonConnect.Size = new System.Drawing.Size(86, 23); this.buttonConnect.TabIndex = 28; this.buttonConnect.Text = "Connect"; this.buttonConnect.UseVisualStyleBackColor = true; this.buttonConnect.Click += new System.EventHandler(this.buttonConnect_Click); this.buttonDisconnect.Enabled = false; this.buttonDisconnect.Location = new System.Drawing.Point(230, 39); this.buttonDisconnect.Name = "buttonDisconnect"; this.buttonDisconnect.Size = new System.Drawing.Size(86, 23); this.buttonDisconnect.TabIndex = 29; this.buttonDisconnect.Text = "Disconnect"; this.buttonDisconnect.UseVisualStyleBackColor = true; this.buttonDisconnect.Click += new System.EventHandler(this.buttonDisconnect_Click); this.groupBox3.Controls.Add(this.textBoxBarcodes); this.groupBox3.Location = new System.Drawing.Point(12, 381); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(422, 167); this.groupBox3.TabIndex = 35; this.groupBox3.TabStop = false; this.groupBox3.Text = "Details of barcodes"; this.groupBox4.Controls.Add(this.tb_cameraUrl); this.groupBox4.Controls.Add(this.label1); this.groupBox4.Controls.Add(this.buttonDisconnect); this.groupBox4.Controls.Add(this.buttonConnect); this.groupBox4.Controls.Add(this.button_Compose); this.groupBox4.Location = new System.Drawing.Point(12, 12); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(423, 74); this.groupBox4.TabIndex = 36; this.groupBox4.TabStop = false; this.groupBox4.Text = "Connect"; this.tb_cameraUrl.Location = new System.Drawing.Point(83, 13); this.tb_cameraUrl.Name = "tb_cameraUrl"; this.tb_cameraUrl.ReadOnly = true; this.tb_cameraUrl.Size = new System.Drawing.Size(233, 20); this.tb_cameraUrl.TabIndex = 26; this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(6, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(71, 13); this.label1.TabIndex = 25; this.label1.Text = "Camera URL:"; this.button_Compose.Location = new System.Drawing.Point(333, 11); this.button_Compose.Name = "button_Compose"; this.button_Compose.Size = new System.Drawing.Size(75, 23); this.button_Compose.TabIndex = 24; this.button_Compose.Text = "Compose"; this.button_Compose.UseVisualStyleBackColor = true; this.button_Compose.Click += new System.EventHandler(this.button_Compose_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(444, 556); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBoxUSBCamera); this.MaximizeBox = false; this.Name = "MainForm"; this.Text = "Barcode Reader"; this.Load += new System.EventHandler(this.BarcodeForm_Load); this.groupBoxUSBCamera.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); this.groupBox4.ResumeLayout(false); this.groupBox4.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBoxUSBCamera; private Ozeki.Media.VideoViewerWF videoViewer; private System.Windows.Forms.TextBox textBoxBarcodes; private System.Windows.Forms.Button buttonConnect; private System.Windows.Forms.Button buttonDisconnect; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.TextBox tb_cameraUrl; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button_Compose; } }
Note: This application has a lot in common with our IP camera barcode scanner application. If your needs require multiple viewers at a time or a greater distance between the camera and the viewscreen, we recommend you using an IP camera.
Conclusion
On this webpage you can find all the needed information about barcode scanning in C#. If you follow these article you will be able to successfully implement barcode reading from the stream of an USB camera in C#.
Related pages
- How to download Ozeki Camera SDK
- How to implement simple decision making in C#
- How to implement decision trees in C#
- How to implement Optical Character Recognition in C#
FAQ
Below you can find the answers for the most frequently asked questions related to this topic:
-
What is the smallest barcode size that can be detected?
It depends on the distance between the scanner and the barcode and on the sharpness of the image which is taken by the camera device.
-
What platforms are the barcode scanner of the Ozeki Camera SDK available for?
The barcode scanner function is available for all types of Windows.
-
I cannot manage to connect the barcode scanner to the FrameCapture. Why?
First, the frame capture should be connected to the camera. After this the frame capture should be connected to the barcode reader. Finally, the frame capture also needed to be started.
More information
- How to implement barcode reading from an IP cam in C#
- How to implement barcode scanning from the stream of an USB camera in C#
- How to accomplish decision trees in C#
- How to implement license plate recognition in C#
- How to implement simple decision making in C#
- How to implement Optical Character Recognition in C#