Baumer Industrial Camera How to use BGAPISDK to use image callback functions through two different methods (C#)

How Baumer Industrial Cameras use BGAPISDK to use image callback functions in two different ways (C#)

  • Baumer industrial cameras
  • Technical background to the fixed frame rate functionality of Baumer industrial cameras
    • Method 1: Use the function in the BGAPI SDK to complete the image data conversion in the image callback function
    • Method 2: Use Bitmap image to complete image data conversion in Baumer image callback function
  • Advantages of Baumer industrial cameras calling image callback functions
  • Industrial application of Baumer industrial camera calling image callback function

?

Baumer Industrial Camera

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

Baumer’s 10GbE 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 image callback function of the Baumer industrial camera is a way to call back (send) the image data and related information to the specified function by registering a callback function after the camera completes shooting. This callback function is usually a static function that cannot be directly associated with the thread class, but can access the member variables of the thread class or perform specific tasks by passing a pointer to the current thread class.

In addition, the image callback function of the industrial camera can also be used to process and operate image data. For example, you can preprocess, analyze, and calculate images in the callback function, and then return the processed image data to the main program or other threads for processing.

It should be noted that when using the image callback function of an industrial camera, you need to ensure that the camera shooting and callback function registration are configured correctly. At the same time, it is also necessary to ensure that the definition and calling of callback functions comply with specifications and requirements to ensure the correctness and stability of the program.

The Camera Explorer software of Baumer industrial cameras is powerful and rich in content. Through this software, you can effectively obtain all information related to the camera. In the development of camera inspection projects, it is sometimes necessary to obtain the image Buffer information and related data flow information in the camera. The Camera Explorer software can effectively display relevant information.

Technical background of fixed frame rate function of Baumer industrial cameras

The image callback function of industrial cameras is an important technology in industrial camera image processing. After the shooting is completed, the picture data and related information are called back (sent) to the specified function for processing by registering a callback function. This technology was originally developed by the German company MVtec and is used to process and manipulate image data captured by cameras to achieve high-precision image recognition, analysis and processing functions.

With the development of industrial automation and intelligence, industrial cameras are used more and more widely, and the requirements for image processing are also getting higher and higher. Therefore, image callback function technology has been widely used and promoted. It can not only achieve high-precision image processing and analysis, but also improve the efficiency and accuracy of industrial automation production, providing strong technical support for the development of industrial automation and intelligence.

In image callback function technology, multiple programming languages and technologies need to be used, such as C++, C#, Qt, etc. Among them, Qt is a cross-platform C++ graphical user interface application development framework. It provides rich GUI components and network functions and is widely used in image processing and callback function development of industrial cameras.

In addition, image callback function technology also requires the use of related software and tools such as camera drivers and image processing libraries. The camera driver is a software program used to control the camera hardware, which can realize functions such as camera control and data collection. Image processing library is a software library used to process and analyze image data, such as OpenCV, Halcon, etc.

In short, image callback function technology is an important technology in industrial camera image processing. Its development and application provide strong technical support for the development of industrial automation and intelligence.

Method 1: Use the functions in the BGAPI SDK to complete image data conversion in the image callback function

Register the image callback function after the camera is connected. The C# calling code is as follows:

void DataStream_NewBufferEvent2(object sender, NewBufferEventArgs bufferEvent)
        {<!-- -->
            try
            {<!-- -->
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                BGAPI2.Buffer bufferFilled = null;

                bufferFilled = bufferEvent.BufferObj;
                if (bufferFilled == null)
                {<!-- -->
                    //Console.WriteLine($"DataStream_NewBufferEvent(Error): Buffer Timeout after 1000 ms!");
                    //logger.LogMessage("BaumerCameraAPI", LogMessageType.Error, $"DataStream_NewBufferEvent(Error): Buffer Timeout after 1000 ms!");
                }
                else if (bufferFilled.IsIncomplete == true)
                {<!-- -->
                    bufferFilled.QueueBuffer();
                }
                else
                {<!-- -->

                    #region//Get the current FrameID
                    FrameIDInt = (int)bufferFilled.FrameID;
                    OnNotifySetFrameID(FrameIDInt.ToString());
                    #endregion

                    imagebuffer = new IntPtr();

                    #region//Get the current FrameID
                    //int FrameIDInt = (int)bufferFilled.FrameID;
                    #endregion

                    #region//Get the current image type
                    string pixelFormatstr = bufferFilled.PixelFormat;
                    if (pixelFormatstr.Contains("Mono8"))
                    {<!-- -->
                    }
                    if (pixelFormatstr.Contains("BRGB"))
                    {<!-- -->
                    }
                    #endregion
                    //Set the width and height of the image
                    BGAPI2.Image p_image = pImgProcessor.CreateImage((uint)pDevice.RemoteNodeList["Width"].Value, (uint)pDevice.RemoteNodeList["Height"].Value, bufferFilled.PixelFormat, bufferFilled.MemPtr, bufferFilled.MemSize);
                    BGAPI2.Image tranImage = null;
                    tranImage = pImgProcessor.CreateTransformedImage(p_image, "Mono8");
                    int w = 0;
                    int h = 0;
                    w = (int)tranImage.Width;
                    h = (int)tranImage.Height;
                    imagebuffer = tranImage.Buffer;

                    if(firstFrame)
                    {<!-- -->
                        pImgBits = new Byte[w * h];
                        PubBitmap = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
                        prcSource.X = 0;
                        prcSource.Y = 0;
                        prcSource.Width = w;
                        prcSource.Height = h;
                        firstFrame = false;
                    }

                    BitmapData bmpdata;
                    ColorPalette palette = PubBitmap.Palette;
                    for (int i = 0; i < 256; i + + )
                    {<!-- -->
                        palette.Entries[i] = Color.FromArgb(255, i, i, i);
                    }
                    PubBitmap.Palette = palette;

                    bmpdata = PubBitmap.LockBits(prcSource, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                    System.Runtime.InteropServices.Marshal.Copy(imagebuffer, pImgBits, 0, w * h);
                    System.Runtime.InteropServices.Marshal.Copy(pImgBits, 0, bmpdata.Scan0, w * h);
                    PubBitmap.UnlockBits(bmpdata);

                    PubBitmapCur = PubBitmap;

                    System.Drawing.Graphics graph = System.Drawing.Graphics.FromHwnd(pictureBoxA.Handle);
                    graph.DrawImage(PubBitmapCur, prcPBox, prcSource, GraphicsUnit.Pixel);


                    bufferFilled.QueueBuffer();
                }
            }
            catch (BGAPI2.Exceptions.IException ex)
            {<!-- -->
               // var errorMessage = $"DataStream_NewBufferEvent:Error in function: {ex.GetFunctionName()}\r\
Error description: {ex.GetErrorDescription()}";
                //System.Console.WriteLine(errorMessage);
                //logger.LogMessage(errorMessage, "DataStream_NewBufferEvent", LogMessageType.Error);
                //logger.LogException(ex, "DataStream_NewBufferEvent");
            }
            catch (Exception ex)
            {<!-- -->
                //logger.LogException(ex, "DataStream_NewBufferEvent");
               // Console.WriteLine($"DataStream_NewBufferEvent: {ex.Message}");
            }
            return;
        }

Method 2: Use Bitmap image to complete image data conversion in Baumer image callback function

Register the image callback function after the camera is connected. The C# calling code is as follows:

 void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
    {<!-- -->
        try
        {<!-- -->
            BGAPI2.Buffer mBufferFilled = null;
            mBufferFilled = mDSEvent.BufferObj;
            if (mBufferFilled == null)
            {<!-- -->
                MessageBox.Show("Error: Buffer Timeout after 1000 ms!");
            }
            else if (mBufferFilled.IsIncomplete == true)
            {<!-- -->
                //MessageBox.Show("Error: Image is incomplete!");
                //queue buffer again
                mBufferFilled.QueueBuffer();
            }
            else
            {<!-- -->
                #region//Get the current FrameID
                FrameIDInt = (int)mBufferFilled.FrameID;
                OnNotifySetFrameID(FrameIDInt.ToString());
                #endregion

                //Convert the camera's internal image memory data to bitmap data
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height, (int)mBufferFilled.Width,
                    System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));
                                      
                #region//Mono image data conversion. Color image data conversion is different from this
                System.Drawing.Imaging.ColorPalette palette = bitmap.Palette;
                int nColors = 256;
                for (int ix = 0; ix < nColors; ix + + )
                {<!-- -->
                    uint Alpha = 0xFF;
                    uint Intensity = (uint)(ix * 0xFF / (nColors - 1));
                    palette.Entries[ix] = System.Drawing.Color.FromArgb((int)Alpha, (int)Intensity, (int)Intensity, (int)Intensity);
                }
                bitmap.Palette = palette;
                #endregion


                //Callback function to save image function
                if (bSaveImg)
                {<!-- -->
                    //Use bitmap's own function to save
                    string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    string saveimagepath = pImgFileDir + "" + strtime + ".jpg";
                    bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);
                    bSaveImg = false;//Variable controls a single save of the image
                }

                #region//bitmap image data copy pBitmap
                Bitmap clonebitmap = (Bitmap)bitmap.Clone();
                BitmapData data = clonebitmap.LockBits(new Rectangle(0, 0, clonebitmap.Width, clonebitmap.Height), ImageLockMode.ReadOnly, clonebitmap.PixelFormat);
                clonebitmap.UnlockBits(data);
                pBitmap = clonebitmap;
                #endregion
                #region//Display pBitmap image data on the UI interface PictureBox control
                prcSource.X = 0;prcSource.Y = 0;
                prcSource.Width = (int)mBufferFilled.Width; prcSource.Height = (int)mBufferFilled.Height;
                System.Drawing.Graphics graph = System.Drawing.Graphics.FromHwnd(pictureBoxA.Handle);
                graph.DrawImage(pBitmap, prcPBox, prcSource, GraphicsUnit.Pixel);
                #endregion

                //clonebitmap.Dispose(); //Clear the memory space occupied by the temporary variable clonebitmap
                mBufferFilled.QueueBuffer();

            }
        }
        catch (BGAPI2.Exceptions.IException ex)
        {<!-- -->
            {<!-- -->
                string str2;
                str2 = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
                MessageBox.Show(str2);
            }
        }
        return;
    }


