1. 程式人生 > >C#:使用海康SDK繪圖回調函數DrawFun()

C#:使用海康SDK繪圖回調函數DrawFun()

efault 技術 對講 eal isp images als ets tpi

簡述

??使用海康攝像頭采集圖像時,需要在圖像上添加圖標、文字等額外數據。可選擇使用SDK提供的NET_DVR_RigisterDrawFun()函數註冊繪圖回調函數,使用NET_DVR_RealPlay_V40()的返回值(用於在DrawFun()函數中區分通道數據)及DrawFun回調函數的指針進行註冊。註意:使用DrawFun回調函數,需要在調用NET_DVR_RealPlay_V40()時將第三個參數設置為null。

代碼

class DeviceCamera : IDeviceCamera
    {
        private CameraInfo m_stCameraInfo;

        private bool m_bInitSDK = false;
        private Int32 m_lUserID = -1;
        private Int32 m_lRealHandle = -1;

        private CHCNetSDK.DRAWFUN cbDrawFun = null;

        public override bool InitCamera( CameraInfo stInfo )
        {
            m_stCameraInfo = stInfo;

            //
            m_bInitSDK = CHCNetSDK.NET_DVR_Init();
            if ( m_bInitSDK == false )
            {
                uint nError = CHCNetSDK.NET_DVR_GetLastError();
                MessageBox.Show( "NET_DVR_Init error! ErrorCode: " + nError );
                return false;
            }
            else
                CHCNetSDK.NET_DVR_SetLogToFile( 3, "C:\\SdkLog\\", true );

            CHCNetSDK.NET_DVR_SetConnectTime( 5000, 1 );
            CHCNetSDK.NET_DVR_SetReconnect( 10000, 1 );

            string DVRIPAddress = stInfo.strIP;     
            Int16 DVRPortNumber = stInfo.nPort;     
            string DVRUserName = stInfo.strUserName;
            string DVRPassword = stInfo.strPassword;
            CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
            m_lUserID = CHCNetSDK.NET_DVR_Login_V30( DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref DeviceInfo );
            if ( m_lUserID < 0 )
            {
                MessageBox.Show( "登錄失敗!" );
                CHCNetSDK.NET_DVR_Cleanup();
                return false;
            }

            //
            CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO() 
            {
                dwStreamType = 0,    
                dwLinkMode = 0,    
                bBlocked = true,     
                dwDisplayBufNum = 10,
            };

            //遍歷PictureBox列表
            cbDrawFun = new CHCNetSDK.DRAWFUN( fDrawFun );
            int nPicBoxSize = m_stCameraInfo.lstPicBoxHdc.Count() <= 4 ? m_stCameraInfo.lstPicBoxHdc.Count() : 4; //海康攝像頭模擬通道數 <= 4
            for ( int nPicBoxIndex = 1; nPicBoxIndex <= nPicBoxSize; nPicBoxIndex++ )
            {
                lpPreviewInfo.hPlayWnd = m_stCameraInfo.lstPicBoxHdc[ nPicBoxIndex - 1 ];
                lpPreviewInfo.lChannel = nPicBoxIndex;
                m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40( m_lUserID, ref lpPreviewInfo, null, new IntPtr() );

                //設置異常消息回調函數
                bool bRet = CHCNetSDK.NET_DVR_SetExceptionCallBack_V30( 0, lpPreviewInfo.hPlayWnd, new CHCNetSDK.EXCEPYIONCALLBACK( cbException ), new IntPtr() );
                if ( !bRet )
                    MessageBox.Show( "設置異常消息回調函數失敗!" );

                //註冊繪圖回調函數
                bRet = CHCNetSDK.NET_DVR_RigisterDrawFun( m_lRealHandle, cbDrawFun, 0 );
                if ( !bRet )
                {
                    MessageBox.Show( "註冊繪圖回調函數失敗!" );
                    return false;
                }
            }

            return true;
        }

        private void fDrawFun( int lRealHandle, IntPtr hDC, uint dwUser )
        {
            try
            {
                Graphics pDC = Graphics.FromHdc( hDC );
                if ( null == pDC )
                    return;
                Brush hBrush = new SolidBrush( Color.Red );
                Pen pen = new Pen( hBrush );

                Rectangle rectTmp = new Rectangle( 100, 100, 100, 100 ); 

               if( 0 == lRealHandle )//Channel 1
               {
                   //Image
                   Bitmap img = new Bitmap( "../Images/NucSource.png" );
                   pDC.DrawImage( img, rectTmp );

                   //Text
                   string strText = "Novel RPS";
                   Font font = new Font( "黑體", 18, FontStyle.Italic | FontStyle.Bold );
                   Point pt = new Point( 100, 75 );
                   pDC.DrawString( strText, font, hBrush, pt );

                   //Rectangle
                   pDC.DrawRectangle( pen, rectTmp );
                   img.Dispose();
               }
               else if ( 1 == lRealHandle )//Channel 2
               {
                   //Text
                   string strText = "Novel RPS";
                   Font font = new Font( "黑體", 18, FontStyle.Italic | FontStyle.Bold );
                   Point pt = new Point( 100, 75 );
                   pDC.DrawString( strText, font, hBrush, pt );
               }
               else if ( 2 == lRealHandle )//Channel 3
               {
                   //Rectangle
                   pDC.DrawRectangle( pen, rectTmp );
               }
               else if ( 3 == lRealHandle )//Channel 4
               {
                   //Image
                   Bitmap img = new Bitmap( "../Images/NucSource.png" );
                   pDC.DrawImage( img, rectTmp );
                   img.Dispose();
               }

               hBrush.Dispose();
               pen.Dispose();
               pDC.Dispose();
            }
            catch
            {
                uint nError = CHCNetSDK.NET_DVR_GetLastError();
                if( nError != 0 )
                    MessageBox.Show( "cbDrawFun -- Catch Error Code: " + nError );
            }
        }

        private void cbException( uint dwType, int lUserID, int lHandle, IntPtr pUser )
        {
            switch( dwType )
            {
                case 0x8000:
                    MessageBox.Show( "語音對講時網絡異常!" );
                    break;
                case 0x8002:
                    MessageBox.Show( "報警上傳時網絡異常!" );
                    break;
                case 0x8003:
                    MessageBox.Show( "網絡預覽時網絡異常!" );
                    break;
                case 0x8004:
                    MessageBox.Show( "透明通道傳輸時網絡異常!" );
                    break;
                case 0x8005:
                    MessageBox.Show( "預覽時重連!" );
                    break;
                default:
                    MessageBox.Show( "攝像頭默認異常!" );
                    break;
            }
        }
    }

註:代碼中使用PictureBox顯示攝像頭圖像,故將其句柄設置給 lpPreviewInfo.hPlayWnd。

效果示例

技術分享圖片

C#:使用海康SDK繪圖回調函數DrawFun()