コード例 #1
0
 private void OnImageGrabbed_1(Object sender, ImageGrabbedEventArgs e)
 {
     try
     {
         IGrabResult grabResult = e.GrabResult;
         if (grabResult.IsValid)
         {
             // 四通道RGBA
             Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
             // 锁定位图的位
             BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
             // 将指针放置到位图的缓冲区
             converter.OutputPixelFormat = PixelType.BGRA8packed;
             IntPtr ptrBmp = bmpData.Scan0;
             converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult);
             bitmap.UnlockBits(bmpData);
             DeepLearning deepLearning = new DeepLearning();
             if (isInference1)
             {
                 bitmap = deepLearning.Inference(model1, bitmap);
             }
             if (pictureBox1.InvokeRequired)  // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它
             {
                 UpdateUI update = delegate { pictureBox1.Image = bitmap; };
                 pictureBox1.BeginInvoke(update);
             }
             else
             {
                 pictureBox1.Image = bitmap;
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(string.Format("CAM:{0}!\n{1}", "CAM1", ex.Message));
     }
     finally
     {
         e.DisposeGrabResultIfClone();
     }
 }
コード例 #2
0
ファイル: SKPylon5NetToMat.cs プロジェクト: Kenkaik/OCR
        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            Mat mat = null;
            //try
            //{
            IGrabResult grabResult = e.GrabResult;

            if (grabResult.IsValid)
            {
                mat = new Mat(new Size(grabResult.Width, grabResult.Height), Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                mat.SetTo <byte>((byte[])grabResult.PixelData);

                lock (Lock)
                {
                    if (GrabImage != null)
                    {
                        GrabImage.Dispose();
                    }
                    GrabImage = mat;
                    GrabEvent.Set();
                }
            }
            if (!grabResult.GrabSucceeded)
            {
                MessageBox.Show("Camera disconnect", "PylonNet5GrayUMat", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Environment.Exit(Environment.ExitCode);
                return;
            }
            //}
            //catch (Exception ex)
            //{
            //     MessageBox.Show(ex.Message, "PylonNet5GrayUMat", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            //}
            //finally
            //{
            // Dispose the grab result if needed for returning it to the grab loop.
            //     e.DisposeGrabResultIfClone();
            //}
            GC.Collect();
        }
コード例 #3
0
        public void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            using (new TimeTicker("AAAA 显示消耗的时间"))
            {
                try
                {
                    // Get the grab result.
                    IGrabResult grabResult = e.GrabResult;
                    // Check if the image can be displayed.
                    if (grabResult.IsValid)
                    {
                        Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
                        // Lock the bits of the bitmap.
                        BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                        // Place the pointer to the buffer of the bitmap.
                        converter.OutputPixelFormat = PixelType.BGRA8packed;
                        IntPtr ptrBmp = bmpData.Scan0;
                        converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult);
                        bitmap.UnlockBits(bmpData);
                        Bitmap temp = (Bitmap)bitmap.Clone();
                        CallFunction(this.Name, temp);

                        //Thread.Sleep(15);
                        if (bitmap != null)
                        {
                            bitmap.Dispose();
                        }

                        GC.Collect();
                    }
                }
                catch (Exception exception)
                {
                }
            }
        }
