c# 解码海康数据流
2018-11-29 10:42:59 weixin_34241036 阅读数 2

简述

  使用海康摄像头采集图像时,需要在图像上添加图标、文字等额外数据。可选择使用海康SDK绘图回调函数叠加字符、图像等(请参考上一篇文章);也可使用海康SDK的解码回调函数,对视频流数据进行解码后处理。该方法流程为:调用视频预览函数NET_DVR_RealPlay_V40()时将第三个参数设置为实时数据回调函数RealDataCallBack()的函数指针,然后在RealDataCallBack()回调函数中注册视频流数据解码函数DecCallbackFUN(),最后在DecCallbackFUN()函数中对数据解析解码、叠加字符等处理。
  本文只调用解码回调函数将YV12格式视频流数据转换为RGB32格式数据,然后将RGB32格式数据转换为Image,并使用PictrueBox显示该Image数据,暂不做叠加字符、图像等处理。

代码

1、摄像头操作代码
    struct CameraInfo
    {
        public string strIP;
        public short nPort;
        public string strUserName;
        public string strPassword;
    }

    class IDeviceCamera
    {
        public Image m_img = null;
        public virtual bool InitCamera( CameraInfo stInfo )
        {
            return true;
        }
    }

        class DeviceCamera : IDeviceCamera
    {
        private CameraInfo m_stCameraInfo;

        private bool m_bInitSDK = false;
        private Int32 m_lUserID = -1;
        private Int32 m_lRealHandle = -1;
        private Int32 m_lPort = -1;
        CHCNetSDK.REALDATACALLBACK RealData = null; //必须得定义为成员变量

        public override bool InitCamera( CameraInfo stInfo )
        {
            m_stCameraInfo = stInfo;
            m_bInitSDK = CHCNetSDK.NET_DVR_Init();
            if ( !m_bInitSDK )
            {
                uint nError = CHCNetSDK.NET_DVR_GetLastError();
            }
            CHCNetSDK.NET_DVR_SetConnectTime( 5000, 1 );
            CHCNetSDK.NET_DVR_SetReconnect( 10000, 1 );
            if ( m_bInitSDK == false )
            {
                MessageBox.Show( "NET_DVR_Init error!" );
                return false;
            }
            else
            {
                //保存SDK日志 To save the SDK log
                CHCNetSDK.NET_DVR_SetLogToFile( 3, "C:\\SdkLog\\", true );
            }

            string DVRIPAddress = stInfo.strIP;     //设备IP地址或者域名 Device IP
            Int16 DVRPortNumber = stInfo.nPort;     //设备服务端口号 Device Port
            string DVRUserName = stInfo.strUserName;//设备登录用户名 User name to login
            string DVRPassword = stInfo.strPassword;//设备登录密码 Password to login

            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();
            lpPreviewInfo.hPlayWnd = (IntPtr)null;
            lpPreviewInfo.lChannel = 1;
            lpPreviewInfo.dwStreamType = 0;       //码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推
            lpPreviewInfo.dwLinkMode = 0;         //连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP 
            lpPreviewInfo.bBlocked = true;        //0- 非阻塞取流,1- 阻塞取流
            lpPreviewInfo.dwDisplayBufNum = 15;   //播放库播放缓冲区最大缓冲帧数

            //使用回调函数取摄像头数据
            RealData = new CHCNetSDK.REALDATACALLBACK( RealDataCallBack );//预览实时流回调函数
            IntPtr pUser = new IntPtr();//用户数据

            m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40( m_lUserID, ref lpPreviewInfo, RealData, pUser );
            CHCNetSDK.NET_DVR_RigisterDrawFun( m_lRealHandle, new CHCNetSDK.DRAWFUN( cbDrawFun ), 0 );//回调函数:绘制图标
            return true;
        }

        private uint nLastErr = 0;
        private static PlayCtrl.DECCBFUN m_fDisplayFun = null;
        private IntPtr m_ptrRealHandle;
        public void RealDataCallBack( Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser )
        {
            //下面数据处理建议使用委托的方式
            switch ( dwDataType )
            {
                case CHCNetSDK.NET_DVR_SYSHEAD:     // sys head
                    if ( dwBufSize > 0 )
                    {
                        if ( m_lPort >= 0 )
                            return; //同一路码流不需要多次调用开流接口

                        //获取播放句柄 Get the port to play
                        if ( !PlayCtrl.PlayM4_GetPort( ref m_lPort ) )
                        {
                            nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                            break;
                        }

                        //设置流播放模式 Set the stream mode: real-time stream mode
                        if ( !PlayCtrl.PlayM4_SetStreamOpenMode( m_lPort, PlayCtrl.STREAME_REALTIME ) )
                        {
                            nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                            //str = "Set STREAME_REALTIME mode failed, error code= " + nLastErr;
                            //this.BeginInvoke( AlarmInfo, str );
                        }

                        //打开码流,送入头数据 Open stream
                        if ( !PlayCtrl.PlayM4_OpenStream( m_lPort, pBuffer, dwBufSize, 2 * 1024 * 1024 ) )
                        {
                            nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                            //str = "PlayM4_OpenStream failed, error code= " + nLastErr;
                            //this.BeginInvoke( AlarmInfo, str );
                            break;
                        }

                        //设置显示缓冲区个数 Set the display buffer number
                        if ( !PlayCtrl.PlayM4_SetDisplayBuf( m_lPort, 15 ) )
                        {
                            nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                            //str = "PlayM4_SetDisplayBuf failed, error code= " + nLastErr;
                            //this.BeginInvoke( AlarmInfo, str );
                        }

                        //设置显示模式 Set the display mode
                        //if ( !PlayCtrl.PlayM4_SetOverlayMode( m_lPort, 0, 0) ) //play off screen 
                        //{
                        //    nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                        //    //str = "PlayM4_SetOverlayMode failed, error code= " + nLastErr;
                        //    //this.BeginInvoke( AlarmInfo, str );
                        //}

                        //设置解码回调函数,获取解码后音视频原始数据 Set callback function of decoded data
                        m_fDisplayFun = new PlayCtrl.DECCBFUN( DecCallbackFUN );
                        if ( !PlayCtrl.PlayM4_SetDecCallBackEx( m_lPort, m_fDisplayFun, IntPtr.Zero, 0 ) )
                        {
                            //this.BeginInvoke( AlarmInfo, "PlayM4_SetDisplayCallBack fail" );
                        }

                        //开始解码 Start to play                       
                        if ( !PlayCtrl.PlayM4_Play( m_lPort, m_ptrRealHandle ) )
                        {
                            nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                            //str = "PlayM4_Play failed, error code= " + nLastErr;
                            //this.BeginInvoke( AlarmInfo, str );
                            break;
                        }
                    }
                    break;
                case CHCNetSDK.NET_DVR_STREAMDATA: // video stream data
                default: //the other data
                    if ( dwBufSize > 0 && m_lPort != -1 )
                    {
                        for ( int i = 0; i < 999; i++ )
                        {
                            //送入码流数据进行解码 Input the stream data to decode
                            if ( !PlayCtrl.PlayM4_InputData( m_lPort, pBuffer, dwBufSize ) )
                            {
                                nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
                                //str = "PlayM4_InputData failed, error code= " + nLastErr;
                                Thread.Sleep( 2 );
                            }
                            else
                                break;
                        }
                    }
                    break;
            }
        }

        //解码回调函数
        private void DecCallbackFUN( int nPort, IntPtr pBuf, int nSize, ref PlayCtrl.FRAME_INFO pFrameInfo, int nReserved1, int nReserved2 )
        {
            if ( pFrameInfo.nType == 3 ) //#define T_YV12   3
            {
                byte[] byteBuffYV12 = new byte[ nSize ];
                Marshal.Copy( pBuf, byteBuffYV12, 0, nSize );

                long lRGBSize = (long)pFrameInfo.nWidth * pFrameInfo.nHeight * 4;
                byte[] bufferRGB32 = new byte[ lRGBSize ];
                CommonFun.YV12_to_RGB32( byteBuffYV12, bufferRGB32, pFrameInfo.nWidth, pFrameInfo.nHeight );
                byteBuffYV12 = null;

                Bitmap bmpFromGRB32 = CommonFun.RGB32_to_Image( bufferRGB32, pFrameInfo.nWidth, pFrameInfo.nHeight );
                bufferRGB32 = null;
                if ( null == bmpFromGRB32 )
                    return;
                m_img = bmpFromGRB32;
            }
        }
    }

