一、基本需求信息
根据项目需求需要接入海康相机SDK进行测温获取数值再之后截图,相机是热成像。在网上搜集了大量资料,有用的却很少,不如自己研究一下。内容全是干货,最后附加自己写的脚本,随意调用即可。。。。(如果有问题可联系我)
二、基本流程
(配置SDK部分,这里我就不描述了。可参考我其他海康系列文档…)
-
初始化SDK
主要使用函数:NET_DVR_Init -
登录
主要使用函数:NET_DVR_Login_V40 -
进行测温
主要使用函数:NET_DVR_StartRemoteConfig、NET_DVR_StopRemoteConfig
注意说明:测温函数需要在已经登录完毕返回完数值之后再进行测温函数操作,所以我直接写了个协程等待方法。
停止测温:
测温方法回调函数:
文章来源:https://www.toymoban.com/news/detail-834551.html -
退出(注销登录 释放内存)
上脚本!文章来源地址https://www.toymoban.com/news/detail-834551.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using PreviewDemo;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine.UI;
public class HK_Camera_Tool : MonoSingleton<HK_Camera_Tool>
{
private uint iLastErr = 0;
public int m_lUserID = -1; //登录的返回值
private CHCNetSDK.NET_DVR_USER_LOGIN_INFO struLogInfo;
private CHCNetSDK.NET_DVR_DEVICEINFO_V40 DeviceInfo;
CHCNetSDK.LOGINRESULTCALLBACK LoginCallBack = null;
private int ret; //NET_DVR_StartRemoteConfig 测温返回的句柄
public static string fMaxTemperature;//最高温度值
#region 海康SDK方法
/// <summary>
/// 初始化
/// </summary>
public void _Init()
{
bool m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
Debug.Log("初始化失败!");
return;
}
else
{
Debug.Log("初始化成功!");
CHCNetSDK.NET_DVR_SetLogToFile(1, "C:\\SdkLogUnity\\", true);
}
}
/// <summary>
/// 登录
/// </summary>
/// <param name="iP"></param>
/// <param name="port"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public void _Login(string iP, string port, string userName, string password)
{
struLogInfo = new CHCNetSDK.NET_DVR_USER_LOGIN_INFO();
if (m_lUserID < 0)
{
//从textBox文本框读取字符串,转成byte数组之后,赋值给结构体对象中对应字段
//设备IP地址或者域名
byte[] byIP = System.Text.Encoding.Default.GetBytes(iP);
struLogInfo.sDeviceAddress = new byte[129];
byIP.CopyTo(struLogInfo.sDeviceAddress, 0);
//设备用户名
byte[] byUserName = System.Text.Encoding.Default.GetBytes(userName);
struLogInfo.sUserName = new byte[64];
byUserName.CopyTo(struLogInfo.sUserName, 0);
//设备密码
byte[] byPassword = System.Text.Encoding.Default.GetBytes(password);
struLogInfo.sPassword = new byte[64];
byPassword.CopyTo(struLogInfo.sPassword, 0);
struLogInfo.wPort = ushort.Parse(port);//设备服务端口号 ///改成自己的 这边注意视频流554 等不是端口 ,端口需要进海康相机自带平台去看一下 8008 8000
struLogInfo.bUseAsynLogin = true; //是否异步登录:0- 否,1- 是
DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();
//登录设备 Login the device
m_lUserID = CHCNetSDK.NET_DVR_Login_V40(ref struLogInfo, ref DeviceInfo);
if (m_lUserID < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
Debug.Log("登陆失败_NET_DVR_Login_V40 failed, error code= " + iLastErr);//登录失败,输出错误号\
return;
}
else
{
Debug.Log("登陆成功!" + "返回iD:" + m_lUserID);
}
}
else
{
//注销登录 Logout the device
if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
Debug.LogWarning("注销登录:" + iLastErr);
return;
}
m_lUserID = -1;
}
}
/// <summary>
/// 测量最高温度 协程版本
/// </summary>
/// <returns></returns>
public IEnumerator ThermometrySetting()
{
Debug.Log("进入协程");
yield return new WaitForSeconds(1f);
Debug.Log("已延迟1s");
//配置测温
var size = Marshal.SizeOf(typeof(CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND));
CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND struThermCond = new CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
struThermCond.dwSize = (uint)size;
struThermCond.byRuleID = 0; //规则ID,0代表获取全部规则,具体规则ID从1开始
struThermCond.dwChan = 2;// dwChannel; //从1开始,0xffffffff代表获取全部通道 有两个相机 热成像仪在通道2上所以选择通道2
IntPtr pCond = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(struThermCond, pCond, false);
ret = CHCNetSDK.NET_DVR_StartRemoteConfig(m_lUserID, 3629, pCond, size, Callback, IntPtr.Zero);
if (ret < 0)
{
Debug.Log("配置测温失败, 错误代码:" + CHCNetSDK.NET_DVR_GetLastError()); //登录失败,输出错误号
}
yield return new WaitForSeconds(1f);
if (CHCNetSDK.NET_DVR_StopRemoteConfig(ret))
{
StopCoroutine(ThermometrySetting());
Debug.Log("已停止测温数据");
}
else
{
Debug.Log("停止测温数据出错:" + CHCNetSDK.NET_DVR_GetLastError());
}
}
/// <summary>
/// 开始测量最高温度
/// </summary>
public void Start_ThermometrySetting()
{
var size = Marshal.SizeOf(typeof(CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND));
CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND struThermCond = new CHCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
struThermCond.dwSize = (uint)size;
struThermCond.byRuleID = 0; //规则ID,0代表获取全部规则,具体规则ID从1开始
struThermCond.dwChan = 2;// dwChannel; //从1开始,0xffffffff代表获取全部通道 有两个相机 热成像仪在通道2上所以选择通道2
IntPtr pCond = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(struThermCond, pCond, false);
ret = CHCNetSDK.NET_DVR_StartRemoteConfig(m_lUserID, 3629, pCond, size, Callback, IntPtr.Zero);
if (ret < 0)
{
Debug.Log("配置测温失败, 错误代码:" + CHCNetSDK.NET_DVR_GetLastError()); //登录失败,输出错误号
}
}
/// <summary>
/// 停止测量最高温度
/// </summary>
public void Stop_ThermometrySetting()
{
if (CHCNetSDK.NET_DVR_StopRemoteConfig(ret))
{
StopCoroutine(ThermometrySetting());
Debug.Log("已停止测温数据");
}
else
{
Debug.Log("停止测温数据出错:" + CHCNetSDK.NET_DVR_GetLastError());
}
}
/// <summary>
/// 测温方法- 回调函数
/// </summary>
/// <param name="dwType"></param>
/// <param name="lpBuffer"></param>
/// <param name="dwBufLen"></param>
/// <param name="pUserData"></param>
private void Callback(uint dwType, IntPtr lpBuffer, uint dwBufLen, IntPtr pUserData)
{
Debug.Log("开始测温...");
var retMes = Marshal.PtrToStructure<CHCNetSDK.NET_DVR_THERMOMETRY_UPLOAD>(lpBuffer);
// Debug.Log("获取到温度数值:"+retMes.struLinePolygonThermCfg.fMaxTemperature.ToString("F1"));
fMaxTemperature = retMes.struLinePolygonThermCfg.fMaxTemperature.ToString("F1");
//Debug.Log("温度静态信息输出:" + fMaxTemperature);
Debug.Log("停止测温数据中...");
}
/// <summary>
/// 画面截图
/// </summary>
/// <param name="登录的返回值"></param>
/// <param name="通道号"></param>
/// <param name="储存路径和名称"></param>
public void JPEGEvent(int lUserID, int lChannel, string pathAndName)
{
//图片保存路径和文件名 the path and file name to save
//string sJpegPicFileName = Application.streamingAssetsPath + "/JPEG_test.jpg";
// int lChannel = 1; //通道号 Channel number
CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
lpJpegPara.wPicQuality = 0; //图像质量 Image quality
lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 2- 4CIF,0xff- Auto(使用当前码流分辨率),抓图分辨率需要设备支持,更多取值请参考SDK文档
//JPEG抓图 Capture a JPEG picture
if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(lUserID, lChannel, ref lpJpegPara, pathAndName))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
Debug.Log("抓图报错:" + iLastErr);
return;
}
else
{
Debug.Log("截图已保存:" + pathAndName);
}
return;
}
/// <summary>
/// 退出
/// </summary>
public void _Disable()
{
Debug.Log("用户注销...");
CHCNetSDK.NET_DVR_Logout(m_lUserID);
m_lUserID = -1;
CHCNetSDK.NET_DVR_Cleanup();
Debug.Log("SDK资源卸载...");
}
#endregion
}
到了这里,关于Unity接入海康SDK—热成像测温篇(最简单且详细)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!