C#实现IP摄像头的方法
本文实例讲述了C#实现IP摄像头的方法。分享给大家供大家参考。具体实现方法如下:
#regionIP摄像头代码
///<summary>
///ip摄像头代码
///</summary>
//视频
privateIntPtrm_hChannel;
privateIntPtrm_hServer,phPlay;
publicdelegatevoidMyInvoke(stringstr);
publicdelegatevoidOutDelegate(IntPtrhSearch,DVSNET_FILE_TIMEpFileTime,DVSNET_FILE_INFOpFileInfo,IntPtrpcontext);
//远程视频处理
publicvoidBeforeRecordFrame(IntPtrhChannel,refRECORD_INFOpRecordInfo,refFRAME_HEADERpFrameHeader,IntPtrpcontext)
{
}
DVSNETClient.FileSearchCallbackFile_SearchCallback;//远程视频回放
publicvoidFileSearchCallback(IntPtrhSearch,refDVSNET_FILE_TIMEpFileTime,refDVSNET_FILE_INFOpFileInfo,IntPtrpcontext)
{
OutText(hSearch,pFileTime,pFileInfo,pcontext);
}
publicvoidOutText(IntPtrhSearch,DVSNET_FILE_TIMEpFileTime,DVSNET_FILE_INFOpFileInfo,IntPtrpcontext)
{
if(lvVideo.InvokeRequired)
{
OutDelegateoutdelegate=newOutDelegate(OutText);
this.BeginInvoke(outdelegate,newobject[]{hSearch,pFileTime,pFileInfo,pcontext});
return;
}
lvVideo.View=View.Details;
lvVideo.FullRowSelect=true;
for(inti=0;i<pFileInfo.sFileName.Count();i++)
{
ListViewItemitem1=newListViewItem(pFileInfo.sFileName,i);
lvVideo.Items.AddRange(newListViewItem[]{item1});
}
}
privatevoidpb_del_video_Click(objectsender,EventArgse)
{
this.Close();
}
publicvoidSearchNotify(intnDevType,stringsServName,stringsServSerial,stringsUrl,IntPtrMacAddr,ushortwWebPort,ushortwDataPort,stringsSubNetMask,stringsGateWay,stringsMultiIp,stringsDNS,ushortwMultiPort,intnChCount,uintbDHCP,IntPtrpcontext)
{
stringstr=Convert.ToString(wDataPort);
//stringstr2=sUrl+"("+str+")";
stringstr2=sUrl;
this.AddText(str2);
}
//赋值给cmbIP
privatevoidAddText(stringstr)
{
MyInvokemethod=newMyInvoke(this.AddTextToCmb);
base.BeginInvoke(method,newobject[]{str});
}
publicvoidAddTextToCmb(stringstr)
{
this.CmbIP.Items.Add(str);
}
//s搜索服务器
privatevoidbtnVideoSearck_Click(objectsender,EventArgse)
{
this.CmbIP.Items.Clear();
errornumerrornum=DVSNETClient.DVSNET_SearchServers(0xbb8,1,newDVSNETClient.SearchNotify(this.SearchNotify),base.Handle,IntPtr.Zero);
if(errornum==errornum.DVSNET_ERR_OK)
{
timerVideo.Enabled=true;
btnVideoSearck.Enabled=false;
}
else
{
}
}
//打开链接
privatevoidbtnVideoOpen_Click(objectsender,EventArgse)
{
stringtext=this.CmbIP.Text;
stringsServIp=text;
intindex=text.IndexOf('(');
if(index!=-1)
{
stringstr3=newstring(text.ToCharArray(),0,index);
sServIp=str3;
}
errornumerrornum=DVSNETClient.DVSNET_OpenServer(this.EdtName.Text,sServIp,Convert.ToUInt16(this.EdtPort.Text),this.EdtUser.Text,this.EdtPassword.Text,refthis.m_hServer,0,0);
this.btnVideoOpen.Enabled=false;//不能点击
if(errornum==errornum.DVSNET_ERR_OK)
{
DVSNET_SERVER_INFOdvsnet_server_info=newDVSNET_SERVER_INFO();
MessageBox.Show("打开链接成功!");
dvsnet_server_info=newDVSNET_SERVER_INFO
{
lStructSize=Marshal.SizeOf(dvsnet_server_info)
};
DVSNETClient.DVSNET_GetServerInfo(this.m_hServer,refdvsnet_server_info);
//this.CmbChannel.Items.Clear();
for(inti=0;i<dvsnet_server_info.nChnNum;i++)
{
intnum3=i+1;
//this.CmbChannel.Items.Add(num3.ToString());
}
//this.CmbChannel.SelectedIndex=0;
}
else
{
MessageBox.Show("打开链接失败,错误码为:"+errornum.ToString());
this.btnVideoOpen.Enabled=true;//不能点击
}
}
//关闭链接
privatevoidbtnVideoClose_Click(objectsender,EventArgse)
{
if(this.m_hChannel!=IntPtr.Zero)
{
MessageBox.Show("请关闭通道!再关闭链接");
}
else
{
if(this.m_hServer!=IntPtr.Zero)
{
DVSNETClient.DVSNET_CloseServer(this.m_hServer);
this.m_hServer=IntPtr.Zero;
this.btnVideoOpen.Enabled=true;//不能点击
}
}
}
//打开通道
IntPtrselServer=IntPtr.Zero;
privatevoidbtnVideoOpenChnnel_Click(objectsender,EventArgse)
{
this.panelVideo.Visible=true;
this.pannelVideoTwo.Visible=false;
if(this.m_hServer!=IntPtr.Zero)
{
this.btnVideoOpenChnnel.Enabled=false;//不能点击
DVSNET_CHANNEL_INFOdvsnet_channel_info=newDVSNET_CHANNEL_INFO();
MessageBox.Show("打开通道成功!请稍候");
dvsnet_channel_info=newDVSNET_CHANNEL_INFO
{
lStructSize=Marshal.SizeOf(dvsnet_channel_info),
nProtocol=0,
hWndDisplay=this.panelVideo.Handle,
bPlayStart=1,
dwStreamNo=0
};
DVSNETClient.DVSNET_OpenChannel(this.m_hServer,Convert.ToUInt16(0),refdvsnet_channel_info,refthis.m_hChannel);
}
else
{
MessageBox.Show("请打开链接!");
}
}
//关闭通道
privatevoidbtnVideoCloseChnnel_Click(objectsender,EventArgse)
{
if(this.m_hChannel!=IntPtr.Zero)
{
DVSNETClient.DVSNET_CloseChannel(this.m_hChannel);
MessageBox.Show("关闭通道成功!请稍候");
this.m_hChannel=IntPtr.Zero;
this.btnVideoOpenChnnel.Enabled=true;//不能点击
}
}
//开始录像
privatevoidbtnVideoOn_Click(objectsender,EventArgse)
{
try
{
stringss=DateTime.Now.ToString().Replace(":","-").Replace("","-").Replace("/","-")+".asf";
if(this.panelVideo.Visible==false)
{
MessageBox.Show("请切换到打开通道");
}
else
{
if(this.m_hChannel!=IntPtr.Zero)
{
this.btnVideoOn.Enabled=false;//不能点击
//DVSNETClient.DVSNET_StartRecord(this.m_hChannel,1,FILETYPE.FILETYPE_ASF,ss,newDVSNETClient.BeforeRecordFrame(this.BeforeRecordFrame),base.Handle);
DVSNETClient.DVSNET_StartRecord(this.m_hChannel,1,FILETYPE.FILETYPE_ASF,ss,Before_RecordFrame,base.Handle);
}
else
{
MessageBox.Show("请打开通道!");
}
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
}
}
//停止录像
privatevoidbtnStopRec_Click(objectsender,EventArgse)
{
DVSNETClient.DVSNET_StopRecord(this.m_hChannel);
MessageBox.Show("已停止录像!");
this.btnVideoOn.Enabled=true;//不能点击
}
//打开本地视频
IntPtrphPlayer;
privatevoidbtnsearch_Click(objectsender,EventArgse)
{
try
{
if(m_hServer!=IntPtr.Zero)
{
stringfName="";
OpenFileDialogopenFileDialog=newOpenFileDialog();
openFileDialog.InitialDirectory=Application.StartupPath;
//注意这里写路径时要用c:\而不是c:
openFileDialog.Filter="文本文件|*.asf|所有文件|*.asf";
openFileDialog.RestoreDirectory=true;
openFileDialog.FilterIndex=1;
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
this.panelVideo.Visible=false;
this.pannelVideoTwo.Visible=true;
this.btnsearch.Enabled=false;
fName=openFileDialog.FileName;
IntPtrhDisplayWnd=pannelVideoTwo.Handle;
phPlayer=(IntPtr)0;
DVSNETClient.DVSPLAYER_ReleaseInstance(phPlayer);//释放
DVSNETClient.DVSPLAYER_Stop(phPlayer);//释放本地
errornumaa=DVSNETClient.DVSPLAYER_CreateInstance(hDisplayWnd,refphPlayer,0);
aa=DVSNETClient.DVSPLAYER_OpenFile(phPlayer,reffName,1);
DVSNETClient.DVSPLAYER_Start(phPlayer);
}
else
{
this.panelVideo.Visible=true;
this.pannelVideoTwo.Visible=false;
}
}
else
{
MessageBox.Show("请打开通道!");
}
}
catch(Exception)
{
throw;
}
}
//停止视频播放
privatevoidbtnhf_Click(objectsender,EventArgse)
{
this.panelVideo.Visible=true;
this.pannelVideoTwo.Visible=false;
IntPtrphPlayer=(IntPtr)0;
DVSNETClient.DVSPLAYER_Stop(phPlayer);
MessageBox.Show("已停止回放录像!");
this.btnsearch.Enabled=true;;
}
//查找远程视频
privatevoidbtnSearchbyName_Click(objectsender,EventArgse)
{
if(m_hServer==IntPtr.Zero)
{
MessageBox.Show("请先打开链接登录服务器!");
return;
}
this.btnSearchbyName.Enabled=false;
DVSNET_FILE_TIMEpStartTime=newDVSNET_FILE_TIME();
pStartTime.lYear=(uint)this.dateTimePicker1.Value.Year;
pStartTime.lMonth=(uint)this.dateTimePicker1.Value.Month;
pStartTime.lDay=(uint)this.dateTimePicker1.Value.Day;
pStartTime.nHour=0;
pStartTime.nMinute=0;
DVSNET_FILE_TIMEpEndTime=newDVSNET_FILE_TIME();
pEndTime.lYear=(uint)this.dateTimePicker1.Value.Year;
pEndTime.lMonth=(uint)this.dateTimePicker1.Value.Month;
pEndTime.lDay=(uint)this.dateTimePicker1.Value.Day;
pEndTime.nHour=23;
pEndTime.nMinute=59;
IntPtrphSearch=IntPtr.Zero;
//if(){}
errornumbak=DVSNETClient.DVSNET_ServerFindFile(m_hServer,0,0,(char)0xff,refpStartTime,refpEndTime,File_SearchCallback,(IntPtr)0,refphSearch);
if(bak!=errornum.DVSNET_ERR_OK)
{
MessageBox.Show("查找远程视频失败!");
}
this.btnSearchbyName.Enabled=true;
}
//鼠标双击事件
privatevoidlvVideo_MouseDoubleClick(objectsender,MouseEventArgse)
{
this.panelVideo.Visible=false;
this.pannelVideoTwo.Visible=true;
stringsa="";
IntPtrhWnd=pannelVideoTwo.Handle;
if(lvVideo.SelectedItems.Count>0)
{//this.listView1.SelectedIndices
sa=lvVideo.SelectedItems[0].Text.ToString();
}
DVSNETClient.DVSPLAYER_Stop(phPlayer);//释放本地回放
DVSNETClient.DVSNET_ClosePlayFile(phPlay);//释放远程
phPlay=IntPtr.Zero;
errornumbak=DVSNETClient.DVSNET_PlayFileByName(m_hServer,sa,1,hWnd,refphPlay);
if(bak==errornum.DVSNET_ERR_OK)
{
UInt32value=0;
DVSNETClient.DVSNET_ControlPlayFile(phPlay,1,0,refvalue);
}
else
{
MessageBox.Show("回放远程文件失败,错误码为"+bak.ToString());
}
}
#endregion
//时间控件1s
privatevoidtimerVideo_Tick(objectsender,EventArgse)
{
intnStatus=0;
errornumerrnum=DVSNETClient.DVSNET_GetSearchStatus(refnStatus);
if(errnum==errornum.DVSNET_ERR_OK)
{
if(nStatus==1)
{
btnVideoSearck.Enabled=true;
timerVideo.Enabled=false;
MessageBox.Show("没有启动搜索");
}
if(nStatus==2)
{
//MessageBox.Show("正在搜索!");
}
if(nStatus==3)
{
timerVideo.Enabled=false;
btnVideoSearck.Enabled=true;
if(this.CmbIP.Items.Count==0)
{
MessageBox.Show("没有找到设备!请接上设备");
}
else
{
MessageBox.Show("已找到设备,请选择IP地址");
}
}
}
else
{
MessageBox.Show("DVSNET_GetSearchStatus失败,错误码为"+errnum.ToString());
}
}
希望本文所述对大家的C#程序设计有所帮助。