注:得到的m_img用于PictureBox进行显示。

2、YV12转换为RGB32、RGB32转换为Image代码
class CommonFun
    {
        public static bool YV12_to_RGB32(  byte[] buffYV12, byte[] bufferRGB32, int nWidth, int nHeight )
        {
            if( buffYV12.Length < 0 || bufferRGB32.Length < 0 )
                return false;

            long nYLen = (long)nWidth * nHeight;
            int nHfWidth = ( nWidth >> 1 );

            if ( nYLen < 1 || nHfWidth < 1 )
                return false;

            byte[] byteYData = buffYV12.Skip( 0 ).Take( nWidth*nHeight ).ToArray();
            byte[] byteUData = buffYV12.Skip( nWidth*nHeight ).Take( (nHeight/2)*(nWidth/2) ).ToArray();
            byte[] byteVData = buffYV12.Skip( nWidth*nHeight + (nHeight/2)*(nWidth/2) ).Take( (nHeight/2)*(nWidth/2) ).ToArray();

            if ( byteYData.Length < 0 || byteVData.Length < 0 || byteUData.Length < 0 )
                return false;

            int[] nRgb = new int[4];
            for( int nRow = 0; nRow < nHeight; nRow++ )
            {
                for( int nCol = 0; nCol < nWidth; nCol++ )
                {
                    int Y = byteYData[nRow*nWidth + nCol];
                    int U = byteUData[(nRow / 2)*(nWidth / 2) + (nCol / 2)];
                    int V = byteVData[(nRow / 2)*(nWidth / 2) + (nCol / 2)];
                    int R = Y + (U - 128) + (((U - 128) * 103) >> 8);
                    int G = Y - (((V - 128) * 88) >> 8) - (((U - 128) * 183) >> 8);
                    int B = Y + (V - 128) + (((V - 128) * 198) >> 8);

                    // r分量值 
                    R = R<0 ? 0 : R;
                    nRgb[2] = R > 255 ? 255 : R;

                    // g分量值
                    G = G<0 ? 0 : G;
                    nRgb[1] = G > 255 ? 255 : G;

                    // b分量值 
                    B = B<0 ? 0 : B;
                    nRgb[0] = B > 255 ? 255 : B;

                    //A分量值
                    nRgb[ 3 ] = 255;

                    //Out RGB Buffer
                    bufferRGB32[4 * (nRow*nWidth + nCol) + 0] = (byte)nRgb[0];
                    bufferRGB32[4 * (nRow*nWidth + nCol) + 1] = (byte)nRgb[1];
                    bufferRGB32[4 * (nRow*nWidth + nCol) + 2] = (byte)nRgb[2];
                    bufferRGB32[4 * (nRow*nWidth + nCol) + 3] = (byte)nRgb[3];
                }
            }
            return true;
        }

        public static Bitmap RGB32_to_Image( byte[] byteBuff, int nWidth, int nHeight )
        {
            if ( byteBuff.Length <= 0 || byteBuff.Length < nWidth*nHeight )
                return null;
            Bitmap bmp = new Bitmap( nWidth, nHeight, PixelFormat.Format32bppArgb );

            //锁定内存数据
            BitmapData bmpData = bmp.LockBits( new Rectangle( 0, 0, nWidth, nHeight ), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );

            //输入颜色数据
            System.Runtime.InteropServices.Marshal.Copy( byteBuff, 0, bmpData.Scan0, byteBuff.Length );

            //解锁
            bmp.UnlockBits( bmpData );

            return bmp;
        }
    }

效果

C#:使用海康SDK解码回调函数DecCallbackFUN()

转载于:https://blog.51cto.com/weiyuqingcheng/2323546

2017-03-22 20:25:27 qq_36545099 阅读数 459

