示例#1
0
        /// <summary>
        /// 图像采集到事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StreamGrabber_ImageGrabbed(object objUserParam, IFrameData objIFrameData)
        {
            try
            {
                if (HoImage != null &&
                    HoImage.IsInitialized())
                {
                    HoImage.Dispose();
                    HoImage = null;
                    //System.Threading.Thread.Sleep(10);
                    //System.GC.Collect();
                    //System.GC.WaitForPendingFinalizers();
                    IsImageGrabbed = false;
                }
                int width  = (int)objIFrameData.GetWidth();
                int height = (int)objIFrameData.GetHeight();

                #region 相机SDK内部像素格式转换
                IntPtr            pBuffer;
                GX_VALID_BIT_LIST emValidBits = __GetBestValudBit(objIFrameData.GetPixelFormat());
                if (_IsColorMode)
                {//彩色
                    //不需要释放指针空间
                    pBuffer = objIFrameData.ConvertToRGB24(emValidBits
                                                           , GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);
                    HOperatorSet.GenImageInterleaved(out HoImage, new HTuple(pBuffer), "bgr",
                                                     width, height, new HTuple(-1),
                                                     "byte", width, height, 0, 0, -1, 0);
                }
                else
                {//黑白
                    if (__IsPixelFormat8(objIFrameData.GetPixelFormat()))
                    {
                        pBuffer = objIFrameData.GetBuffer();
                    }
                    else
                    {
                        //不需要释放指针空间
                        pBuffer = objIFrameData.ConvertToRaw8(emValidBits);
                    }
                    HOperatorSet.GenImage1(out HoImage, "byte", width, height, new HTuple(pBuffer));
                }

                if (HoImage != null &&
                    HoImage.IsInitialized())
                {
                    IsImageGrabbed = true;
                    if (CameraImageGrabbedEvt != null)
                    {
                        CameraImageGrabbedEvt(Camera, HoImage);
                    }
                }
            }
            catch (Exception ex)
            { }
            finally
            {
            }
            #endregion
        }
示例#2
0
        private void OnFrameCallbackFun(object objUserParam, IFrameData objIFrameData)
        {
            try
            {
                IntPtr pRaw8Buffer = objIFrameData.ConvertToRaw8(GX_VALID_BIT_LIST.GX_BIT_0_7);
                Marshal.Copy(pRaw8Buffer, ImageArray, 0, ArraySize);
                m_grabBmp = CopyToBitmap(m_width, m_height, ImageArray, PixelFormat.Format8bppIndexed);

                if (m_bIsGrab)
                {
                    manualEvent.Set();
                }

                Bitmap grab0 = (Bitmap)m_grabBmp.Clone();
                ShowOriginalImageEvent(grab0);

                if (m_bCorrect == true)
                {
                    Bitmap grab = (Bitmap)m_grabBmp.Clone();
                    CorrectImageEvent(grab);
                }

                if (count % 15 == 0 && m_bWait == true)
                {
                    imagealg.TargetLocation = TargetLocation;
                    WaitResponseEvent();
                    count = 1;
                }
                count += 1;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.ToString());
            }
        }
        ////////////////////////////////////////////////////////////
        private void __OnFrameCallbackFun(object objUserParam, IFrameData objIFrameData)
        {
            //IntPtr pBufferMono = IntPtr.Zero;
            //pBufferMono = objIFrameData.GetBuffer();
            //int stride = __GetStride((int)objIFrameData.GetWidth(), false);
            //byte[] m_byMonoBuffer = null;
            //m_byMonoBuffer = new byte[stride * (int)objIFrameData.GetHeight()];

            //Marshal.Copy(pBufferMono, m_byMonoBuffer, 0, stride * (int)objIFrameData.GetHeight());


            //GCHandle hObject = GCHandle.Alloc(m_byMonoBuffer, GCHandleType.Pinned);
            //IntPtr pObject = hObject.AddrOfPinnedObject();

            //Bitmap bmp = BuiltGrayBitmap(m_byMonoBuffer, (int)objIFrameData.GetWidth(), (int)objIFrameData.GetHeight());
            //// Bitmap tempbmp = (Bitmap)bmp.Clone();
            //CallFunction(null, bmp);

            //hObject.Free();
            ////if (bmp != null)
            ////    bmp.Dispose();

            //GC.Collect();


            try
            {
                Bitmap       dec     = new Bitmap((int)objIFrameData.GetWidth(), (int)objIFrameData.GetHeight(), PixelFormat.Format8bppIndexed);
                ColorPalette palette = dec.Palette;
                for (int i = 0; i < 256; i++)
                {
                    palette.Entries[i] = Color.FromArgb(i, i, i);
                }
                dec.Palette = palette;
                Rectangle  rect       = new Rectangle(0, 0, dec.Width, dec.Height);
                BitmapData decBmpData = dec.LockBits(rect, ImageLockMode.ReadWrite, dec.PixelFormat);

                IntPtr ptrSrc    = objIFrameData.ConvertToRaw8(GX_VALID_BIT_LIST.GX_BIT_0_7);
                byte[] p_byteSrc = new byte[objIFrameData.GetPayloadSize()];
                int    stride    = (int)objIFrameData.GetWidth();
                int    buffsize  = stride * (int)objIFrameData.GetHeight();
                Marshal.Copy(ptrSrc, p_byteSrc, 0, buffsize);
                Marshal.Copy(p_byteSrc, 0, decBmpData.Scan0, buffsize);
                dec.UnlockBits(decBmpData);
                CallFunction(this.Name, dec);
            }
            catch (CGalaxyException ex)
            {
                throw (ex.InnerException);
            }
        }