コード例 #4
0
        /****************************************************/


        /****************  图像响应事件函数  ****************/


        // 相机取像回调函数.
        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            try
            {
                IGrabResult grabResult = e.GrabResult;
                if (grabResult.GrabSucceeded)
                {
                    grabTime = stopWatch.ElapsedMilliseconds;
                    {
                        if (latestFrameAddress == IntPtr.Zero)
                        {
                            latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                        }
                        converter.OutputPixelFormat = PixelType.Mono8;
                        converter.Convert(latestFrameAddress, grabResult.PayloadSize, grabResult);
                        // 转换为Halcon图像显示
                        HOperatorSet.GenImage1(out hPylonImage, "byte", (HTuple)grabResult.Width, (HTuple)grabResult.Height, (HTuple)latestFrameAddress);
                        // 抛出图像处理事件
                        eventProcessImage(hPylonImage);
                        hPylonImage.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("Grab faild!\n" + grabResult.ErrorDescription, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exception)
            {
                ShowException(exception);
            }
            finally
            {
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #5
0
        /****************************************************/


        /****************  图像响应事件函数  ****************/


        // 相机取像回调函数.
        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            try
            {
                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.

                // Get the grab result.
                IGrabResult grabResult = e.GrabResult;

                // Check if the image can be displayed.
                if (grabResult.GrabSucceeded)
                {
                    grabTime = stopWatch.ElapsedMilliseconds;
                    eventComputeGrabTime(grabTime);

                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    // ****  降低显示帧率,减少CPU占用率  **** //
                    //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)

                    {
                        //stopWatch.Restart();
                        // 判断是否是黑白图片格式
                        if (grabResult.PixelTypeValue == PixelType.Mono8)
                        {
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                            }
                            converter.OutputPixelFormat = PixelType.Mono8;
                            converter.Convert(latestFrameAddress, grabResult.PayloadSize, grabResult);

                            // 转换为Halcon图像显示
                            HOperatorSet.GenImage1(out hPylonImage, "byte", (HTuple)grabResult.Width, (HTuple)grabResult.Height, (HTuple)latestFrameAddress);
                        }
                        else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8 ||
                                 grabResult.PixelTypeValue == PixelType.BayerRG8 || grabResult.PixelTypeValue == PixelType.BayerGR8)
                        {
                            int imageWidth  = grabResult.Width - 1;
                            int imageHeight = grabResult.Height - 1;
                            int payloadSize = imageWidth * imageHeight;

                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * payloadSize));
                            }
                            converter.OutputPixelFormat = PixelType.BGR8packed;     // 根据bayer格式不同切换以下代码
                            //converter.OutputPixelFormat = PixelType.RGB8packed;
                            converter.Parameters[PLPixelDataConverter.InconvertibleEdgeHandling].SetValue("Clip");
                            converter.Convert(latestFrameAddress, 3 * payloadSize, grabResult);

                            HOperatorSet.GenImageInterleaved(out hPylonImage, latestFrameAddress, "bgr",
                                                             (HTuple)imageWidth, (HTuple)imageHeight, -1, "byte", (HTuple)imageWidth, (HTuple)imageHeight, 0, 0, -1, 0);
                        }

                        // 抛出图像处理事件
                        eventProcessImage(hPylonImage);
                        //hPylonImage.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("Grab faild!\n" + grabResult.ErrorDescription, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exception)
            {
                ShowException(exception);
            }
            finally
            {
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #6
0
 // This method is called if one or more images have been skipped during
 // image acquisition.
 static void OnImageSkipped(Object sender, ImageGrabbedEventArgs e)
 {
     Console.WriteLine("OnImageSkipped Event");
     Console.WriteLine("Number Of skipped images {0}", e.GrabResult.SkippedImageCount);
 }
コード例 #7
0
        private void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            try
            {
                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.

                // Get the grab result.
                IGrabResult grabResult = e.GrabResult;

                // Check if the image can be displayed.
                if (grabResult.GrabSucceeded)
                {
                    grabTime = stopWatch.ElapsedMilliseconds;
                    base.ProcessGrabTimeCallback(grabTime);

                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    // ****  降低显示帧率,减少CPU占用率  **** //
                    //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)

                    {
                        //stopWatch.Restart();
                        // 判断是否是黑白图片格式
                        if (grabResult.PixelTypeValue == PixelType.Mono8)
                        {
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                            }
                            converter.OutputPixelFormat = PixelType.Mono8;
                            converter.Convert(latestFrameAddress, grabResult.PayloadSize, grabResult);

                            // 转换为Halcon图像显示
                            Image.GenImage1("byte", grabResult.Width, grabResult.Height, latestFrameAddress);
                        }
                        else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8 ||
                                 grabResult.PixelTypeValue == PixelType.BayerRG8 || grabResult.PixelTypeValue == PixelType.BayerGR8)
                        {
                            int imageWidth  = grabResult.Width - 1;
                            int imageHeight = grabResult.Height - 1;
                            int payloadSize = imageWidth * imageHeight;

                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * payloadSize));
                            }
                            converter.OutputPixelFormat = PixelType.BGR8packed;     // 根据bayer格式不同切换以下代码
                            //converter.OutputPixelFormat = PixelType.RGB8packed;
                            converter.Parameters[PLPixelDataConverter.InconvertibleEdgeHandling].SetValue("Clip");
                            converter.Convert(latestFrameAddress, 3 * payloadSize, grabResult);

                            Image.GenImageInterleaved(latestFrameAddress, "bgr",
                                                      imageWidth, imageHeight, -1, "byte", imageWidth, imageHeight, 0, 0, -1, 0);
                        }
                        HImage newImage = Image.CopyImage();
                        image = newImage;
                        // 抛出图像处理事件
                        base.ProcessImageCallBack(newImage);

                        Image.Dispose();
                    }
                }
                else
                {
                    WriteErrorLog("Grab faild!\n" + grabResult.ErrorDescription + " of " + UserID);
                }
            }
            catch
            {
                bConnectOk = false;
            }
            finally
            {
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #8
0
 private void RaiseImageGrabbedEvent(object sender, ImageGrabbedEventArgs e) => ImageGrabbed?.Invoke(sender, e);
コード例 #9
0
 private void Cam_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
 {
     HWindow.DispObj(e.Image);
 }
コード例 #10
0
ファイル: BaslerCamera.cs プロジェクト: fb-jince/Jet_System
        private void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            try
            {
                if (ignoreImage)
                {
                    return;
                }


                //HOperatorSet.CountSeconds(out startTime);
                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.

                // Get the grab result.
                IGrabResult grabResult = e.GrabResult;

                // Check if the image can be displayed.
                if (grabResult.IsValid)
                {
                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
                    {
                        //stopWatch.Restart();
                        width  = grabResult.Width;
                        height = grabResult.Height;
                        //if (hPylonImage != null && hPylonImage.IsInitialized())
                        //{
                        //    hPylonImage.Dispose();
                        //}
                        hPylonImage = new Cognex.VisionPro.CogImage8Grey();
                        if (grabResult.PixelTypeValue == PixelType.Mono8)
                        {
                            // while (grabResult.GrabSucceeded == false) ;
                            if (grabResult.GrabSucceeded == false)
                            {/*
                              * Util.Notify(string.Format("相机{0}数据损坏,采集失败", cameraIndex));*/
                                Trace.WriteLine("数据损坏,采集失败" + width + "." + height);
                                return;
                            }
                            if (grabResult.PayloadSize == 0)
                            {/*
                              * Util.Notify(string.Format("相机{0}数据损坏,图像包大小为0", cameraIndex));*/
                                return;
                            }


                            //Util.Notify(string.Format("相机{0}数据尺寸{1}", cameraIndex, grabResult.PayloadSize));
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (m_latestFrameAddress == IntPtr.Zero)
                            {
                                m_latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                            }
                            g_converter.OutputPixelFormat = PixelType.Mono8;
                            g_converter.Convert(m_latestFrameAddress, grabResult.PayloadSize, grabResult);

                            byte[] ImagebufferPtr = new byte[grabResult.Width * grabResult.Height];

                            //转换为Halcon图像显示
                            //hPylonImage.Get8GreyPixelMemory( Cognex.VisionPro.CogImageDataModeConstants.Read,);
                            Marshal.Copy(m_latestFrameAddress, ImagebufferPtr, 0, grabResult.Width * grabResult.Height - 1);


                            var cogRoot = new Cognex.VisionPro.CogImage8Root();
                            cogRoot.Initialize(grabResult.Width, grabResult.Height, m_latestFrameAddress, grabResult.Width, null);
                            hPylonImage.SetRoot(cogRoot);
                            //hPylonImage.GenImage1("byte", grabResult.Width, grabResult.Height, m_latestFrameAddress);
                        }
                        else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8)
                        {
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (m_latestFrameAddress == IntPtr.Zero)
                            {
                                m_latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * g_camera.Parameters[PLCamera.PayloadSize].GetValue()));
                            }
                            g_converter.OutputPixelFormat = PixelType.BGR8packed;
                            g_converter.Convert(m_latestFrameAddress, 3 * grabResult.PayloadSize, grabResult);

                            /*hPylonImage.GenImageInterleaved(m_latestFrameAddress, "bgr",
                             *       grabResult.Width, grabResult.Height, -1, "byte", grabResult.Width, grabResult.Height, 0, 0, -1, 0);
                             */
                        }
                        else
                        {/*
                          * Util.Notify(string.Format("相机{0}编码格式不正确", cameraIndex));*/
                        }
                        Trace.WriteLine("采集成功" + width + "." + height);
                        TrigerImageEvent();
                    }
                }
            }
            catch (System.ArgumentException ex)
            {/*
              * Util.WriteLog(this.GetType(), ex);
              * Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));*/
            }
            catch (Exception ex)
            {/*
              * Util.WriteLog(this.GetType(), ex);
              * Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));*/
            }
            finally
            {
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #11
0
        private void                            StreamGrabber_ImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            if (_ngrabCount > 0)
            {
                _ncount++;

                if (_ncount >= _ngrabCount)
                {
                    Set_Stop();
                }
            }

            IGrabResult result = e.GrabResult;

            if (result.GrabSucceeded)
            {
                if (result.PixelTypeValue.IsMonoImage())
                {
                    var src  = result.PixelData as byte[];
                    var data = new byte[src.Length];
                    Array.Copy(src, data, src.Length);
                    if (ImageGrabbed != null)
                    {
                        ImageGrabbed(new tsInfo_Grab(EGRAB_RESULT.eSuccess, result.Width, result.Height, 1, data));
                    }

                    return;
                }
                else
                {
                    var data = new byte[result.Width * result.Height * 3];
                    _converter.Convert(data, result);

                    if (ImageGrabbed != null)
                    {
                        ImageGrabbed(new tsInfo_Grab(EGRAB_RESULT.eSuccess, result.Width, result.Height, 3, data));
                    }

                    return;
                }
            }

            if (ImageGrabbed != null)
            {
                ImageGrabbed(new tsInfo_Grab(EGRAB_RESULT.eError));
            }
        }
