C# 外接(网口)双摄像头视频获取

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 外接(网口)双摄像头视频获取相关的知识,希望对你有一定的参考价值。

【注意事项】
------------------------------------
1. 更新设备网络SDK时,SDK开发包【库文件】里的HCNetSDK.dll、HCCore.dll、PlayCtrl.dll、SuperRender.dll、AudioRender.dll、HCNetSDKCom文件夹、ssleay32.dll、libeay32.dll、hlog.dll、hpr.dll、zlib1.dll、log4cxx.properties等文件均要加载到程序里面,【HCNetSDKCom文件夹】(包含里面的功能组件dll库文件)需要和HCNetSDK.dll、HCCore.dll一起加载,放在同一个目录下,且HCNetSDKCom文件夹名不能修改。

2. 如果自行开发软件不能正常实现相应功能,而且程序没有指定加载的dll库路径,请在程序运行的情况下尝试删除HCNetSDK.dll。如果可以删除,说明程序可能调用到系统盘Windows->System32目录下的dll文件,建议删除或者更新该目录下的相关dll文件;如果不能删除,dll文件右键选择属性确认SDK库版本。

3. 如按上述步骤操作后还是不能实现相应功能,请根据NET_DVR_GetLastError返回的错误号判断原因。

4.适用型号:DS-2CD2D15DWD 2.8MM,DS-2CD6424FWD-20 3.7MM,DS-2CD6425FWD-20  2.8MM B

5.dll和sdk可以上海康官网下载:https://www.hikvision.com/cn/download_61.html

 

Form1.cs:

using System;using System.Threading.Tasks;using System.Windows.Forms;
namespace WindowsFormsApp1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); init(); } public IntPtr PictureDev1Cam { get { return pictureBox1.Handle; } } public IntPtr PictureDev2Cam { get { return pictureBox2.Handle; } } IntPtr Cam1Handle = IntPtr.Zero; IntPtr Cam2Handle = IntPtr.Zero; Cam Camera1 = new Cam(); Cam Camera2 = new Cam(); static private string Cam1IP = "192.168.1.64"; static private string Cam2IP = "192.168.1.61"; void init() { Cam1Handle = PictureDev1Cam; Cam2Handle = PictureDev2Cam; } private void ConnectCamera(int whitch) { string userName = "admin"; string password = "a123456."; int PortCamera = 8000; if (whitch == 1) { labelCamera1Status.Text = "摄像头连接中..."; Task.Run(() => { if (!Camera1.ConnectCamera(Cam1IP, PortCamera, userName, password)) { this.Invoke((Action)delegate { labelCamera1Status.Text = "摄像头连接失败"; }); } else { this.Invoke((Action)delegate { labelCamera1Status.Text = ""; }); Camera1.Preview(Cam1Handle); Camera1.AdjustMirrorPara(1); Cam1ReconnectTimer.Stop(); } }); } else { labelCamera2Status.Text = "摄像头连接中..."; Task.Run(() => { if (!Camera2.ConnectCamera(Cam2IP, PortCamera, userName, password)) { this.Invoke((Action)delegate { labelCamera2Status.Text = "摄像头连接失败"; }); } else { this.Invoke((Action)delegate { labelCamera2Status.Text = ""; }); Camera2.Preview(Cam2Handle); Camera2.AdjustMirrorPara(1); Cam2ReconnectTimer.Stop(); } }); } } private void Cam1ReconnectTimer_Tick(object sender, EventArgs e) { ConnectCamera(1);
} private void Cam2ReconnectTimer_Tick(object sender, EventArgs e) { ConnectCamera(2); } private void Form1_Load(object sender, EventArgs e) { } }}

cam:

using System;using System.IO;using System.Runtime.InteropServices;

