资讯

精准传达 • 有效沟通

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

C#:使用海康SDK绘图回调函数DrawFun()-创新互联

简述

  使用海康摄像头采集图像时,需要在图像上添加图标、文字等额外数据。可选择使用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()

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


本文题目:C#:使用海康SDK绘图回调函数DrawFun()-创新互联
文章源于:http://www.cdkjz.cn/article/cepdjh.html
多年建站经验

多一份参考,总有益处

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

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

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