コード例 #12
0
ファイル: clsBasler.cs プロジェクト: JiaquanLi/AutoTech
        // Occurs when an image has been acquired and is ready to be processed.
        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            if (pbx_Image == null)
            {
            }
            else
            {
                if (pbx_Image.InvokeRequired)
                {
                    // If called from a different thread, we must use the Invoke method to marshal the call to the proper GUI thread.
                    // The grab result will be disposed after the event call. Clone the event arguments for marshaling to the GUI thread.
                    pbx_Image.BeginInvoke(new EventHandler <ImageGrabbedEventArgs>(OnImageGrabbed), sender, e.Clone());
                    return;
                }
            }

            try
            {
                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.

                // Get the grab result.
                IGrabResult grabResult = e.GrabResult;

                // Check if the image can be displayed.
                if (grabResult.IsValid)
                {
                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
                    {
                        stopWatch.Restart();

                        Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
                        // Lock the bits of the bitmap.
                        BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                        // Place the pointer to the buffer of the bitmap.
                        converter.OutputPixelFormat = PixelType.BGRA8packed;
                        IntPtr ptrBmp = bmpData.Scan0;
                        converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); //Exception handling TODO
                        bitmap.UnlockBits(bmpData);

                        // Assign a temporary variable to dispose the bitmap after assigning the new bitmap to the display control.
                        Bitmap bitmapOld = null;
                        // Provide the display control with the new bitmap. This action automatically updates the display.
                        if (pbx_Image != null)
                        {
                            bitmapOld       = pbx_Image.Image as Bitmap;
                            pbx_Image.Image = bitmap;
                        }
                        if (bSaveImgae)
                        {
                            bitmap.Save(@"image.bmp");
                        }
                        if (bitmapOld != null)
                        {
                            // Dispose the bitmap.
                            bitmapOld.Dispose();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ShowException(exception);
            }
            finally
            {
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #13
0
        private void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            try
            {
                if (ignoreImage)
                {
                    return;
                }
                if (Command == Command.Video || Command == Command.ExtTrigger)
                {
                    HOperatorSet.CountSeconds(out startTime);
                }
                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.

                // Get the grab result.
                IGrabResult grabResult = e.GrabResult;

                // Check if the image can be displayed.
                if (grabResult.IsValid)
                {
                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
                    {
                        //stopWatch.Restart();
                        width  = grabResult.Width;
                        height = grabResult.Height;
                        //if (hPylonImage != null && hPylonImage.IsInitialized())
                        //{
                        //    hPylonImage.Dispose();
                        //}
                        hPylonImage = new HImage();
                        if (grabResult.PixelTypeValue == PixelType.Mono8)
                        {
                            if (grabResult.GrabSucceeded == false)
                            {
                                Util.Notify(string.Format("相机{0}数据损坏,采集失败", cameraIndex));
                                return;
                            }
                            if (grabResult.PayloadSize == 0)
                            {
                                Util.Notify(string.Format("相机{0}数据损坏,图像包大小为0", cameraIndex));
                                return;
                            }


                            //Util.Notify(string.Format("相机{0}数据尺寸{1}", cameraIndex, grabResult.PayloadSize));
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (m_latestFrameAddress == IntPtr.Zero)
                            {
                                m_latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                            }
                            g_converter.OutputPixelFormat = PixelType.Mono8;
                            g_converter.Convert(m_latestFrameAddress, grabResult.PayloadSize, grabResult);

                            //转换为Halcon图像显示
                            hPylonImage.GenImage1("byte", grabResult.Width, grabResult.Height, m_latestFrameAddress);
                        }
                        else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8)
                        {
                            //allocate the m_stream_size amount of bytes in non-managed environment
                            if (m_latestFrameAddress == IntPtr.Zero)
                            {
                                m_latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * g_camera.Parameters[PLCamera.PayloadSize].GetValue()));
                            }
                            g_converter.OutputPixelFormat = PixelType.BGR8packed;
                            g_converter.Convert(m_latestFrameAddress, 3 * grabResult.PayloadSize, grabResult);
                            hPylonImage.GenImageInterleaved(m_latestFrameAddress, "bgr",
                                                            grabResult.Width, grabResult.Height, -1, "byte", grabResult.Width, grabResult.Height, 0, 0, -1, 0);
                        }
                        else
                        {
                            Util.Notify(Common.Basic.Level.Err, string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, grabResult.PixelTypeValue));
                        }
                        TrigerImageEvent();
                    }
                }
            }
            catch (System.ArgumentException ex)
            {
                Util.WriteLog(this.GetType(), ex);
                Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
            }
            catch (Exception ex)
            {
                Util.WriteLog(this.GetType(), ex);
                Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
            }
            finally
            {
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
            }
        }