namespace WindowsFormsApp1{ class Cam { private bool initSDK = false; private uint lastErr = 0; private string debugMessage = ""; private bool err = false; private Int32 userID = -1; public bool Isconnected { get; private set; } UInt32 dwReturn = 0; Int32 nSize = 0; IntPtr ptrPicCfg; private CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX camPara = new CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX(); private CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30(); private Int32 realPlayHandle = -1; public bool ConnectCamera(string deviceIP, int port, string userName, string password) {
initSDK = CHCNetSDK.NET_DVR_Init(); if (initSDK == false) { lastErr = CHCNetSDK.NET_DVR_GetLastError(); debugMessage = "初始化过程失败, 错误码:" + lastErr;
err = true; return false; } else {
CHCNetSDK.NET_DVR_SetLogToFile(3, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SdkLog\\"), true); }
CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();

userID = CHCNetSDK.NET_DVR_Login_V30(deviceIP, port, userName, password, ref DeviceInfo); if (userID < 0) { lastErr = CHCNetSDK.NET_DVR_GetLastError(); debugMessage = "注册失败, 错误码:" + lastErr; err = true;
return false; }

Isconnected = true; return true; } public void AdjustMirrorPara(byte type) { dwReturn = 0; nSize = Marshal.SizeOf(camPara); ptrPicCfg = Marshal.AllocHGlobal(nSize); Marshal.StructureToPtr(camPara, ptrPicCfg, false); if (CHCNetSDK.NET_DVR_GetDVRConfig(userID, 3368, DeviceInfo.byStartChan, ptrPicCfg, (uint)nSize, ref dwReturn)) { camPara = (CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX)Marshal.PtrToStructure(ptrPicCfg, typeof(CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX)); Marshal.FreeHGlobal(ptrPicCfg); camPara.byMirror = type;
ptrPicCfg = Marshal.AllocHGlobal(nSize); Marshal.StructureToPtr(camPara, ptrPicCfg, false);

if (!CHCNetSDK.NET_DVR_SetDVRConfig(userID, 3369, DeviceInfo.byStartChan, ptrPicCfg, (uint)nSize)) { lastErr = CHCNetSDK.NET_DVR_GetLastError(); debugMessage = "设置镜像关闭失败, 错误码:" + lastErr; err = true;
} } else { lastErr = CHCNetSDK.NET_DVR_GetLastError(); debugMessage = "获取参数失败, 错误码:" + lastErr; err = true;
} Marshal.FreeHGlobal(ptrPicCfg); } public void Preview(IntPtr handle) { CHCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO(); previewInfo.hPlayWnd = handle;//预览窗口 previewInfo.lChannel = 1;//(int)DeviceInfo.byStartChan;//预览的设备通道 previewInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推 previewInfo.dwLinkMode = 0;//连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP previewInfo.bBlocked = true; //0- 非阻塞取流,1- 阻塞取流 previewInfo.dwDisplayBufNum = 0; //播放库播放缓冲区最大缓冲帧数
//CHCNetSDK.REALDATACALLBACK RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);//预览实时流回调函数 IntPtr pUser = new IntPtr();//用户数据
//打开预览 Start live view realPlayHandle = CHCNetSDK.NET_DVR_RealPlay_V40(userID, ref previewInfo, null/*RealData*/, pUser); if (realPlayHandle < 0) { lastErr = CHCNetSDK.NET_DVR_GetLastError(); debugMessage = "预览失败, 错误码:" + lastErr; //预览失败,输出错误号 err = true; //MessageBox.Show(str); return; } } }}

CHCNetSDK.cs

这个太长,需要的可以上我的github下载,省略,我的账号zyr365

工程文件

bin目录

C# 外接(网口)双摄像头视频获取

定时器:

运行结果:


----------------------------------------------------------
如果觉得有用,麻烦点击 在看 ,让更多志同道合的伙伴加入我们的交流群,一起共同进步!                                                                                                              


以上是关于C# 外接(网口)双摄像头视频获取的主要内容,如果未能解决你的问题,请参考以下文章

外接摄像头怎么打开?

飞凌干货|A40i平台双网口怎么配?教你详细配置方法

TSINGSEE青犀视频通道FFmpeg获取视频片段的方法介绍

A40i平台双网口怎么配?教你详细配置方法

opencv学习-视频加载与摄像头调用

C# 如何采集 视频和音频 如何调用摄像头获取图片