用海康ISAPI接口c#生成一个HpptCliant类 , 要求 接口进行登录 视频控制显示,使用长连接获取设备中所有的人脸库 人脸库中添加人脸。 删除人脸,控制设备
时间: 2023-11-28 14:48:40 浏览: 128
好的,这个需求比较复杂,需要用到海康的ISAPI接口,以及C#语言和网络编程的知识。我会尽力解释每一步的实现方式,但需要您有一定的编程基础。
首先,需要下载海康的SDK和ISAPI接口文档,并在项目中引用相关的DLL文件。接下来,我们可以编写一个类来封装ISAPI接口的调用。
```csharp
public class HikvisionISAPI
{
private string baseURL; // ISAPI接口的根URL
private string username; // 登录账号
private string password; // 登录密码
private string sessionID; // 登录会话ID
public HikvisionISAPI(string baseURL, string username, string password)
{
this.baseURL = baseURL;
this.username = username;
this.password = password;
}
// 登录
public bool Login()
{
string url = baseURL + "/api/system/user/login";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string requestData = "{\"userName\":\"" + username + "\",\"password\":\"" + password + "\"}";
byte[] postData = Encoding.UTF8.GetBytes(requestData);
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string responseData = reader.ReadToEnd();
JObject jsonObj = JObject.Parse(responseData);
sessionID = (string)jsonObj["sessionID"];
return true;
}
}
}
catch (WebException ex)
{
// 登录失败
return false;
}
return false;
}
// 获取所有人脸库
public List<string> GetAllFaceLibraries()
{
string url = baseURL + "/api/resource/facelib";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json;charset=UTF-8";
request.Headers.Add("Cookie", "session=" + sessionID);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string responseData = reader.ReadToEnd();
JObject jsonObj = JObject.Parse(responseData);
JArray faceLibs = (JArray)jsonObj["FaceLib"];
List<string> result = new List<string>();
foreach (JToken faceLib in faceLibs)
{
result.Add((string)faceLib["name"]);
}
return result;
}
}
}
catch (WebException ex)
{
// 获取失败
}
return null;
}
// 添加人脸
public bool AddFace(string faceLibName, string faceID, string faceData)
{
string url = baseURL + "/api/resource/facelib/" + faceLibName + "/faceData/" + faceID;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.ContentType = "application/json;charset=UTF-8";
request.Headers.Add("Cookie", "session=" + sessionID);
string requestData = "{\"FDID\":\"" + faceID + "\",\"faceData\":\"" + faceData + "\"}";
byte[] postData = Encoding.UTF8.GetBytes(requestData);
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
return true;
}
}
catch (WebException ex)
{
// 添加失败
}
return false;
}
// 删除人脸
public bool DeleteFace(string faceLibName, string faceID)
{
string url = baseURL + "/api/resource/facelib/" + faceLibName + "/faceData/" + faceID;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "DELETE";
request.ContentType = "application/json;charset=UTF-8";
request.Headers.Add("Cookie", "session=" + sessionID);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
return true;
}
}
catch (WebException ex)
{
// 删除失败
}
return false;
}
// 控制设备
// TODO: 实现设备控制的方法
}
```
这个类中实现了登录、获取所有人脸库、添加人脸、删除人脸等方法。其中,登录时需要向ISAPI接口发送账号和密码,如果登录成功则会返回一个会话ID,后续的操作都需要在该会话ID下进行。获取所有人脸库时需要向接口发送GET请求,并将会话ID放在请求头中。添加人脸和删除人脸也类似,需要向接口发送PUT和DELETE请求。
需要注意的是,此处只是示例代码,并未实现完整的功能。控制设备的部分需要根据具体的需求实现。
接下来,我们可以编写一个测试程序来使用这个类。在测试程序中,我们可以先创建一个HikvisionISAPI对象,并调用Login方法进行登录。登录成功后,我们可以调用GetAllFaceLibraries方法获取所有人脸库的名称,并将其输出。然后,我们可以调用AddFace方法向某个人脸库中添加一个人脸。最后,我们可以调用DeleteFace方法删除这个人脸。
```csharp
static void Main(string[] args)
{
string baseURL = "http://192.168.1.100:8080"; // ISAPI接口的根URL
string username = "admin"; // 登录账号
string password = "password"; // 登录密码
HikvisionISAPI isapi = new HikvisionISAPI(baseURL, username, password);
if (isapi.Login())
{
Console.WriteLine("登录成功");
List<string> faceLibs = isapi.GetAllFaceLibraries();
Console.WriteLine("所有人脸库:");
foreach (string faceLib in faceLibs)
{
Console.WriteLine(faceLib);
}
string faceLibName = "test"; // 人脸库名称
string faceID = "1"; // 人脸ID
string faceData = "base64encodedfaceimage"; // 人脸数据,需要进行Base64编码
if (isapi.AddFace(faceLibName, faceID, faceData))
{
Console.WriteLine("添加人脸成功");
}
else
{
Console.WriteLine("添加人脸失败");
}
if (isapi.DeleteFace(faceLibName, faceID))
{
Console.WriteLine("删除人脸成功");
}
else
{
Console.WriteLine("删除人脸失败");
}
}
else
{
Console.WriteLine("登录失败");
}
}
```
这个测试程序中,我们先创建一个HikvisionISAPI对象,并传入ISAPI接口的根URL、登录账号和密码。然后,我们调用Login方法进行登录,如果登录成功则输出“登录成功”,否则输出“登录失败”。接下来,我们调用GetAllFaceLibraries方法获取所有人脸库的名称,并将其输出。然后,我们调用AddFace方法向某个人脸库中添加一个人脸,并输出添加结果。最后,我们调用DeleteFace方法删除这个人脸,并输出删除结果。
需要注意的是,此处的人脸数据需要进行Base64编码。另外,此处的IP地址、账号和密码等信息需要根据实际情况修改。
阅读全文