资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#:使用海康SDK绘图回调函数DrawFun()

简述

  使用海康摄像头采集图像时,需要在图像上添加图标、文字等额外数据。可选择使用SDK提供的NET_DVR_RigisterDrawFun()函数注册绘图回调函数,使用NET_DVR_RealPlay_V40()的返回值(用于在DrawFun()函数中区分通道数据)及DrawFun回调函数的指针进行注册。注意:使用DrawFun回调函数,需要在调用NET_DVR_RealPlay_V40()时将第三个参数设置为null。

专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业贵州免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

代码

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()


本文标题:C#:使用海康SDK绘图回调函数DrawFun()
网站链接:http://cdkjz.cn/article/jejogo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220