//EVENT HANDLER
        void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
        {
            if (Parameters.CarImageCount <= 0 || Parameters.CarImageCount > InternalBufferCount || Parameters.CarImageCount > Parameters.BatchCaptureCount)
            {
                return;
            }

            Helpers.Log.LogThisInfo(" [event of {0}] ", ((BGAPI2.DataStream)sender).Parent.Model);             // device
            Status = BaumerStatus.Capturing;
            int i = 0;

            IsProcessing = true;

            //mDevice.RemoteNodeList["TriggerMode"].Value = "Off";
            Helpers.Log.LogThisInfo("==>> Trigger Mode: " + mDevice.RemoteNodeList["TriggerMode"].Value);

            try
            {
                BGAPI2.Buffer mBufferFilled = null;
                mBufferFilled = mDSEvent.BufferObj;
                if (mBufferFilled == null)
                {
                    System.Diagnostics.Debug.WriteLine("Error: Buffer Timeout after 1000 msec");
                }
                else if (mBufferFilled.IsIncomplete == true)
                {
                    Helpers.Log.LogThisError("Error: Image is incomplete, Trial: {0}", i);
                    // queue buffer again
                    mBufferFilled.QueueBuffer();
                }
                else
                {
                    int w = (int)mBufferFilled.Width, h = (int)mBufferFilled.Height;

                    System.Drawing.Bitmap bb = Helpers.Utility.GetGrayBitmap(w, h, mBufferFilled.MemPtr);

                    //string carPath = Path.Combine(Parameters.BasePath, string.Format("{0}", Helpers.Utility.GetTimeStamp()));
                    string destFilePath = Path.Combine(current_car_path, string.Format("{0}_{1:D3}.bmp", Helpers.Utility.GetTimeStamp(), Parameters.CarImageCount++));
                    Task.Factory.StartNew(() =>
                    {
                        bb.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Bmp);
                    });


                    //event TotalImageShot is removed
                    //TotalImageShot++;
                    //if (ImageFileWritten != null)
                    //	ImageFileWritten(destFilePath);

                    mBufferFilled.QueueBuffer();
                }
            }
            catch (BGAPI2.Exceptions.LowLevelException exx)
            {
                Helpers.Log.LogThisError("-->ExceptionType:    {0} ", exx.GetType());
                Helpers.Log.LogThisError("-->Msg:    {0} ", exx.Message);
                Helpers.Log.LogThisError("-->ErrorDescription: {0} ", exx.GetErrorDescription());
                Helpers.Log.LogThisError("-->in function:      {0} ", exx.GetFunctionName());
            }
            catch (BGAPI2.Exceptions.IException ex)
            {
                Helpers.Log.LogThisError("-->ExceptionType:    {0} ", ex.GetType());
                Helpers.Log.LogThisError("-->Msg:    {0} ", ex.Message);
                Helpers.Log.LogThisError("-->ErrorDescription: {0} ", ex.GetErrorDescription());
                Helpers.Log.LogThisError("-->in function:      {0} ", ex.GetFunctionName());
            }
            Status = BaumerStatus.Ready;
            return;
        }
示例#2
0
        public void mDataStream_NewBufferEvent(object sender, BGAPI2.Events.NewBufferEventArgs mDSEvent)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Restart();

                BGAPI2.Buffer mBufferFilled = null;

                mBufferFilled = mDSEvent.BufferObj;

                if (mBufferFilled == null)
                {
                }

                else if (mBufferFilled.IsIncomplete == true)
                {
                    mBufferFilled.QueueBuffer();
                }

                else
                {
                    int width  = (int)mBufferFilled.Width;
                    int height = (int)mBufferFilled.Height;
                    m_nImageWidth  = width;
                    m_nImageHeight = height;
                    m_sPixelFormat = mBufferFilled.PixelFormat;

                    if (mBufferFilled.PixelFormat == "Mono8")
                    {
                        ulong size = mBufferFilled.SizeFilled;

                        IntPtr pBuffer = mBufferFilled.MemPtr;

                        m_pImgData = new byte[width * height];
                        Marshal.Copy(pBuffer, m_pImgData, 0, width * height);
                    }

                    else //color
                    {
                        ulong        size   = mBufferFilled.SizeFilled;
                        BGAPI2.Image mImage = imgProcessor.CreateImage((uint)mBufferFilled.Width, (uint)mBufferFilled.Height, (string)mBufferFilled.PixelFormat, mBufferFilled.MemPtr, (ulong)mBufferFilled.MemSize);

                        //  BGAPI2.Image mImageTransfer = mImage.TransformImage("BGR8");
                        BGAPI2.Image mTransformImage = imgProcessor.CreateTransformedImage(mImage, "BGR8");
                        try
                        {
                            m_pImgData = new byte[(uint)((uint)mImage.Width * (uint)mImage.Height * 3.0)];
                            Marshal.Copy(mTransformImage.Buffer, m_pImgData, 0, (int)((int)mImage.Width * (int)mImage.Height * 3.0));
                        }
                        catch (Exception)
                        {
                            GrabComplete.Set();
                            MessageBox.Show("拍照处理异常");
                        }

                        if (mImage != null)
                        {
                            mImage.Release();
                            mTransformImage.Release();
                        }
                    }
                    string mode = mDevice.RemoteNodeList["TriggerSource"].Value;

                    if (mode == "Line0")
                    {
                        HTRGNum++;
                    }
                    OnRan(m_pImgData, width, height, m_sPixelFormat);
                    GrabComplete.Set();

                    mBufferFilled.QueueBuffer();
                    sw.Stop();
                    AcqTime = sw.ElapsedMilliseconds;
                    //MessageBox.Show(AcqTime.ToString());
                }
            }

            catch (BGAPI2.Exceptions.IException ex)
            {
                GrabComplete.Set();
                string str;
                str = string.Format("ExceptionType:{0}! ErrorDescription:{1} in function:{2}", ex.GetType(), ex.GetErrorDescription(), ex.GetFunctionName());
                MessageBox.Show("002" + str);
            }
        }