Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        private void SaveImage(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo, IntPtr pUser)
        {
            try
            {
                if (ignoreImage)
                {
                    return;
                }

                //HTuple startTime;
                // HOperatorSet.CountSeconds(out startTime);

                // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 10)
                {
                    //stopWatch.Restart();

                    //if (hPylonImage != null && hPylonImage.IsInitialized())
                    //{
                    //    hPylonImage.Dispose();
                    //}
                    hPylonImage = new Cognex.VisionPro.CogImage8Grey();

                    if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                    {
                        //hPylonImage.GenImage1("byte", pFrameInfo.nWidth, pFrameInfo.nHeight, pData);
                        var cogRoot = new Cognex.VisionPro.CogImage8Root();



                        uint buffersize = new uint();


                        // m_pOperator.GetIntValue("PayloadSize",ref buffersize);



                        //byte[] ImagebufferPtr = new byte[pFrameInfo.nWidth * pFrameInfo.nHeight];
                        //var pBufForDriver = Marshal.AllocHGlobal((Int32)(buffersize));



                        //Marshal.Copy(pBufForDriver, ImagebufferPtr, 0, pFrameInfo.nWidth * pFrameInfo.nHeight - 1);

                        cogRoot.Initialize(pFrameInfo.nWidth, pFrameInfo.nHeight, pData, pFrameInfo.nWidth, null);

                        hPylonImage.SetRoot(cogRoot);
                        GC.Collect();
                        //Marshal.FreeHGlobal(pBufForDriver);


                        /*
                         * MyCamera.MV_FRAME_OUT info = new MyCamera.MV_FRAME_OUT();
                         * var stDevInfo = (MyCamera.MV_FRAME_OUT)Marshal.PtrToStructure(stDevList.pDeviceInfo[0], typeof(MyCamera.MV_FRAME_OUT));*/
                    }
                    else if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)
                    {/*
                      * hPylonImage.GenImageInterleaved(pData,
                      * "rgb",
                      *  pFrameInfo.nWidth, pFrameInfo.nHeight,
                      * -1, "byte",
                      * pFrameInfo.nWidth, pFrameInfo.nHeight,
                      * 0, 0, -1, 0);*/
                    }
                    else
                    {/*
                      * Util.Notify(string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, pFrameInfo.enPixelType));*/
                    }
                    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));*/
            }
        }