Advantages of Baumer industrial cameras calling image callback functions

The advantages of Baumer industrial cameras calling image callback functions mainly include:

1. Real-time: The image callback function can return the image data immediately after the shooting is completed, making the processing process real-time, so that the image can be analyzed and processed in a timely manner.

2. Efficiency: Through the image callback function, image data can be returned directly to the main program or thread for processing, avoiding unnecessary data copying and transmission, and improving processing efficiency.

3. Flexibility: The image callback function can flexibly handle various types of image data, including color and grayscale images, standard formats and custom formats, etc. At the same time, image processing algorithms can be flexibly designed and implemented according to different needs and scenarios.

4. Scalability: The image callback function can be expanded and customized by registering the callback function, allowing customized development according to specific needs and scenarios, improving the scalability and maintainability of the system.

5. Reliability: The image callback function can provide stable and reliable image data output, reducing errors and losses caused by errors and abnormalities during data transmission and processing.

In short, industrial cameras calling image callback functions can bring advantages such as real-time performance, efficiency, flexibility, scalability and reliability, and provide strong technical support for the development of industrial automation and intelligence.

Industrial application of Baumer industrial camera calling image callback function

Industrial cameras have a wide range of applications for calling image callback functions, and are mainly used in the following industries:

1. Automobile manufacturing: In the automobile manufacturing process, industrial cameras can be used to take images of parts, and return the captured image data to the main program through the image callback function for image processing, size measurement, defect detection and other tasks, thereby improving Production efficiency and quality.

2. Electronic manufacturing: In the field of electronic manufacturing, industrial cameras can be used to identify and detect the appearance and position of electronic components to ensure that they meet production requirements and quality standards. At the same time, industrial cameras can also be used to achieve tasks such as material handling and assembly on automated production lines.

3. Food processing: In the food processing industry, industrial cameras can be used to take images of food, and return the captured image data to the main program through the image callback function for image processing, quality detection and classification tasks, thereby improving the production process. efficiency and quality.

4. Logistics and warehousing: In the field of logistics and warehousing, industrial cameras can be used to take images of goods, and return the captured image data to the main program through the image callback function for image processing, identification and tracking tasks, thereby realizing automated warehouse management and Intelligent logistics and distribution and other goals.

5. Medical imaging: In the field of medical imaging, industrial cameras can be used to shoot medical images, such as X-rays, CT scans and MRI, etc., and return the captured image data to the main program through the image callback function for image processing, analysis and Diagnosis and other tasks, thereby improving the accuracy and efficiency of medical diagnosis.

In short, the application of industrial cameras calling image callback functions is very wide, involving many fields such as manufacturing, logistics, and medical industries. With the development of industrial automation and intelligence, its application prospects will become increasingly broad.