温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-14 21:34:57 来源:网络 阅读:2707 作者:006玩命 栏目:编程语言

简述

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI