Java调用海康sdk操作热成像设备获取对应点温度
Java调用海康sdk操作热成像设备获取对应点温度, 海康官方提供有Java版的sdk,遗憾的是里面提供的api比较旧了新版的api需要根据c++版的开发文档自己写对应的java接口和类。这对于不熟悉c++的开发人员比较吃力。下面的代码示例了通过海康的SDK获取热成像画面上某一点的具体温度。目前已经使用Python实现了相应的效果,有需要Python版本的可以留言。 maven依赖
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import lombok.extern.slf4j.Slf4j;
import java.nio.ByteBuffer;
@Slf4j
public class HKNetApp {
private NativeLong lUserID = new NativeLong(-1);
private HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;
static HCNetSDK hCNetSDK = null;
public static synchronized void loadLibrary(String libraryPath){
if(null == hCNetSDK){
hCNetSDK = (HCNetSDK) Native.loadLibrary(libraryPath, HCNetSDK.class);
}
}
public static void main(String[] args) {
loadLibrary("你的sdk地址");
HKNetApp hkNetApp = new HKNetApp();
if (hkNetApp.init() && hkNetApp.login()) {
Float temperature = hkNetApp.getTemperature(new HKPoint(1,1), 126, 100);
log.info("1,1点的温度是,{}", temperature);
}
hkNetApp.logout();
hCNetSDK.NET_DVR_Cleanup();
}
public Boolean init() {
if (!hCNetSDK.NET_DVR_Init()) {
log.error("初始化失败");
return false;
}
return true;
}
public Boolean login() {
if (lUserID.longValue() > -1) {
hCNetSDK.NET_DVR_Logout_V30(lUserID);
}
m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
lUserID = hCNetSDK.NET_DVR_Login_V30("192.168.8.14", (short) 8000, "admin", "a1234567", m_strDeviceInfo);
long userID = lUserID.longValue();
boolean initSuc = hCNetSDK.NET_DVR_SetLogToFile(3, "f:\\sdklog\\", false);
if (userID == -1) {
log.error("登录失败,{}", userID);
return false;
}
return true;
}
public void logout() {
hCNetSDK.NET_DVR_Logout_V30(lUserID);
}
public Float getTemperature(HKPoint hkPoint, int sourceWidth, int sourceHeight) {
boolean bRet = false;
int nErr = 0;
int erro;
HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO m_struThermometryInfo = new HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO();
m_struThermometryInfo.dwSize = m_struThermometryInfo.size();
HCNetSDK.NET_DVR_THERMOMETRY_COND m_struThermometryCond = new HCNetSDK.NET_DVR_THERMOMETRY_COND();
m_struThermometryCond.dwSize = m_struThermometryCond.size();
m_struThermometryCond.dwChannel = 1;
m_struThermometryCond.wPresetNo = 1;
m_struThermometryCond.write();
HCNetSDK.NET_DVR_STD_CONFIG struCfg = new HCNetSDK.NET_DVR_STD_CONFIG();
struCfg.lpCondBuffer = m_struThermometryCond.getPointer();
struCfg.dwCondSize = m_struThermometryCond.size();
struCfg.lpOutBuffer = m_struThermometryInfo.getPointer();
struCfg.dwOutSize = m_struThermometryInfo.size();
HCNetSDK.BYTE_ARRAY m_szStatusBuf = new HCNetSDK.BYTE_ARRAY(4096 * 4);
struCfg.lpStatusBuffer = m_szStatusBuf.getPointer();
struCfg.dwStatusSize = 4096 * 4;
struCfg.byDataType = 0;
bRet = hCNetSDK.NET_DVR_GetSTDConfig(lUserID, 3624, struCfg);
if (!bRet) {
nErr = hCNetSDK.NET_DVR_GetLastError();
log.error("NET_DVR_GetSTDConfig失败,{}", nErr);
return 0f;
}
m_struThermometryInfo.dwSize = m_struThermometryInfo.size();
m_struThermometryInfo.wPresetNo = 1;
m_struThermometryInfo.struPresetInfo[0] = new HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO_PARAM();
m_struThermometryInfo.struPresetInfo[0].byEnabled = 1;
m_struThermometryInfo.struPresetInfo[0].byRuleID = 1;
m_struThermometryInfo.struPresetInfo[0].wDistance = 10;
m_struThermometryInfo.struPresetInfo[0].fEmissivity = (float) 0.9599;
m_struThermometryInfo.struPresetInfo[0].byReflectiveEnabled = 0;
m_struThermometryInfo.struPresetInfo[0].fReflectiveTemperature = 20;
m_struThermometryInfo.struPresetInfo[0].byRuleCalibType = 2;
m_struThermometryInfo.struPresetInfo[0].byDistanceUnit = 0;
m_struThermometryInfo.struPresetInfo[0].struPoint = new HCNetSDK.NET_VCA_POINT();
m_struThermometryInfo.struPresetInfo[0].struPoint.fX = 0;
m_struThermometryInfo.struPresetInfo[0].struPoint.fY = 0;
m_struThermometryInfo.struPresetInfo[0].struPoint.write();
m_struThermometryInfo.struPresetInfo[0].struRegion = new HCNetSDK.NET_VCA_POLYGON();
m_struThermometryInfo.struPresetInfo[0].struRegion.dwPointNum = 2;
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[0] = new HCNetSDK.NET_VCA_POINT();
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[0].fX = (float) 0.187;
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[0].fY = (float) 0.6119;
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[0].write();
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[1] = new HCNetSDK.NET_VCA_POINT();
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[1].fX = (float) 0.876;
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[1].fY = (float) 0.569;
m_struThermometryInfo.struPresetInfo[0].struRegion.struPos[1].write();
m_struThermometryInfo.struPresetInfo[0].struRegion.write();
m_struThermometryInfo.write();
struCfg.lpInBuffer = m_struThermometryInfo.getPointer();
struCfg.dwInSize = m_struThermometryInfo.size();
boolean setSTDConfig = hCNetSDK.NET_DVR_SetSTDConfig(lUserID, 3625, struCfg);
log.info("NET_DVR_SetSTDConfig,{}", setSTDConfig);
HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA m_strJpegWithAppenData = new HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA();
m_strJpegWithAppenData.dwSize = m_strJpegWithAppenData.size();
m_strJpegWithAppenData.dwChannel = 1;
HCNetSDK.BYTE_ARRAY ptrJpegByte = new HCNetSDK.BYTE_ARRAY(2 * 1024 * 1024);
HCNetSDK.BYTE_ARRAY ptrP2PDataByte = new HCNetSDK.BYTE_ARRAY(2 * 1024 * 1024);
m_strJpegWithAppenData.pJpegPicBuff = ptrJpegByte.getPointer();
m_strJpegWithAppenData.pP2PDataBuff = ptrP2PDataByte.getPointer();
bRet = hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData(lUserID, 2, m_strJpegWithAppenData);
if (bRet) {
if (m_strJpegWithAppenData.dwP2PDataLen > 0) {
HKPoint point = point2point(hkPoint, sourceWidth, sourceHeight, m_strJpegWithAppenData.dwJpegPicWidth, m_strJpegWithAppenData.dwJpegPicHeight);
return getTemperature0(m_strJpegWithAppenData.pP2PDataBuff.getByteBuffer(point.x * point.y * 4, 4));
}
}
return 0f;
}
private Float getTemperature0(ByteBuffer byteBuffer) {
byte[] byTempData = new byte[4];
byteBuffer.get(byTempData);
int l = byTempData[0];
l &= 0xff;
l |= ((long) byTempData[1] << 8);
l &= 0xffff;
l |= ((long) byTempData[2] << 16);
l &= 0xffffff;
l |= ((long) byTempData[3] << 24);
return Float.intBitsToFloat(l);
}
private HKPoint point2point(HKPoint points, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
points.x = points.x * sourceWidth / targetWidth;
points.y = points.y * sourceHeight / targetHeight;
points.x = points.x >= targetWidth ? targetWidth : points.x;
points.x = points.x < 0 ? 0 : points.x;
points.y = points.y >= targetHeight ? targetHeight : points.y;
points.y = points.y < 0 ? 0 : points.y;
return points;
}
}
sdk接口类,因为官方demo代码较多这里只提取了需要使用的部分。
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;
import java.util.Arrays;
import java.util.List;
public interface HCNetSDK extends StdCallLibrary {
int SERIALNO_LEN = 48;
int NAME_LEN = 32;
boolean NET_DVR_Init();
boolean NET_DVR_Cleanup();
NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
boolean NET_DVR_Logout_V30(NativeLong lUserID);
boolean NET_DVR_SetSTDConfig(NativeLong lUserID, int dwCommand, NET_DVR_STD_CONFIG lpInConfigParam);
boolean NET_DVR_GetSTDConfig(NativeLong lUserID, int dwCommand, NET_DVR_STD_CONFIG lpOutConfigParam);
boolean NET_DVR_CaptureJPEGPicture_WithAppendData(NativeLong lUserID, int lChannel, NET_DVR_JPEGPICTURE_WITH_APPENDDATA lpJpegWithAppend);
int NET_DVR_GetLastError();
boolean NET_DVR_SetLogToFile(int bLogEnable, String strLogDir, boolean bAutoDel);
class NET_DVR_DEVICEINFO_V30 extends Structure {
public byte[] sSerialNumber = new byte[SERIALNO_LEN];
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte[] byRes1 = new byte[24];
}
class NET_DVR_THERMOMETRY_PRESETINFO extends Structure {
public int dwSize;
public short wPresetNo;
public byte[] byRes = new byte[2];
public NET_DVR_THERMOMETRY_PRESETINFO_PARAM[] struPresetInfo = new NET_DVR_THERMOMETRY_PRESETINFO_PARAM[40];
}
class NET_DVR_THERMOMETRY_PRESETINFO_PARAM extends Structure {
public byte byEnabled;
public byte byRuleID;
public short wDistance;
public float fEmissivity;
public byte byDistanceUnit;
public byte[] byRes = new byte[2];
public byte byReflectiveEnabled;
public float fReflectiveTemperature;
public byte[] szRuleName = new byte[NAME_LEN];
public byte[] byRes1 = new byte[63];
public byte byRuleCalibType;
public NET_VCA_POINT struPoint;
public NET_VCA_POLYGON struRegion;
}
class NET_VCA_POINT extends Structure {
public float fX;
public float fY;
}
class NET_VCA_POLYGON extends Structure {
public int dwPointNum;
public NET_VCA_POINT[] struPos = new NET_VCA_POINT[10];
}
class NET_DVR_THERMOMETRY_COND extends Structure {
public int dwSize;
public int dwChannel;
public short wPresetNo;
public byte[] byRes = new byte[62];
}
class NET_DVR_STD_CONFIG extends Structure {
public Pointer lpCondBuffer;
public int dwCondSize;
public Pointer lpInBuffer;
public int dwInSize;
public Pointer lpOutBuffer;
public int dwOutSize;
public Pointer lpStatusBuffer;
public int dwStatusSize;
public Pointer lpXmlBuffer;
public int dwXmlSize;
public byte byDataType;
public byte[] byRes = new byte[23];
}
class BYTE_ARRAY extends Structure {
public byte[] byValue;
public BYTE_ARRAY(int iLen) {
byValue = new byte[iLen];
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("byValue");
}
}
class NET_DVR_JPEGPICTURE_WITH_APPENDDATA extends Structure {
public int dwSize;
public int dwChannel;
public int dwJpegPicLen;
public Pointer pJpegPicBuff;
public int dwJpegPicWidth;
public int dwJpegPicHeight;
public int dwP2PDataLen;
public Pointer pP2PDataBuff;
public byte byIsFreezedata;
public byte[] byRes = new byte[255];
}
}
下面是运行结果:
|