How to implement barcode reading from the video stream of an IP camera in C#

This guide provides you all the necessary information about barcode reading. This function can be used for supervising or identifying your tools, equipments and products. After studying the content below you will know how to manage barcode reading from the video stream of an IP camera with the help of your C# camera application. To be successful, 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 set up 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 the 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:\Program Files\Ozeki\Ozeki SDK\examples\04_ComputerVision\07_BarcodeDetection

To compile this example you will need Microsoft Visual Studio installed on your computer.

Corporate use of barcode reading

The barcode scanner function can be a really great help in corporate environments. If you have a storage shack and you would like to follow the expensive equipment of yours the barcode scanner can be a good solution for you. Also, if you wish to number the items in your store you can use the barcode scanner function for numbering. Furthermore, if you are a shop owner and you do the supervision of your stock and the prices on your own, the barcode scanning function of the Ozeki Camera SDK is the ideal help for you. The IP camera barcode detection is a really useful feaure because you can use an IP camera to be your barcode detector and all of your workstations can trace your storage together using the Onvif technology this way minimizing the need of over-communication when it comes to someone taking up a unique item or machinery.

barcode scanning in c#
Figure 1 - Barcode scanning

On the first illustration you can find an example for barcode reading and the way it helps you supervising, detecting and identifying the items and tools which can be found in limitless amounts in your warehouse, shop, etc..

qr code detecting in c#
Figure 2 - QR code detecting

On the second picture you will find the illustration of the QR code detecting function. The usage of QR code is extremely popular in industrial environments, in logistics and in production since it is an effective medium for storing data. QR code is a really convinient way of informing yourself about products or programs through your mobile phone. By using the QR code detecting function of the Ozeki Camera SDK you can easily benefit from the advantages of it. It is possible to turn your IP camera to a QR-code scanner thereby making it possible for your workers to check in by showing their QR-code-containing cards to the camera. After receiving the code, the system can register the entrance and record it to a database. When the employee leaves he just have to present the card again for a second record. Working hours can be calculated by substracting the two times from each other. IP cameras make it possible to do this on all work stations and company bases using only one PC.

We can mention another fields where the barcode scanning function can be used efficiently: inspection of packages which are sent by an express delivery company and packaging of medical products (the packaging of medical products is a field where no mistake is tolerated so the security is extremely important).

Implement barcode reading from the video stream of an IP camera in C#

namespace: Ozeki.Vision

With the help of the barcode reader function you can scan and analyse barcodes from local images or from video streams 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 rotates the image for 90, 180 or 270 degrees
  • Invert: logical type, if true the reader inverts the black and white colors
  • DeeperAnalysis: logical type, if true it makes 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 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 IP 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 IP 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 the barcode reading. 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 _barcodeReaderIPcam_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:

One advantage of the barcode scanning function of the Ozeki Camera SDK is that it supports a lot of types of BarcodeFormat. Below you will find the list of the supported BarcodeFormats which can be detected with the help of the Ozeki Camera SDK.

1D barcode formats: CODABAR, CODE 39, CODE 93, CODE 128, EAN 8, EAN 13, ITF, MSI, PLESSEY, RSS 14, RSS EXPANDED, UPC-A, UPC-E,

2D barcode formats: AZTEC, DATA MATRIX, MAXI CODE, PDF417, QR CODE

C# code example for barcode reading from an IP 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 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();
            _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;
        }
    }
}

		

Code 1 - Implement barcode reading from the video stream of an IP camera in C#

GUI

gui of an application for barcode scanning/qr code detecting from the video stream of an ip camera in c#
Figure 3 - The graphical user interface of your application

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;
    }
}


		

Code 2 - GUI example in C#

Conclusion

In this guide you can discover how to manage barcode reading from the video stream of an IP camera. Barcode and QR detection is a very efficient way of supervising your product. With the help of this function you can enjoy all the advantages that can offer.

Related pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. 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.

  2. What platform support the barcode scanner of the Ozeki Camera SDK?

    The barcode scanner function is available for all types of Windows.

  3. 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