コード例 #14
0
            /// <summary>
              ///  相机取像回调函数.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
        private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
        {
            try
            {

                // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed.
                // Get the grab result.
                if ( getcpu )
                {
                    if (sortnum == 0)
                    {
                    
                        GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x1));
                    }
                    else
                    {
                        GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x2));
                    }

                    Thread.CurrentThread.Priority = ThreadPriority.Highest;
                    getcpu = false;
                }
          
                IGrabResult grabResult =e.GrabResult;
                // Check if the image can be displayed.
                if (grabResult.GrabSucceeded)
                {
                        //stopWatch.Restart();
                        // 判断是否是黑白图片格式
                        if (grabResult.PixelTypeValue == PixelType.Mono8)
                        {
                            //allocate the m_stream_size amount of bytes in non-managed environment 
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                            }
                            converter.OutputPixelFormat = PixelType.Mono8;
                            converter.Convert(latestFrameAddress, grabResult.PayloadSize, grabResult);

                            // 转换为Halcon图像显示
                            HOperatorSet.GenImage1(out hPylonImage, "byte", (HTuple)grabResult.Width, (HTuple)grabResult.Height, (HTuple)latestFrameAddress);

                        }
                        else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8
                                    || grabResult.PixelTypeValue == PixelType.BayerRG8 || grabResult.PixelTypeValue == PixelType.BayerGR8)
                        {
                            int imageWidth = grabResult.Width - 1;
                            int imageHeight = grabResult.Height - 1;
                            int payloadSize = imageWidth * imageHeight;

                            //allocate the m_stream_size amount of bytes in non-managed environment 
                            if (latestFrameAddress == IntPtr.Zero)
                            {
                                latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * payloadSize));
                            }
                            converter.OutputPixelFormat = PixelType.BGR8packed;     // 根据bayer格式不同切换以下代码
                         
                            converter.Parameters[PLPixelDataConverter.InconvertibleEdgeHandling].SetValue("Clip");
                            converter.Convert(latestFrameAddress, 3 * payloadSize, grabResult);

                            HOperatorSet.GenImageInterleaved(out hPylonImage, latestFrameAddress, "bgr",
                                     (HTuple)imageWidth, (HTuple)imageHeight, -1, "byte", (HTuple)imageWidth, (HTuple)imageHeight, 0, 0, -1, 0);

                        }

                        // 抛出图像处理事件
                        if (hPylonImage == null)
                        {
                            MessageBox.Show("hPylonImage null");
                        }
                        //Console.WriteLine("CAMmm" + sortnum + "when--1--" + DateTime.Now.ToString("mm-ss-fff"));
                        poscmin = Turntable.Instance.copos[sortnum].pos.FirstOrDefault();
                      //  poscmin = Turntable.Instance.copos[sortnum].pos.First();
                        //Console.WriteLine("w" + sortnum + "ss" + poscmin);
                       Turntable.Instance.copos[sortnum].pos.Clear();
                        imagenum++;
                        //if (kkk > 1 && sortnum == 0)
                        //{
                        //    HOperatorSet.WriteImage(hPylonImage, "bmp", 0, @"C:\Users\mxw\Desktop\a2.bmp");
                        //    Console.WriteLine(sortnum+1);

                        ////}
                        HObject mimage = new HObject();
                        HOperatorSet.GenEmptyObj(out mimage);
                        mimage.Dispose();
                       // TImage.Dispose();
                        HOperatorSet.CopyImage(hPylonImage, out mimage);
                       // listimage.Add(mimage);
                        listimage1.Enqueue(mimage);
                        hPylonImage.Dispose();
                       
       
                }
                else
                {
                    MessageBox.Show("Grab faild!\n" + grabResult.ErrorDescription, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch (Exception exception)
            {
                Console.WriteLine("  ShowException(exception);");
             //   ShowException(exception);
              //  Turntable.Instance.copos[sortnum + 1].onwrite = false;
                poscmin = Turntable.Instance.copos[sortnum].pos.FirstOrDefault();
                Turntable.Instance.copos[sortnum].pos.Clear();
            }
            finally
            {
              
                // Dispose the grab result if needed for returning it to the grab loop.
                e.DisposeGrabResultIfClone();
          //    Console.WriteLine("CAM" + sortnum + "when--2--" + DateTime.Now.ToString("mm-ss-fff"));
            }
        }