Baumer industrial camera Baumer camera how to use BGAPI SDK on Winform platform to realize the correct release of camera resources (C#)



#Qinwrite Pacesetter Challenge#

Baumer industrial camera

Baumer Industrial Cameras Baumer cameras are high-performance, high-quality industrial cameras that can be used in various application scenarios, such as object detection, counting and recognition, motion analysis and image processing.

Baumer's 10 Gigabit cameras have excellent image processing performance and can transmit high-resolution images in real time. In addition, the camera features fast data transfer, low power consumption, easy integration, and high scalability.

The BGAPI SDK of Baumer industrial cameras generally needs to release the camera resources after using the camera. The release steps are more important. This article introduces how to use the BGAPI SDK correctly to release the camera resources under the C# platform.

Baumer industrial camera SDK technical background

The BGAPI SDK of Baumer industrial cameras can provide the original image data of the camera. After the integrated camera and vision software are used, the SDK needs to be used at the same time to release the camera resources occupied by the SDK when the vision software is closed.

The SDK (Software Development Kit) of industrial cameras is a set of software tools provided to facilitate developers to control and image capture industrial cameras. Halcon is a powerful machine vision software capable of image processing, analysis, recognition and many other tasks.

Here mainly describes how to implement the core code of the corresponding resource release function under the C# platform

code analysis

This article introduces the function of using the SDK to release camera resources when using the BGAPI SDK to develop Baumer industrial cameras.

Release all camera resources

Release of camera resources. When the software is closed and the program exits, the created resources need to be closed and released one by one. The resources are closed.
The order of release is just the opposite of the order of creation;

The code to release all camera resources in the C# environment is as follows:

private void buttonExit_Click(object sender, EventArgs e)
{
    if (pDevice != null)
    {
        try
        {
        	//Execute camera off command
            if (pDevice.RemoteNodeList.GetNodePresent("AcquisitionAbort"))
                pDevice.RemoteNodeList["AcquisitionAbort"].Execute();
            pDevice.RemoteNodeList["AcquisitionStop"].Execute();
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            string str19;
            str19 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
            MessageBox.Show(str19);
        }
    }

    if (pDataStream != null & pBufferList != null)
    {
        try
        {
            pDataStream.StopAcquisition();
            pBufferList.DiscardAllBuffers();
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            string str20;
            str20 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
            MessageBox.Show(str20);
        }
    }

    if (pDataStream != null)
    {
        try
        {
        	//unregister event
            pDataStream.UnregisterNewBufferEvent();
            pDataStream.RegisterNewBufferEvent(BGAPI2.Events.EventMode.UNREGISTERED);
            BGAPI2.Events.EventMode currentEventMode = pDataStream.EventMode;
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            string str21;
            str21 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
            MessageBox.Show(str21);
        }
    }

    if (pBufferList != null)
    {
        try
        {
            while (pBufferList.Count > 0)
            {
                pBuffer = pBufferList.Values.First();
                pBufferList.RevokeBuffer(pBuffer);
            }
            //Turn off the camera's data stream
            pDataStream.Close();
            //device that turns off the camera
            pDevice.Close();
            //Close the network port connected to the camera
            pInterface.Close();
            //Instance class for closing camera connection
            pSystem.Close();
            //Release the resources of the camera connection class
            this.Close();
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            string str22;
            str22 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
            MessageBox.Show(str22);
        }
    }
            
}

Points to note when releasing camera resources

To release the camera resource code in the C# environment, you need to pay special attention to the stop command of the camera data stream, which is especially important;
Sometimes after the stop command is sent, the camera data flow is not stopped, and the subsequent release of the camera class directly will cause an error to occur.

 if (pDataStream != null & pBufferList != null)
    {
        try
        {
            pDataStream.StopAcquisition();
            pBufferList.DiscardAllBuffers();
        }
        catch (BGAPI2.Exceptions.IException ex)
        {
            string str20;
            str20 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
            MessageBox.Show(str20);
        }
    }

Advantages of industrial camera SDK to release the resources of industrial cameras

  1. Increased flexibility: By using the SDK, developers can customize and adjust camera functions to better suit their specific application requirements.

  2. Improved Integration: The SDK allows for seamless integration of multiple cameras and camera components with other hardware and software systems.

  3. Greater Control: Using the Industrial Camera SDK provides deeper, more direct access to camera functions and data, allowing greater control over captured images and video.

  4. Enhanced performance: Using the SDK can improve camera performance and speed up data processing, resulting in higher quality images and videos.

  5. Reduced costs: The SDK allows greater flexibility in selecting and customizing camera systems, potentially resulting in cost savings in the long run.

The importance of industrial camera SDK to release the resources of industrial cameras

Proper release of industrial camera resources in software is important because it ensures that the camera and its associated hardware are not left in an inconsistent or unstable state, which can lead to issues such as memory leaks, performance degradation, and system crashes.

Following the steps in the SDK (Software Development Kit) will ensure that all components used by the camera are properly shut down and any memory or other system resources are properly released.

This can help ensure that the camera and its associated components are functioning properly, and that the software application is stable and reliable over the long term.

Tags: image processing C# OpenCV Computer Vision machine vision programming language

Posted by djjjozsi on Mon, 03 Apr 2023 06:12:25 +0930