コード例 #1
0
        private void StreamGrabber_ImageGrabbed(object sender, Basler.Pylon.ImageGrabbedEventArgs e)
        {
            if (_grabCount > 0)
            {
                _count++;

                if (_count > _grabCount)
                {
                    //Modify.Choijh.2021.05.27.Insert Disable Stop Grab Code.Start...
                    //Stop();
                    //Modify.Choijh.2021.05.27.Insert Disable Stop Grab Code.End...
                    if (GrabDone != null)
                    {
                        GrabDone();
                    }
                }
            }

            Basler.Pylon.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 GrabInfo(
                                EGrabResult.Success, 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 GrabInfo(
                                EGrabResult.Success, result.Width, result.Height, 3, data));
                    }

                    return;
                }
            }

            if (ImageGrabbed != null)
            {
                ImageGrabbed(new GrabInfo(EGrabResult.Error));
            }
        }
コード例 #2
0
        private void DeviceStreamGrabber_ImageGrabbedEvent(object sender, ImageGrabbedEventArgs e)
        {
            Basler.Pylon.IGrabResult grabResult = e.GrabResult;
            if (grabResult.GrabSucceeded)
            {
                Basler.Pylon.PixelType pixelType = grabResult.PixelTypeValue;
                if (pixelType == Basler.Pylon.PixelType.Mono8)
                {
                    //allocate the m_stream_size amount of bytes in non-managed environment
                    if (_latestFrameAddress == IntPtr.Zero)
                    {
                        _latestFrameAddress = System.Runtime.InteropServices.Marshal.AllocHGlobal((Int32)grabResult.PayloadSize);
                    }

                    _converter.OutputPixelFormat = Basler.Pylon.PixelType.Mono8;
                    _converter.Convert(_latestFrameAddress, grabResult.PayloadSize, grabResult);
                    HalconDotNet.HOperatorSet.GenImage1(out HoImage, "byte", (HalconDotNet.HTuple)grabResult.Width, (HalconDotNet.HTuple)grabResult.Height, (HalconDotNet.HTuple)_latestFrameAddress);
                }
                else if (grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerBG8 ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerGB8 ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerRG8 ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerGR8 ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerRG12Packed ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.YUV422packed ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.YUV422_YUYV_Packed ||
                         grabResult.PixelTypeValue == Basler.Pylon.PixelType.BayerRG12
                         )
                {
                    if (_latestFrameAddress == IntPtr.Zero)
                    {
                        _latestFrameAddress = System.Runtime.InteropServices.Marshal.AllocHGlobal((Int32)(3 * grabResult.Width * grabResult.Height));
                    }

                    _converter.OutputPixelFormat = Basler.Pylon.PixelType.BGR8packed;// 根据bayer格式不同切换以下代码
                    _converter.Convert(_latestFrameAddress, 3 * grabResult.Width * grabResult.Height, grabResult);
                    HalconDotNet.HOperatorSet.GenImageInterleaved(out HoImage, _latestFrameAddress, "bgr", (HalconDotNet.HTuple)_imageWidth, (HalconDotNet.HTuple)_imageHeight, -1, "byte", (HalconDotNet.HTuple)_imageWidth, (HalconDotNet.HTuple)_imageHeight, 0, 0, -1, 0);
                }

                System.Runtime.InteropServices.Marshal.FreeHGlobal(_latestFrameAddress);
                _latestFrameAddress = IntPtr.Zero;


                if (HoImage != null &&
                    HoImage.IsInitialized())
                {
                    IsImageGrabbed = true;
                    if (CameraImageGrabbedEvt != null)
                    {
                        CameraImageGrabbedEvt(Camera, HoImage);
                    }
                }
            }
        }