C#数据流

  C#编程中数据流的使用一直不很熟练,没有一个系统的认识,但是它的重要性显然不言而喻。System.IO下的Stream类是所有数据流的基类,当我们对数据进行逐字节操作时,首先需要将数据转换为数据流。C#数据流主要分为三类:FileStream、MemoryStream、NetworkStream,还有常用的StreamReader、StreamWriter和TextWriter类等。

  1.FileStream(文件流),继承Stream类。由名字可以看到这个类主要是对文件进行操作的。得到FileStream对象的常用方式有如下两种:

    FileStream fs=new FileStream(string path,FileMode mode,FileAccess access);//利用构造函数创建,FileMode枚举和FileAccess枚举这里就不总结了。

    FileStream fs=File.OpenWrite(@"C:\");//还有File.Open、File.OpenRead、File.OpenText都用的很多。

    得到FileStream对象后,调用常见的Read和Write方法即可进行读写,如果要读写比1kb字节大的文件时可以加个while循环来获得数据。

    //写文件流
    string str = "hh";
    byte[] bytes1 = new byte[1024];
    bytes1 = Encoding.UTF8.GetBytes(str);
    FileStream fst = new FileStream("sd", FileMode.OpenOrCreate, FileAccess.Write);
    fst.Write(bytes1, 0, bytes1.Length);
    //读文件流
    FileStream fs = File.OpenRead("sd");
    byte[] bytes2 = new byte[1024];
    int num = fs.Read(bytes2, 0, bytes2.Length);

  2.MemoryStream,内存流,它是以内存流的形式对内存中的字节数组进行操作,支持对数据流的查找和随机访问,和文件流一样可以使用Position属性得到内存流的当前位置。内存流的优点是对长度不定的数据进行缓存时,由于内存流是可以自动增长的,因此使用很方便。

    byte[] bytes=Encoding.UTF8.GetBytes("hh");
    using(MemoryStream ms=new MemoryStream())
    {
      ms.Write(bytes, 0, bytes.Length);
      byte[] byte2 = new byte[bytes.Length];
      ms.Position = 0;
      int n = ms.Read(byte2, 0, byte2.Length);
    }

  3.NetworkStream,网络流,这个流在网络编程里使用非常普遍。当客户端程序与服务器程序进行通信时数据的接受与发送就要用到这个类,有一点要特别注意,NetworkStream类仅仅只支持面向连接的套接字,比如使用TcpClient编写聊天程序时,只有服务器与客户端的TcpClient进行了连接后才能使用NetworkStream。得到NetworkStream对象创建好后就可以利用网络流对象进行网络数据的传递,但是网络流对象是没有Position对象的,无法指定起始位置来进行读写。还有NetworkStream对象的CanSeek属性始终返回false,使用Position和Seek方法会出现异常。

  得到NetworkStream对象有2种方式,一种是利用socket对象获得,一种是利用TcpClient对象来获得,后者我用的非常多。

    //写入数据到缓冲流

    NetworkStream ns=new NetworkStream(socket);

    NetworkStream ns=tcpClient.GetStream();

    if(ns.CanWrite)

    {

      byte[] bytes=Encoding.UTF8.GetBytes("hh");

      ns.Write(bytes,0,bytes.Length);

    }

    //读取数据
    if (ns.CanRead)
    {
      int num;
      byte[] bytes = new byte[1024];
      while (ns.DataAvailable)
      {
        num = ns.Read(bytes, 0, bytes.Length); 
      }
    }

  很明显,在实际编程中如果使用字节数组来接受或发送数据太麻烦了,后来从网络编程老师那学到了两个非常好用的类BinaryReader类和BinaryWriter类。我们在实际通信中使用最多的还是字符串通信,只要得到NetworkStream对象后,就可以得到字符串读取流和写入流,比如以下代码

    TcpClient tcp = new TcpClient();
    NetworkStream networkStream = new NetworkStream();
    BinaryReader reader;
    BinaryWriter writer;
    string msg = "hh";
    networkStream = tcp.GetStream();
    writer = new BinaryWriter(networkStream);
    writer.Write(msg);//写字符串
    string str=reader.ReadString();//读字符串

  还有StreamReader类和StreamWriter类用的也是比较多,比如读一个txt文档读到末尾,可以调用StreamReader对象的ReadToEnd方法来拿到这个txt的所有字符串内容。另外如果要对发送的数据进行加密的话,有一个CryptoStream类用起来也挺方便的,在创建这个对象时可以直接就指定采用的对称加密算法,比如DES加密算法。解密时也非常方便,只需要使用和加密时相同的密钥创建CryptoStream实例,并在创建实例时指定CryptoStreamMode为读模式,即可拿到解密后的数据。

 文章转自:http://www.cnblogs.com/fangyz/p/5122546.html

2009-04-02 08:54:00 doubaijun 阅读数 7092

以前用vc开发一个客户端,现在公司需要做一个视频点播系统。由于项目比较急用vc做,时间可能会不够,所以选择用c#来完成。这个点播系统主要包括客户端和流媒体服务器两个部分。开始项目之前要重新写海康的.h文件,引用vc写的dll。封装.h文件。

namespace HIK
{

    public delegate void fVoiceDataCallBack(int lVoiceComHandle, [MarshalAs(UnmanagedType.LPArray)]  byte[] pRecvDataBuffer, uint dwBufSize, byte byAudioFlag, uint dwUser);

    public delegate void SerialDataCallBack(int lSerialHandle, [MarshalAs(UnmanagedType.LPArray)]  byte[] pRecvDataBuffer, uint dwBufSize, uint dwUser);

    public delegate void DrawFun(int lRealHandle, System.Drawing.Graphics hDc, uint dwUser);

    public delegate void RealDataCallBack(int lRealHandle, uint dwDataType, [MarshalAs(UnmanagedType.LPArray, SizeConst = 11520)] byte[] pBuffer, uint dwBufSize, uint dwUser);

    public delegate void PlayDataCallBack(int lPlayHandle, uint dwDataType, [MarshalAs(UnmanagedType.LPArray)]  byte[] pBuffer, uint dwBufSize, uint dwUser);

    public struct NET_DVR_DEVICEINFO
    {
        // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] 

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
        public byte[] sSerialNumber;//[SERIALNO_LEN];  //序列号
        public byte byAlarmInPortNum;  //DVR报警输入个数
        public byte byAlarmOutPortNum;  //DVR报警输出个数
        public byte byDiskNum;    //DVR 硬盘个数
        public byte byDVRType;    //DVR类型,
        public byte byChanNum;    //DVR 通道个数
        public byte byStartChan;   //起始通道号,例如DVS-1,DVR - 1
    }


    public struct NET_DVR_DISKSTATE
    {
        public uint dwVolume;//硬盘的容量
        public uint dwFreeSpace;//硬盘的剩余空间
        public uint dwHardDiskStatic; //硬盘的状态,休眠,活动,不正常等
    }


    public struct NET_DVR_TIME
    {
        public uint dwYear;  //年
        public uint dwMonth;  //月
        public uint dwDay;  //日
        public uint dwHour;  //时
        public uint dwMinute;  //分
        public uint dwSecond;  //秒
    }

    public struct NET_DVR_FIND_DATA
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
        public string sFileName;//文件名
        public NET_DVR_TIME struStartTime;//文件的开始时间
        public NET_DVR_TIME struStopTime;//文件的结束时间
        public uint dwFileSize;//文件的大小
    }


    public struct NET_DVR_CHANNELSTATE
    {
        public byte byRecordStatic; //通道是否在录像,0-不录像,1-录像
        public byte bySignalStatic; //连接的信号状态,0-正常,1-信号丢失
        public byte byHardwareStatic;//通道硬件状态,0-正常,1-异常,例如DSP死掉
        public char reservedData;
        public uint dwBitRate;//实际码率
        public uint dwLinkNum;//客户端连接的个数
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]  //MAX_LINK
        public uint[] dwClientIP;//客户端的IP地址
    }

    public struct NET_DVR_WORKSTATE
    {

        public uint dwDeviceStatic;  //设备的状态,0-正常,1-CPU占用率太高,超过85%,2-硬件错误,例如串口死掉

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public NET_DVR_DISKSTATE[] struHardDiskStatic; //MAX_DISKNUM

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public NET_DVR_CHANNELSTATE[] struChanStatic;//通道的状态MAX_CHANNUM

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] byAlarmInStatic; //报警端口的状态,0-没有报警,1-有报警MAX_ALARMIN

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] byAlarmOutStatic; //报警输出端口的状态,0-没有输出,1-有报警输出MAX_ALARMOUT
        public uint dwLocalDisplay;//本地显示状态,0-正常,1-不正常
    }

    public struct NET_DVR_ETHERNET
    {
        public string sDVRIP;          //DVR IP地址
        public string sDVRIPMask;      // DVR IP地址掩码
        public uint dwNetInterface;   //网络接口 1-10MBase-T 2-10MBase-T全双工 3-100MBase-TX 4-100M全双工 5-10M/100M自适应
        public ushort wDVRPort;  //端口号
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public byte[] byMACAddr;  //服务器的物理地址MACADDR_LEN
    }


    public struct NET_DVR_NETCFG
    {
        public uint dwSize;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public NET_DVR_ETHERNET[] struEtherNet;  /* 以太网口 */
        public string sManageHostIP;  //远程管理主机地址
        public short wManageHostPort; //远程管理主机端口号
        public string sDNSIP;            //DNS服务器地址 
        public string sMultiCastIP;     //多播组地址
        public string sGatewayIP;        //网关地址
        public string sNFSIP;   //NAS主机IP地址
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
        public byte[] sNFSDirectory;//NAS目录PATHNAME_LEN
        public uint dwPPPOE;    //0-不启用,1-启用
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] sPPPoEUser; //PPPoE用户名NAME_LEN
        //  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public string sPPPoEPassword;// PPPoE密码
        public string sPPPoEIP;   //PPPoE IP地址(只读)
        public ushort wHttpPort;    //HTTP端口号
    }

    public struct NET_DVR_ALARMOUTSTATUS
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] Output;//MAX_ALARMOUT
    }

    public struct NET_DVR_CLIENTINFO
    {
        public int lChannel;//通道号
        public int lLinkMode;//最高位(31)为0表示主码流,为1表示子码流,0-30位表示码流连接方式: 0:TCP方式,1:UDP方式,2:多播方式,3 - RTP方式,4-音视频分开(TCP)
        public System.IntPtr hPlayWnd;//播放窗口的句柄,为NULL表示不播放图象
        public string sMultiCastIP;//多播组地址
    }

    /// <summary>
    /// 海康嵌入式
    /// </summary>
    public sealed class Hik_HCNetSDK
    {
        #region 常量定义
        public const int NET_DVR_NOERROR = 0; //没有错误
        public const int NET_DVR_PASSWORD_ERROR = 1;  //用户名密码错误
        public const int NET_DVR_NOENOUGHPRI = 2;  //权限不足
        public const int NET_DVR_NOINIT = 3; //没有初始化
        public const int NET_DVR_CHANNEL_ERROR = 4;  //通道号错误
        public const int NET_DVR_OVER_MAXLINK = 5; //连接到DVR的客户端个数超过最大
        public const int NET_DVR_VERSIONNOMATCH = 6; //版本不匹配
        public const int NET_DVR_NETWORK_FAIL_CONNECT = 7;//连接服务器失败
        public const int NET_DVR_NETWORK_SEND_ERROR = 8; //向服务器发送失败
        public const int NET_DVR_NETWORK_RECV_ERROR = 9;//从服务器接收数据失败
        public const int NET_DVR_NETWORK_RECV_TIMEOUT = 10; //从服务器接收数据超时
        public const int NET_DVR_NETWORK_ERRORDATA = 11; //传送的数据有误
        public const int NET_DVR_ORDER_ERROR = 12;//调用次序错误
        public const int NET_DVR_OPERNOPERMIT = 13; //无此权限
        public const int NET_DVR_COMMANDTIMEOUT = 14;//DVR命令执行超时
        public const int NET_DVR_ERRORSERIALPORT = 15;//串口号错误
        public const int NET_DVR_ERRORALARMPORT = 16; //报警端口错误
        public const int NET_DVR_PARAMETER_ERROR = 17; //参数错误
        public const int NET_DVR_CHAN_EXCEPTION = 18;//服务器通道处于错误状态
        public const int NET_DVR_NODISK = 19;//没有硬盘
        public const int NET_DVR_ERRORDISKNUM = 20;//硬盘号错误
        public const int NET_DVR_DISK_FULL = 21; //服务器硬盘满
        public const int NET_DVR_DISK_ERROR = 22;//服务器硬盘出错
        public const int NET_DVR_NOSUPPORT = 23; //服务器不支持
        public const int NET_DVR_BUSY = 24;//服务器忙
        public const int NET_DVR_MODIFY_FAIL = 25; //服务器修改不成功
        public const int NET_DVR_PASSWORD_FORMAT_ERROR = 26; //密码输入格式不正确
        public const int NET_DVR_DISK_FORMATING = 27; //硬盘正在格式化,不能启动操作
        public const int NET_DVR_DVRNORESOURCE = 28; //DVR资源不足
        public const int NET_DVR_DVROPRATEFAILED = 29; //DVR操作失败
        public const int NET_DVR_OPENHOSTSOUND_FAIL = 30; //打开PC声音失败
        public const int NET_DVR_DVRVOICEOPENED = 31; //服务器语音对讲被占用
        public const int NET_DVR_TIMEINPUTERROR = 32; //时间输入不正确
        public const int NET_DVR_NOSPECFILE = 33; //回放时服务器没有指定的文件
        public const int NET_DVR_CREATEFILE_ERROR = 34;//创建文件出错
        public const int NET_DVR_FILEOPENFAIL = 35; //打开文件出错
        public const int NET_DVR_OPERNOTFINISH = 36; //上次的操作还没有完成
        public const int NET_DVR_GETPLAYTIMEFAIL = 37; //获取当前播放的时间出错
        public const int NET_DVR_PLAYFAIL = 38; //播放出错
        public const int NET_DVR_FILEFORMAT_ERROR = 39; //文件格式不正确
        public const int NET_DVR_DIR_ERROR = 40; //路径错误
        public const int NET_DVR_ALLOC_RESOUCE_ERROR = 41; //资源分配错误
        public const int NET_DVR_AUDIO_MODE_ERROR = 42; //声卡模式错误
        public const int NET_DVR_NOENOUGH_BUF = 43;//缓冲区太小
        public const int NET_DVR_CREATESOCKET_ERROR = 44; //创建SOCKET出错
        public const int NET_DVR_SETSOCKET_ERROR = 45; //设置SOCKET出错
        public const int NET_DVR_MAX_NUM = 46;//个数达到最大
        public const int NET_DVR_USERNOTEXIST = 47; //用户不存在
        public const int NET_DVR_WRITEFLASHERROR = 48;  //写FLASH出错
        public const int NET_DVR_UPGRADEFAIL = 49; //DVR升级失败
        public const int NET_DVR_CARDHAVEINIT = 50; //解码卡已经初始化过
        public const int NET_DVR_PLAYERFAILED = 51; //播放器中错误
        public const int NET_DVR_MAX_USERNUM = 52; //用户数达到最大
        public const int NET_DVR_GETLOCALIPANDMACFAIL = 53; //获得客户端的IP地址或物理地址失败
        public const int NET_DVR_NOENCODEING = 54;//该通道没有编码
        public const int NET_DVR_IPMISMATCH = 55; //IP地址不匹配
        public const int NET_DVR_MACMISMATCH = 56;//MAC地址不匹配
        public const int NET_DVR_UPGRADELANGMISMATCH = 57;//升级文件语言不匹配
        public const int NET_DVR_DDRAWDEVICENOSUPPORT = 58;//本地显卡不支持


        //查找文件和日志函数返回值
        public const int NET_DVR_FILE_SUCCESS = 1000; //获得文件信息
        public const int NET_DVR_FILE_NOFIND = 1001;//没有文件
        public const int NET_DVR_ISFINDING = 1002;//正在查找文件
        public const int NET_DVR_NOMOREFILE = 1003; //查找文件时没有更多的文件
        public const int NET_DVR_FILE_EXCEPTION = 1004; //查找文件时异常

        //NET_DVR_IsSupport()返回值
        //1-9位分别表示以下信息(位与是TRUE)表示支持;
        public const int NET_DVR_SUPPORT_DDRAW = 0x01;//支持DIRECTDRAW,如果不支持,则播放器不能工作;
        public const int NET_DVR_SUPPORT_BLT = 0x02;//显卡支持BLT操作,如果不支持,则播放器不能工作;
        public const int NET_DVR_SUPPORT_BLTFOURCC = 0x04;//显卡BLT支持颜色转换,如果不支持,播放器会用软件方法作RGB转换;
        public const int NET_DVR_SUPPORT_BLTSHRINKX = 0x08;//显卡BLT支持X轴缩小;如果不支持,系统会用软件方法转换;
        public const int NET_DVR_SUPPORT_BLTSHRINKY = 0x10;//显卡BLT支持Y轴缩小;如果不支持,系统会用软件方法转换;
        public const int NET_DVR_SUPPORT_BLTSTRETCHX = 0x20;//显卡BLT支持X轴放大;如果不支持,系统会用软件方法转换;
        public const int NET_DVR_SUPPORT_BLTSTRETCHY = 0x40;//显卡BLT支持Y轴放大;如果不支持,系统会用软件方法转换;
        public const int NET_DVR_SUPPORT_SSE = 0x80;//CPU支持SSE指令,Intel Pentium3以上支持SSE指令;
        public const int NET_DVR_SUPPORT_MMX = 0x100;//CPU支持MMX指令集,Intel Pentium3以上支持SSE指令;

        public const int SET_PRESET = 8;// 设置预置点
        public const int CLE_PRESET = 9; // 清除预置点
        public const int GOTO_PRESET = 39;// 转到预置点

        public const int LIGHT_PWRON = 2; /* 接通灯光电源 */
        public const int WIPER_PWRON = 3; /* 接通雨刷开关 */
        public const int FAN_PWRON = 4;/* 接通风扇开关 */
        public const int HEATER_PWRON = 5;/* 接通加热器开关 */
        public const int AUX_PWRON = 6;/* 接通辅助设备开关 */

        public const int ZOOM_IN = 11; /* 焦距以速度SS变大(倍率变大) */
        public const int ZOOM_OUT = 12;/* 焦距以速度SS变小(倍率变小) */
        public const int FOCUS_NEAR = 13; /* 焦点以速度SS前调 */
        public const int FOCUS_FAR = 14;/* 焦点以速度SS后调 */
        public const int IRIS_OPEN = 15; /* 光圈以速度SS扩大 */
        public const int IRIS_CLOSE = 16; /* 光圈以速度SS缩小 */
        public const int TILT_UP = 21;/* 云台以SS的速度上仰 */
        public const int TILT_DOWN = 22;/* 云台以SS的速度下俯 */
        public const int PAN_LEFT = 23;/* 云台以SS的速度左转 */
        public const int PAN_RIGHT = 24; /* 云台以SS的速度右转 */
        public const int PAN_AUTO = 29; /* 云台以SS的速度左右自动扫描 */

        public const int RUN_CRUISE = 36; /* 开始轨迹 */
        public const int RUN_SEQ = 37; /* 开始巡航 */
        public const int STOP_SEQ = 38;/* 停止巡航 */

        //显示模式
        public enum DispMode { NORMALMODE = 0, OVERLAYMODE };
        //发送模式
        public enum TransMode { PTOPTCPMODE, PTOPUDPMODE, MULTIMODE, RTPMODE, AUDIODETACH, NOUSEMODE };

        public const int NET_DVR_SYSHEAD = 1;//系统头数据
        public const int NET_DVR_STREAMDATA = 2; //流数据

        //播放控制命令宏定义 NET_DVR_PlayBackControl,NET_DVR_PlayControlLocDisplay,NET_DVR_DecPlayBackCtrl的宏定义
        public const int NET_DVR_PLAYSTART = 1;//开始播放
        public const int NET_DVR_PLAYSTOP = 2;//停止播放
        public const int NET_DVR_PLAYPAUSE = 3;//暂停播放
        public const int NET_DVR_PLAYRESTART = 4;//恢复播放
        public const int NET_DVR_PLAYFAST = 5;//快放
        public const int NET_DVR_PLAYSLOW = 6;//慢放
        public const int NET_DVR_PLAYNORMAL = 7;//正常速度
        public const int NET_DVR_PLAYFRAME = 8;//单帧放
        public const int NET_DVR_PLAYSTARTAUDIO = 9;//打开声音
        public const int NET_DVR_PLAYSTOPAUDIO = 10;//关闭声音
        public const int NET_DVR_PLAYAUDIOVOLUME = 11;//调节音量
        public const int NET_DVR_PLAYSETPOS = 12;//改变文件回放的进度
        public const int NET_DVR_PLAYGETPOS = 13;//获取文件回放的进度
        public const int NET_DVR_PLAYGETTIME = 14;//获取当前已经播放的时间
        public const int NET_DVR_PLAYGETFRAME = 15;//获取当前已经播放的帧数
        public const int NET_DVR_GETTOTALFRAMES = 16;//获取当前播放文件总的帧数
        public const int NET_DVR_GETTOTALTIME = 17;//获取当前播放文件总的时间
        public const int NET_DVR_THROWBFRAME = 20;//丢B帧

        //NET_DVR_GetDVRConfig,NET_DVR_GetDVRConfig的命令定义
        public const int NET_DVR_GET_DEVICECFG = 100;  //获取设备参数
        public const int NET_DVR_SET_DEVICECFG = 101; //设置设备参数
        public const int NET_DVR_GET_NETCFG = 102; //获取网络参数
        public const int NET_DVR_SET_NETCFG = 103; //设置网络参数
        public const int NET_DVR_GET_PICCFG = 104; //获取图象参数
        public const int NET_DVR_SET_PICCFG = 105; //设置图象参数
        public const int NET_DVR_GET_COMPRESSCFG = 106; //获取压缩参数
        public const int NET_DVR_SET_COMPRESSCFG = 107; //设置压缩参数
        public const int NET_DVR_GET_COMPRESSCFG_EX = 204; //获取压缩参数(扩展)
        public const int NET_DVR_SET_COMPRESSCFG_EX = 205; //设置压缩参数(扩展)
        public const int NET_DVR_GET_RECORDCFG = 108;//获取录像时间参数
        public const int NET_DVR_SET_RECORDCFG = 109; //设置录像时间参数
        public const int NET_DVR_GET_DECODERCFG = 110; //获取解码器参数
        public const int NET_DVR_SET_DECODERCFG = 111; //设置解码器参数
        public const int NET_DVR_GET_RS232CFG = 112; //获取232串口参数
        public const int NET_DVR_SET_RS232CFG = 113;//设置232串口参数
        public const int NET_DVR_GET_ALARMINCFG = 114; //获取报警输入参数
        public const int NET_DVR_SET_ALARMINCFG = 115; //设置报警输入参数
        public const int NET_DVR_GET_ALARMOUTCFG = 116; //获取报警输出参数
        public const int NET_DVR_SET_ALARMOUTCFG = 117; //设置报警输出参数
        public const int NET_DVR_GET_TIMECFG = 118; //获取DVR时间
        public const int NET_DVR_SET_TIMECFG = 119; //设置DVR时间
        public const int NET_DVR_GET_PREVIEWCFG = 120; //获取预览参数
        public const int NET_DVR_SET_PREVIEWCFG = 121; //设置预览参数
        public const int NET_DVR_GET_VIDEOOUTCFG = 122; //获取视频输出参数
        public const int NET_DVR_SET_VIDEOOUTCFG = 123; //设置视频输出参数
        public const int NET_DVR_GET_USERCFG = 124; //获取用户参数
        public const int NET_DVR_SET_USERCFG = 125; //设置用户参数
        public const int NET_DVR_GET_EXCEPTIONCFG = 126; //获取异常参数
        public const int NET_DVR_SET_EXCEPTIONCFG = 127;  //设置异常参数
        public const int NET_DVR_GET_SHOWSTRING = 130; //获取叠加字符参数
        public const int NET_DVR_SET_SHOWSTRING = 131; //设置叠加字符参数
        public const int NET_DVR_GET_AUXOUTCFG = 140; //获取辅助输出设置
        public const int NET_DVR_SET_AUXOUTCFG = 141;  //设置辅助输出设置
        //2006-04-13 -s系列双输出
        public const int NET_DVR_GET_PREVIEWCFG_AUX = 142; //获取预览参数
        public const int NET_DVR_SET_PREVIEWCFG_AUX = 143; //设置预览参数
        public const int NET_DVR_GET_PICCFG_EX = 200; //获取图象参数(扩展)
        public const int NET_DVR_SET_PICCFG_EX = 201; //设置图象参数(扩展)
        public const int NET_DVR_GET_USERCFG_EX = 202; //获取用户参数
        public const int NET_DVR_SET_USERCFG_EX = 203; //设置用户参数

        //回调函数类型
        public const int COMM_ALARM = 0x1100;//报警信息
        public const int COMM_TRADEINFO = 0x1500;//ATMDVR主动上传交易信息

        //消息方式
        //异常类型
        public const int EXCEPTION_AUDIOEXCHANGE = 0x8001;//语音对讲异常
        public const int EXCEPTION_ALARM = 0x8002;//报警异常
        public const int EXCEPTION_PREVIEW = 0x8003; //网络预览异常
        public const int EXCEPTION_SERIAL = 0x8004;//透明通道异常
        public const int EXCEPTION_RECONNECT = 0x8005;//预览时重连

        public const int NAME_LEN = 32;
        public const int SERIALNO_LEN = 48;
        public const int MACADDR_LEN = 6;
        public const int MAX_ETHERNET = 2;
        public const int PATHNAME_LEN = 128;
        public const int PASSWD_LEN = 16;
        public const int MAX_CHANNUM = 16;
        public const int MAX_ALARMOUT = 4;
        public const int MAX_TIMESEGMENT = 4;
        public const int MAX_PRESET = 128;
        public const int MAX_DAYS = 7;
        public const int PHONENUMBER_LEN = 32;
        public const int MAX_DISKNUM = 16;
        public const int MAX_WINDOW = 16;
        public const int MAX_VGA = 1;
        public const int MAX_USERNUM = 16;
        public const int MAX_EXCEPTIONNUM = 16;
        public const int MAX_LINK = 6;
        public const int MAX_ALARMIN = 16;
        public const int MAX_VIDEOOUT = 2;
        public const int MAX_NAMELEN = 16; //DVR本地登陆名
        public const int MAX_RIGHT = 32;//权限
        public const int CARDNUM_LEN = 20;
        public const int MAX_SHELTERNUM = 4;//遮挡区域数
        public const int MAX_DECPOOLNUM = 4;
        public const int MAX_DECNUM = 4;
        public const int MAX_TRANSPARENTNUM = 2;
        public const int MAX_STRINGNUM = 4;
        public const int MAX_AUXOUT = 4;

        /* 网络接口定义 */
        public const int NET_IF_10M_HALF = 1;    /* 10M ethernet */
        public const int NET_IF_10M_FULL = 2;
        public const int NET_IF_100M_HALF = 3;    /* 100M ethernet */
        public const int NET_IF_100M_FULL = 4;
        public const int NET_IF_AUTO = 5;

        //设备型号(DVR类型)
        public const int DVR = 1;
        public const int ATMDVR = 2;
        public const int DVS = 3;
        public const int DEC = 4; /* 6001D */
        public const int ENC_DEC = 5; /* 6001F */
        public const int DVR_HC = 6;
        public const int DVR_HT = 7;
        public const int DVR_HF = 8;
        public const int DVR_HS = 9;
        public const int DVR_HTS = 10;
        public const int DVR_HB = 11;
        public const int DVR_HCS = 12;
        public const int DVS_A = 13;
        public const int DVR_HC_S = 14;
        public const int DVR_HT_S = 15;
        public const int DVR_HF_S = 16;
        public const int DVR_HS_S = 17;
        public const int ATMDVR_S = 18;
        public const int LOWCOST_DVR = 19;
        #endregion

        //  public static readonly  int  SERIALNO_LEN = 48;


        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Init();

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Cleanup();

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_Login(string sDVRIP, ushort wDVRPort, string sUserName, string sPassword, ref  NET_DVR_DEVICEINFO lpDeviceInfo);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Logout(int lUserID);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetDVRMessage(uint nMessage, System.IntPtr hWnd);
        //public static extern bool NET_DVR_SetDVRMessCallBack(BOOL (CALLBACK *fMessCallBack)(int  lCommand,char *sDVRIP,char *pBuf,uint  dwBufLen));
        //public static extern bool NET_DVR_SetDVRMessCallBack_EX(BOOL (CALLBACK *fMessCallBack_EX)(int  lCommand,int  lUserID,char *pBuf,uint  dwBufLen));
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetConnectTime(uint dwWaitTime, uint dwTryTimes);
        [DllImport("HCNetSDK.dll")]
        public static extern uint NET_DVR_GetSDKVersion();
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_IsSupport();
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StartListen(string sLocalIP, ushort wLocalPort);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopListen();

        [DllImport("HCNetSDK.dll")]
        public static extern uint NET_DVR_GetLastError();
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetShowMode(uint dwShowType, System.Drawing.Color colorKey);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_GetDVRIPByResolveSvr(string sServerIP, ushort wServerPort, string sDVRName, ushort wDVRNameLen, string sDVRSerialNumber, ushort wDVRSerialLen, string sGetIP);
        //图像预览
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_RealPlay(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopRealPlay(int lRealHandle);
        //视频参数是索引值 1-10
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ClientSetVideoEffect(int lRealHandle, uint dwBrightValue, uint dwContrastValue, uint dwSaturationValue, uint dwHueValue);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ClientGetVideoEffect(int lRealHandle, uint pBrightValue, uint pContrastValue, uint pSaturationValue, uint pHueValue);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_RigisterDrawFun(int lRealHandle, DrawFun x, uint dwUser);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetPlayerBufNumber(int lRealHandle, uint dwBufNum);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ThrowBFrame(int lRealHandle, uint dwNum);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetAudioMode(uint dwMode);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_OpenSound(int lRealHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CloseSound();
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_OpenSoundShare(int lRealHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CloseSoundShare(int lRealHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Volume(int lRealHandle, ushort wVolume);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SaveRealData(int lRealHandle, string sFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopSaveRealData(int lRealHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetRealDataCallBack(int lRealHandle, RealDataCallBack x, uint dwUser);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CapturePicture(int lRealHandle, string sPicFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_MakeKeyFrame(int lUserID, int lChannel);
        //云台控制
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControl(int lRealHandle, uint dwPTZCommand, uint dwStop);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControl_Other(int lUserID, int lChannel, uint dwPTZCommand, uint dwStop);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_TransPTZ(int lRealHandle, byte[] pPTZCodeBuf, uint dwBufSize);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_TransPTZ_Other(int lUserID, int lChannel, byte[] pPTZCodeBuf, uint dwBufSize);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZPreset(int lRealHandle, uint dwPTZPresetCmd, uint dwPresetIndex);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZPreset_Other(int lUserID, int lChannel, uint dwPTZPresetCmd, uint dwPresetIndex);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_TransPTZ_EX(int lRealHandle, byte[] pPTZCodeBuf, uint dwBufSize);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControl_EX(int lRealHandle, uint dwPTZCommand, uint dwStop);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZPreset_EX(int lRealHandle, uint dwPTZPresetCmd, uint dwPresetIndex);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZCruise(int lRealHandle, uint dwPTZCruiseCmd, byte byCruiseRoute, byte byCruisePoint, ushort wInput);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZCruise_Other(int lUserID, int lChannel, uint dwPTZCruiseCmd, byte byCruiseRoute, byte byCruisePoint, ushort wInput);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZCruise_EX(int lRealHandle, uint dwPTZCruiseCmd, byte byCruiseRoute, byte byCruisePoint, ushort wInput);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZTrack(int lRealHandle, uint dwPTZTrackCmd);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZTrack_Other(int lUserID, int lChannel, uint dwPTZTrackCmd);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZTrack_EX(int lRealHandle, uint dwPTZTrackCmd);
        //带速度的云台控制
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControlWithSpeed(int lRealHandle, uint dwPTZCommand, uint dwStop, uint dwSpeed);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControlWithSpeed_Other(int lUserID, int lChannel, uint dwPTZCommand, uint dwStop, uint dwSpeed);

        //文件回放
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_FindFile(int lUserID, int lChannel, uint dwFileType, ref NET_DVR_TIME lpStartTime, ref NET_DVR_TIME lpStopTime);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_FindNextFile(int lFindHandle, ref NET_DVR_FIND_DATA lpFindData);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_FindClose(int lFindHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_PlayBackByName(int lUserID, string sPlayBackFileName, System.IntPtr hWnd);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_PlayBackByTime(int lUserID, int lChannel, ref NET_DVR_TIME lpStartTime, ref NET_DVR_TIME lpStopTime, System.IntPtr hWnd);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PlayBackControl(int lPlayHandle, uint dwControlCode, uint dwInValue, uint lpOutValue);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopPlayBack(int lPlayHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetPlayDataCallBack(int lPlayHandle, PlayDataCallBack x, uint dwUser);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PlayBackSaveData(int lPlayHandle, string sFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopPlayBackSave(int lPlayHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_GetPlayBackOsdTime(int lPlayHandle, ref NET_DVR_TIME lpOsdTime);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PlayBackCaptureFile(int lPlayHandle, string sFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_GetFileByName(int lUserID, string sDVRFileName, string sSavedFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_GetFileByTime(int lUserID, int lChannel, ref NET_DVR_TIME lpStartTime, ref NET_DVR_TIME lpStopTime, string sSavedFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopGetFile(int lFileHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_GetDownloadPos(int lFileHandle);

        //恢复默认值
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_RestoreConfig(int lUserID);
        //保存参数
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SaveConfig(int lUserID);
        //重启
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_RebootDVR(int lUserID);
        //关闭DVR
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ShutDownDVR(int lUserID);
        //升级
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_Upgrade(int lUserID, string sFileName);
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_GetUpgradeState(int lUpgradeHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CloseUpgradeHandle(int lUpgradeHandle);
        //远程格式化硬盘
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_FormatDisk(int lUserID, int lDiskNumber);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_GetFormatProgress(int lFormatHandle, int pCurrentFormatDisk, int pCurrentDiskPos, int pFormatStatic);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CloseFormatHandle(int lFormatHandle);
        //报警
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_SetupAlarmChan(int lUserID);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CloseAlarmChan(int lAlarmHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_GetAlarmOut(int lUserID, ref NET_DVR_ALARMOUTSTATUS lpAlarmOutState);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetAlarmOut(int lUserID, int lAlarmOutPort, int lAlarmOutStatic);

 


        //语音对讲
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_StartVoiceCom(int lUserID, fVoiceDataCallBack x, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SetVoiceComClientVolume(int lVoiceComHandle, ushort wVolume);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopVoiceCom(int lVoiceComHandle);
        //语音广播
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ClientAudioStart();

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ClientAudioStop();

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_AddDVR(int lUserID);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_DelDVR(int lUserID);
        //语音转发
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StartVoiceCom_MR(int lUserID, fVoiceDataCallBack x, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_VoiceComSendData(int lVoiceComHandle, byte[] pSendBuf, uint dwBufSize);

        //透明通道设置
        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_SerialStart(int lUserID, int lSerialPort, SerialDataCallBack x, uint dwUser);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SerialSend(int lSerialHandle, int lChannel, byte[] pSendBuf, uint dwBufSize);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SerialStop(int lSerialHandle);
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SendTo232Port(int lUserID, byte[] pSendBuf, uint dwBufSize);
        //远程控制本地显示
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_ClickKey(int lUserID, int lKeyIndex);

        //远程控制手动录像
        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StartDVRRecord(int lUserID, int lChannel, int lRecordType);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopDVRRecord(int lUserID, int lChannel);
    }
}

2018-02-08 09:52:19 weixin_41715295 阅读数 104

http://www.cnblogs.com/fangyz/p/5122546.html

C#编程中数据流的使用一直不很熟练,没有一个系统的认识,但是它的重要性显然不言而喻。System.IO下的Stream类是所有数据流的基类,当我们对数据进行逐字节操作时,首先需要将数据转换为数据流。C#数据流主要分为三类:FileStream、MemoryStream、NetworkStream,还有常用的StreamReader、StreamWriter和TextWriter类等。

  1.FileStream(文件流),继承Stream类。由名字可以看到这个类主要是对文件进行操作的。得到FileStream对象的常用方式有如下两种:

    FileStream fs=new FileStream(string path,FileMode mode,FileAccess access);//利用构造函数创建,FileMode枚举和FileAccess枚举这里就不总结了。

    FileStream fs=File.OpenWrite(@"C:\");//还有File.Open、File.OpenRead、File.OpenText都用的很多。

    得到FileStream对象后,调用常见的Read和Write方法即可进行读写,如果要读写比1kb字节大的文件时可以加个while循环来获得数据。

    //写文件流
    string str = "hh";
    byte[] bytes1 = new byte[1024];
    bytes1 = Encoding.UTF8.GetBytes(str);
    FileStream fst = new FileStream("sd", FileMode.OpenOrCreate, FileAccess.Write);
    fst.Write(bytes1, 0, bytes1.Length);
    //读文件流
    FileStream fs = File.OpenRead("sd");
    byte[] bytes2 = new byte[1024];
    int num = fs.Read(bytes2, 0, bytes2.Length);

  2.MemoryStream,内存流,它是以内存流的形式对内存中的字节数组进行操作,支持对数据流的查找和随机访问,和文件流一样可以使用Position属性得到内存流的当前位置。内存流的优点是对长度不定的数据进行缓存时,由于内存流是可以自动增长的,因此使用很方便。

    byte[] bytes=Encoding.UTF8.GetBytes("hh");
    using(MemoryStream ms=new MemoryStream())
    {
      ms.Write(bytes, 0, bytes.Length);
      byte[] byte2 = new byte[bytes.Length];
      ms.Position = 0;
      int n = ms.Read(byte2, 0, byte2.Length);
    }

  3.NetworkStream,网络流,这个流在网络编程里使用非常普遍。当客户端程序与服务器程序进行通信时数据的接受与发送就要用到这个类,有一点要特别注意,NetworkStream类仅仅只支持面向连接的套接字,比如使用TcpClient编写聊天程序时,只有服务器与客户端的TcpClient进行了连接后才能使用NetworkStream。得到NetworkStream对象创建好后就可以利用网络流对象进行网络数据的传递,但是网络流对象是没有Position对象的,无法指定起始位置来进行读写。还有NetworkStream对象的CanSeek属性始终返回false,使用Position和Seek方法会出现异常。

  得到NetworkStream对象有2种方式,一种是利用socket对象获得,一种是利用TcpClient对象来获得,后者我用的非常多。

    //写入数据到缓冲流

    NetworkStream ns=new NetworkStream(socket);

    NetworkStream ns=tcpClient.GetStream();

    if(ns.CanWrite)

    {

      byte[] bytes=Encoding.UTF8.GetBytes("hh");

      ns.Write(bytes,0,bytes.Length);

    }

    //读取数据
    if (ns.CanRead)
    {
      int num;
      byte[] bytes = new byte[1024];
      while (ns.DataAvailable)
      {
        num = ns.Read(bytes, 0, bytes.Length); 
      }
    }

  很明显,在实际编程中如果使用字节数组来接受或发送数据太麻烦了,后来从网络编程老师那学到了两个非常好用的类BinaryReader类和BinaryWriter类。我们在实际通信中使用最多的还是字符串通信,只要得到NetworkStream对象后,就可以得到字符串读取流和写入流,比如以下代码

    TcpClient tcp = new TcpClient();
    NetworkStream networkStream = new NetworkStream();
    BinaryReader reader;
    BinaryWriter writer;
    string msg = "hh";
    networkStream = tcp.GetStream();
    writer = new BinaryWriter(networkStream);
    writer.Write(msg);//写字符串
    string str=reader.ReadString();//读字符串

  还有StreamReader类和StreamWriter类用的也是比较多,比如读一个txt文档读到末尾,可以调用StreamReader对象的ReadToEnd方法来拿到这个txt的所有字符串内容。另外如果要对发送的数据进行加密的话,有一个CryptoStream类用起来也挺方便的,在创建这个对象时可以直接就指定采用的对称加密算法,比如DES加密算法。解密时也非常方便,只需要使用和加密时相同的密钥创建CryptoStream实例,并在创建实例时指定CryptoStreamMode为读模式,即可拿到解密后的数据。


2018-10-05 10:13:16 kenjianqi1647 阅读数 195

一、前言

1、C# 通过数据流来执行I/O操作。

I/O数据流技术包括:文件操作、文件夹操作(目录操作)、I/O流

 

文件操作:使用File类、FileInto类  实现对文件的基本操作,如判断是否存在 、创建、移动、复制、删除、获取信息等。

文件夹操作:使用Directory类、DirectoryInfo类实现对文件夹的操作、如判断是否存在、创建、移动、删除、遍历等。

I/O操作:读写文件

2、  数据流基本概念

当希望通过网络传输数据,或对文件数据进行操作时,首先需要将数据化成数据流。数据流是对串行传输数据的一种抽象表示。

System.IO下的Stream类是所有数据流的基类,当我们对数据进行逐字节操作时,首先需要将数据转换为数据流。C#数据流主要分为三类:FileStream、MemoryStream、NetworkStream,还有常用的StreamReader、StreamWriter和TextWriter类等。

二、文件操作

1、FIie类

File类支持对文件的基本操作,包括提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建FileStream对象,由于所有的File类的方法都是静态的,所以如果只进行一个操作,那么使用File方法的效率比使用File方法的效率高。

知识补充:

静态方法:C#静态方法是一种特殊的成员方法 它不属于类的某一个具体的实例,而是属于类本身。所以对静态方法不需要首先创建一个类的实例,而是采用类名.静态方法的格式 。

(1)static方法是类中的一个成员方法,属于整个类,即不用创建任何对象也可以直接调用! 
static内部只能出现static变量和其他static方法!而且static方法中还不能使用this....等关键字..因为它是属于整个类! 
(2)静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。 
(3)静态方法和静态变量创建后始终使用同一块内存,而使用实例的方式会创建多个内存. 
(4)C#中的方法有两种:实例方法,静态方法.

静态方法销毁:主进程销毁时,内存中分配的静态方法销毁

2、FileInfo类

FileInfo类与FIie类之间的许多方法的调用都是相同的,但是FileInfo类没有静态方法,该类中的方法仅可以用于实例化对象。

3、使用方法

(1)判断文件是否存在

方一:

File.Exists("C:\\Test.txt");//判读C盘目录下是否存在Test.txt类

方二:

FileInfo finfo = new FileInfo("C:\\Test.txt");//创建文件对象
if(finfo.Exists)  //判断文件是否存在
{
}

三、流

流:程序开发中将输入与输出设备之间的数据传递抽象为流。

(1)不同分类方式:

字节流:操作的数据单元是一个字节。

字符流:操作的数据单元是两个字节或一个字符,因为一个字符占用两个字节。

以内存的角度出发:

输入流:数据从数据源(文件、压缩包、视频等)流入内存的过程。

输出流:数据从内存流出到数据源的过程。

c#中最重要的是FileStream类,它提供了读取和写入文件的方式。

1、FileStream 文件流

    文件流是字节序列的抽象概念,序列化用于将对象的状态存储到文件中。  FileStream类主要用于读写磁盘文件。常用于向磁盘存储数据或读取配置文件。

(1)创建FileStream实例

FileStream fs=new FileStream(string path,FileMode mode,FileAccess access);//文件所在的流程、文件创建的方式、文件的访问权限设置

FileInfo类创建FileStream对象的方法

FileStream fs=File.OpenWrite(@"C:\\File1.txt");//以只写的方式打开C盘目录下的File1.txt
FileStream fs=File.OpenRead(@"C:\\File1.txt");//以只读的方式打开C盘目录下的File1.txt

 (2)流操作文本文件

使用I/O流操作文本文件时主要用到StreamWriter类和StreamReader类。

StreamWriter类:

可以向文本文件中写入字符串,同时负责重要的准换以及处理向FileStream对象写入的工作。

saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";//设置文件保存格式
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
    StreamWriter sw = new StreamWriter(saveFileDialog1.FileName,true);
    sw.WriteLine(textBox1.Text);//向创建的文件中写入流
    sw.Close();//关闭当前写入流
}

StreamReader类:

用来读取文本文档的类。

用法:

  private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt";//设置文件的打开格式
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
            //此处有Bug,因为Windows系统自己创建的记事本文件编码格式为ANSI,而下面读取时用了UTF8编码
            //改正该程序,需要将UTF8编码换成默认编码即可,将Encoding.UTF8替换为Encoding.Default
            StreamReader SReader = new StreamReader(textBox1.Text, Encoding.UTF8);
            textBox2.Text = SReader.ReadToEnd();//将整个流或从流的当前位置到流的结尾作为字符串的读取
        }

2、网络流

NetWorkStream类是专门用来处理服务器与客户端通信的流。它在网络编程中经常使用,主要是用来处理类似Socket、TcpClient和TcpListener这些类中得到的流。C#在System.Net.Sockets命名空间中提供了NetworkStream类用于收发网络数据 。

3、内存流

      MemoryStream类主要用于操作内存中的数据。比如说网络中传输数据时可以用流的形式,当我们收到这些流数据时就可以声明MemoryStream类来存储并且处理它们。

参考:https://www.cnblogs.com/mazhenyu/p/9139321.html

C#数据流

阅读数 869

网络流NetworkStream类提供用于网络访问的基础数据流,NetworkStream实现了通过网络套接字发送和接收数据的标准。NetworkStream支持对网络数据流的同步和异步访问,但不支持对网络数据流的随机访问。Read    NetworkStreamnewStream=newNetworkStream(mysock);    byte[]myByte

博文 来自: oyf421102

C# 数据流 截图

阅读数 313

publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="text/plain";varfiles=context.Request.Files[0];intx=(int)Convert.ToDouble(conte

博文 来自: jiayawei90

C#文件与数据流

阅读数 323

staticvoidfunc1(){     //利用File类判断指定目录中是否包含指定的文件      stringfilepath="e:\\FileText\\Text.txt";      if(File.Exists(filepath)){        Console.WriteLine("文件存在!");

博文 来自: Su_wu

C#中数据流

阅读数 138

概念:流用于对IO的处理,在system.IO的命名空间下有以下一些类:1.BinaryReader/BinaryWriter  用特定的编码将基元数据类型读作二进制值/将二进制中的基元类型写入流并支持用特定的编码写入字符串2.TextReader/Writer   表示可读取有序字符系列的读取器。/表示可以编写一个有序字符系列的编写器(1)StreamReader/Strea...

博文 来自: qq_27032631

C# Xml 数据流

阅读数 454

FileStreamstream=newFileStream("c://个人信息.xml",FileMode.Open);//声明xml变量XmlDataDocumentdoc=newXmlDataDocument();//将xml读取到dataset中doc.DataSet.ReadXml(newStreamReader(stream));//将数据绑定到GridView1if(d

博文 来自: HanYanBin
没有更多推荐了,返回首页