使用C#帮我用海康SDK写一个控制海康线阵相机取像的winform程序源代码
时间: 2023-12-12 21:02:14 浏览: 150
c#winform程序源码
5星 · 资源好评率100%
好的,以下是使用C#和WinForm开发的控制海康线阵相机采集图像的示例程序源代码,需要使用海康线阵相机SDK:
```c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using HCNetSDK;
namespace CameraSample
{
public partial class Form1 : Form
{
//相机参数
private int m_lUserID = -1; //登录用户ID
private int m_lRealPlayHandle = -1; //预览句柄
private int m_lCaptureHandle = -1; //抓图句柄
private uint m_dwPicSize = 0; //抓图大小
private IntPtr m_pPicBuffer = IntPtr.Zero; //抓图缓存
//回调函数
private HCNetSDK.REALDATACALLBACK m_RealDataCallBack = null;
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//初始化SDK
if (!HCNetSDK.NET_DVR_Init())
{
MessageBox.Show("初始化SDK失败!错误代码:" + HCNetSDK.NET_DVR_GetLastError(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//登录设备
HCNetSDK.NET_DVR_DEVICEINFO_V30 struDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
m_lUserID = HCNetSDK.NET_DVR_Login_V30(txtIP.Text, Convert.ToInt16(txtPort.Text), txtUserName.Text, txtPassword.Text, ref struDeviceInfo);
if (m_lUserID < 0)
{
MessageBox.Show("登录设备失败!错误代码:" + HCNetSDK.NET_DVR_GetLastError(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
HCNetSDK.NET_DVR_Cleanup();
return;
}
//设置相机参数
HCNetSDK.NET_DVR_SetConnectTime(2000, 1);
HCNetSDK.NET_DVR_SetReconnect(10000, true);
//开启预览
HCNetSDK.NET_DVR_PREVIEWINFO struPlayInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
struPlayInfo.lChannel = 1;
struPlayInfo.hPlayWnd = IntPtr.Zero;
struPlayInfo.dwStreamType = 0;
struPlayInfo.dwLinkMode = 0;
m_lRealPlayHandle = HCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref struPlayInfo, null, IntPtr.Zero);
if (m_lRealPlayHandle < 0)
{
MessageBox.Show("开启预览失败!错误代码:" + HCNetSDK.NET_DVR_GetLastError(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
HCNetSDK.NET_DVR_Logout(m_lUserID);
HCNetSDK.NET_DVR_Cleanup();
return;
}
//设置抓图回调函数
m_RealDataCallBack = new HCNetSDK.REALDATACALLBACK(g_RealDataCallBack_V30);
HCNetSDK.NET_DVR_SetCapturePictureMode(0); //设置为BMP格式
HCNetSDK.NET_DVR_SetCapturePictureMode_JPEG(100); //设置JPEG图像质量
HCNetSDK.NET_DVR_SetRealDataCallBack(m_lRealPlayHandle, m_RealDataCallBack, IntPtr.Zero);
}
private void btnCapture_Click(object sender, EventArgs e)
{
//抓图
HCNetSDK.NET_DVR_JPEGPARA struJpegPara = new HCNetSDK.NET_DVR_JPEGPARA();
struJpegPara.wPicSize = 0; //0: 2048x1536
struJpegPara.wPicQuality = 0; //0-最好,1-较好,2-一般
m_lCaptureHandle = HCNetSDK.NET_DVR_CaptureJPEGPicture(m_lRealPlayHandle, 1, ref struJpegPara, "test.jpg");
}
private void btnLogout_Click(object sender, EventArgs e)
{
//结束采集
HCNetSDK.NET_DVR_StopRealPlay(m_lRealPlayHandle);
HCNetSDK.NET_DVR_Logout(m_lUserID);
HCNetSDK.NET_DVR_Cleanup();
}
private void g_RealDataCallBack_V30(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser)
{
//处理图像数据
//...
}
}
}
```
以上代码仅供参考,实际使用时需要根据具体需求进行修改和调整。
阅读全文