帮我用C#写一个海康采集图像再传给VisionPro窗口的类
时间: 2024-03-21 18:41:53 浏览: 230
好的,以下是一个简单的C#类,用于从海康相机采集图像,并将图像传递给VisionPro窗口显示:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HCNetSDK;
using System.Drawing;
using System.Drawing.Imaging;
using VisionPro;
namespace HikvisionVisionPro
{
public class HikvisionCamera
{
private IntPtr m_lRealHandle = IntPtr.Zero;
private CHCNetSDK.NET_DVR_DEVICEINFO_V30 m_struDeviceInfo;
private CHCNetSDK.NET_DVR_PREVIEWINFO m_struPreviewInfo;
private uint m_iLastErr = 0;
private bool m_bInitSDK = false;
private bool m_bRecord = false;
private bool m_bSaveRealData = false;
private int m_iPlayHandle = -1;
private int m_iPort = -1;
private int m_iWidth = 0;
private int m_iHeight = 0;
private byte[] m_bySaveRealData = new byte[1024 * 1024 * 100];
private int m_iSaveRealDataSize = 0;
private VDisplay m_display;
public HikvisionCamera()
{
m_struDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
m_struPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
}
public bool InitSDK()
{
bool result = CHCNetSDK.NET_DVR_Init();
if (result == false)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("SDK初始化失败,错误码:" + m_iLastErr);
return false;
}
else
{
m_bInitSDK = true;
return true;
}
}
public bool Login(string strIP, ushort wPort, string strUsername, string strPassword)
{
CHCNetSDK.NET_DVR_DEVICEINFO_V30 struDeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
int iUserID = CHCNetSDK.NET_DVR_Login_V30(strIP, wPort, strUsername, strPassword, ref struDeviceInfo);
if (iUserID < 0)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("登录失败,错误码:" + m_iLastErr);
return false;
}
else
{
m_struDeviceInfo = struDeviceInfo;
return true;
}
}
public bool StartPreview(IntPtr hWnd)
{
m_struPreviewInfo.hPlayWnd = hWnd;
m_struPreviewInfo.lChannel = 1;
m_struPreviewInfo.dwStreamType = 0; // 0-主码流,1-子码流
m_struPreviewInfo.dwLinkMode = 0; // 0-TCP模式,1-UDP模式,2-多播模式,3-RTP模式,4-RTP/RTSP,5-RSTP/HTTP
m_struPreviewInfo.bBlocked = true;
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(0, ref m_struPreviewInfo, null, IntPtr.Zero);
if (m_lRealHandle < 0)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("预览失败,错误码:" + m_iLastErr);
return false;
}
else
{
return true;
}
}
public bool StopPreview()
{
if (m_lRealHandle >= 0)
{
if (CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle))
{
m_lRealHandle = IntPtr.Zero;
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("停止预览失败,错误码:" + m_iLastErr);
return false;
}
}
else
{
return true;
}
}
public bool CaptureImage(string strSavePath)
{
if (m_lRealHandle < 0)
{
MessageBox.Show("请先启动预览!");
return false;
}
CHCNetSDK.NET_DVR_JPEGPARA strPicpara = new CHCNetSDK.NET_DVR_JPEGPARA();
strPicpara.wPicQuality = 0;
strPicpara.wPicSize = 0;
bool result = CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lRealHandle, 1, ref strPicpara, strSavePath);
if (result == false)
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("抓图失败,错误码:" + m_iLastErr);
return false;
}
else
{
return true;
}
}
public bool StartRecord(string strSavePath)
{
if (m_lRealHandle < 0)
{
MessageBox.Show("请先启动预览!");
return false;
}
if (m_bRecord == true)
{
MessageBox.Show("正在录像,请先停止录像!");
return false;
}
if (m_bSaveRealData == true)
{
MessageBox.Show("正在保存实时数据,请先停止保存!");
return false;
}
m_bRecord = true;
if (CHCNetSDK.NET_DVR_SaveRealData(m_lRealHandle, strSavePath))
{
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("启动录像失败,错误码:" + m_iLastErr);
m_bRecord = false;
return false;
}
}
public bool StopRecord()
{
if (m_bRecord == false)
{
MessageBox.Show("没有在录像!");
return false;
}
m_bRecord = false;
if (CHCNetSDK.NET_DVR_StopSaveRealData(m_lRealHandle))
{
return true;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("停止录像失败,错误码:" + m_iLastErr);
return false;
}
}
public void SetDisplay(VDisplay display)
{
m_display = display;
}
public void StartDisplay()
{
if (m_iPlayHandle > 0)
{
MessageBox.Show("正在播放,请先停止!");
return;
}
if (m_iPort < 0)
{
m_iPort = CHCNetSDK.NET_DVR_GetRealPlayerIndex();
}
if (m_display != null)
{
m_display.Start();
}
if (CHCNetSDK.NET_DVR_RigisterDrawFun(m_iPort, DrawFun, IntPtr.Zero))
{
if (CHCNetSDK.NET_DVR_PlayBackByPreview(m_lRealHandle, m_iPort))
{
m_iPlayHandle = m_iPort;
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("启动播放失败,错误码:" + m_iLastErr);
}
}
else
{
m_iLastErr = CHCNetSDK.NET_DVR_GetLastError();
MessageBox.Show("注册回调函数失败,错误码:" + m_iLastErr);
}
}
public void StopDisplay()
{
if (m_iPlayHandle > 0)
{
CHCNetSDK.NET_DVR_StopPlayBack(m_iPlayHandle);
m_iPlayHandle = -1;
}
if (m_display != null)
{
m_display.Stop();
}
}
private void DrawFun(int lRealHandle, IntPtr hDc, uint dwUser)
{
if (m_display != null)
{
Bitmap bmp = GetBitmap(lRealHandle);
m_display.DrawBitmap(bmp);
}
}
private Bitmap GetBitmap(int lRealHandle)
{
IntPtr pBitmap = IntPtr.Zero;
CHCNetSDK.NET_DVR_GetBitmap(lRealHandle, ref pBitmap);
Bitmap bmp = Bitmap.FromHbitmap(pBitmap);
DeleteObject(pBitmap);
return bmp;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
static extern bool DeleteObject(IntPtr hObject);
}
}
```
使用方法:
1. 在你的项目中添加VisionPro和海康相机SDK的引用,并将HCNetSDK.dll文件复制到你的项目目录下。
2. 创建一个HikvisionCamera对象,并调用InitSDK方法初始化SDK。
```csharp
HikvisionCamera camera = new HikvisionCamera();
camera.InitSDK();
```
3. 调用Login方法登录相机。
```csharp
string strIP = "192.168.1.64";
ushort wPort = 8000;
string strUsername = "admin";
string strPassword = "123456";
camera.Login(strIP, wPort, strUsername, strPassword);
```
4. 调用StartPreview方法启动预览。
```csharp
IntPtr hWnd = pictureBox1.Handle;
camera.StartPreview(hWnd);
```
5. 调用StartDisplay方法启动图像显示。
```csharp
VDisplay display = new VDisplay(pictureBox1.Handle);
camera.SetDisplay(display);
camera.StartDisplay();
```
6. 调用StopDisplay方法停止图
阅读全文