如何从网络摄像头捕捉视频?我需要从网络摄像头捕获视频。C#/.NET中是否有任何类可以帮助我解决这个问题。我只对实时数据感兴趣。是否有任何好的C#/.NET书籍可供我学习以加深对语言和平台的理解?这是我用的。你需要第一个类来遍历你的设备:MarshalAs(UnmanagedType.VBByRefStr)]refStringlpszVer,intcbVer);静态ArrayList设备=newArrayList();publicstaticTCamDevice[]GetAllDevices(){StringdName="".PadRight(100);StringdVersion="".PadRight(100);for(shorti=0;i和这个控制凸轮。公共类TCamDevice{privateconstshortWM_CAP=0x400;私人常量intWM_CAP_DRIVER_CONNECT=0x40a;私有常量intWM_CAP_DRIVER_DISCONNECT=0x40b;私人常量intWM_CAP_EDIT_COPY=0x41e;私有常量intWM_CAP_SET_PREVIEW=0x432;私有常量intWM_CAP_SET_OVERLAY=0x433;私有常量intWM_CAP_SET_PREVIEWRATE=0x434;私人常量intWM_CAP_SET_SCALE=0x435;私有常量WS_CHILD=0x40000000;私有常量WS_VISIBLE=0x10000000;[DllImport("avicap32.dll")]protectedstaticexternintcapCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)]refstringlpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,inthWndParent,intnID);[DllImport("user32",EntryPoint="SendMessageA")]protectedstaticexternintSendMessage(inthwnd,intwMsg,intwParam,[MarshalAs(UnmanagedType.AsAny)]objectlParam);[DllImport("user32")]protectedstaticexternintSetWindowPos(inthwnd,inthWndInsertAfter,intx,inty,intcx,intcy,intwFlags);[DllImport("user32")]protectedstaticexternboolDestroyWindow(inthwnd);整数索引;int设备句柄;publicTCamDevice(intindex){this.index=index;}私有字符串_name;publicstringName{get{return_name;}设置{_name=值;}}私有字符串_version;publicstringVersion{get{return_version;}设置{_version=值;}}publicoverridestringToString(){returnthis.Name;}//////初始化设备//////窗口高度///窗口宽度///附加设备的控制句柄publicvoidInit(intwindowHeight,intwindowWidth,inthandle){stringdeviceIndex=Convert.ToString(this.index);deviceHandle=capCreateCaptureWindowA(refdeviceIndex,WS_VISIBLE|WS_CHILD,0,0,windowWidth,windowHeight,handle,0);如果(SendMessage(deviceHandle,WM_CAP_DRIVER_CONNECT,this.index,0)>0){SendMessage(deviceHandle,WM_CAP_SET_SCALE,-1,0);森dMessage(deviceHandle,WM_CAP_SET_PREVIEWRATE,0x42,0);SendMessage(deviceHandle,WM_CAP_SET_PREVIEW,-1,0);SetWindowPos(deviceHandle,1,0,0,windowWidth,windowHeight,6);}}//////在控件中显示网络摄像头预览//////控件附加网络摄像头预览publicvoidShowWindow(global::System.Windows.Forms.ControlwindowsControl){Init(windowsControl.Height,windowsControl.Width,windowsControl.Handle.ToInt32());}//////停止网络摄像头并销毁句柄///publicvoidStop(){SendMessage(deviceHandle,WM_CAP_DRIVER_DISCONNECT,this.index,0);销毁窗口(设备句柄);ShowWindow以PictureBox作为参数我建议你使用第三方库。这将是最好的解决方案,而不是发明自己的自行车。在这里,我使用了AForge.Net。虽然它在性能方面存在一些问题,但当性能成为一个关键问题时,我自己调整了库。AForge.Net代码是开源的,你可以根据自己的需要进行调整。至于书籍,您绝对应该阅读JeffreyRichter的“CLRviaC#”和JohnSkeet的“C#inDepth”。您可以使用MicrosoftExpressionEncoder4SP2。这是一个很棒的库,您可以将其添加到您的项目中并获得实时预览、快照和视频录制。更多详细信息包括MassimoConti的示例项目:如何在C#中使用带有.NETFramework4.0和MicrosoftExpressionEncoder4的网络摄像头使用WebCam_Capture.dll捕获照片和视频。这里的链接是链接的源代码以上是C#学习教程:如何从网络摄像头捕捉视频?如果分享的所有内容对您有用,需要了解更多C#学习教程,希望您多多关注---本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
