c# 通过海康ISAPI 获取人脸数据
时间: 2024-03-06 22:50:57 浏览: 273
要通过海康ISAPI获取人脸数据,需要先进行以下步骤:
1. 确认你已经有了海康设备,并且设备的IP地址、端口号、用户名和密码都正确无误;
2. 通过ISAPI协议访问设备,获取登录sessionID;
3. 通过设备的人脸识别接口,获取人脸数据。
下面是一个C#示例代码,可以帮助你实现上述步骤:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
namespace HikvisionISAPI
{
class Program
{
static void Main(string[] args)
{
//设备信息
string ip = "192.168.1.10";
int port = 80;
string username = "admin";
string password = "12345";
//登录
string sessionID = Login(ip, port, username, password);
//获取人脸数据
string faceData = GetFaceData(ip, port, sessionID);
Console.WriteLine(faceData);
//注销登录
Logout(ip, port, sessionID);
Console.ReadLine();
}
//登录
static string Login(string ip, int port, string username, string password)
{
string url = string.Format("http://{0}:{1}/ISAPI/Security/userCheck", ip, port);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password)));
string postData = "{\"userName\":\"" + username + "\",\"password\":\"" + password + "\"}";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/json";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> dict = (Dictionary<string, object>)serializer.DeserializeObject(result);
string sessionID = dict["sessionID"].ToString();
return sessionID;
}
//获取人脸数据
static string GetFaceData(string ip, int port, string sessionID)
{
string url = string.Format("http://{0}:{1}/ISAPI/Intelligent/FDLib/FDSearch", ip, port);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "Session " + sessionID);
string postData = "{\"searchResultPosition\":0,\"maxResults\":10,\"FDIDList\":[\"1\"],\"faceMatchThreshold\":0.9}";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/json";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
return result;
}
//注销登录
static void Logout(string ip, int port, string sessionID)
{
string url = string.Format("http://{0}:{1}/ISAPI/Security/userLogout", ip, port);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "Session " + sessionID);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
}
}
```
这个示例代码中,我们使用了C#的HttpWebRequest类来发送HTTP请求,获取海康设备的人脸数据。其中,Login方法用于登录设备,GetFaceData方法用于获取人脸数据,Logout方法用于注销登录。你可以根据自己的需求,修改这个示例代码来实现你自己的功能。
阅读全文