├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── github │ └── lkqm │ └── hcnet │ ├── DeviceOptions.java │ ├── DeviceTemplateOptions.java │ ├── HCNetSDK.java │ ├── HikDevice.java │ ├── HikDeviceTemplate.java │ ├── HikResult.java │ ├── JnaPathUtils.java │ ├── Token.java │ ├── handler │ ├── AbstractFaceSnapHandler.java │ ├── AbstractFreshCardHandler.java │ ├── AbstractHandler.java │ ├── DispatchMessageCallback.java │ ├── FaceSnapFileStoreHandler.java │ ├── Handler.java │ └── VideoFileStoreCallback.java │ ├── model │ ├── DeviceInfo.java │ ├── FaceSnapEvent.java │ ├── FaceSnapInfo.java │ ├── FreshCardEvent.java │ ├── IDCardInfo.java │ ├── PassThroughResponse.java │ ├── ResponseStatus.java │ ├── UpgradeAsyncResponse.java │ └── UpgradeResponse.java │ ├── options │ ├── BaseOptions.java │ ├── MaintainOptions.java │ ├── MaintainOptionsImpl.java │ ├── PtzOptions.java │ ├── PtzOptionsImpl.java │ ├── SdkOptions.java │ └── SdkOptionsImpl.java │ └── util │ ├── BiFunction.java │ ├── InnerUtils.java │ └── JnaUtils.java └── test ├── java └── com │ └── github │ └── lkqm │ └── hcnet │ ├── HikDeviceTemplateTest.java │ ├── HikDeviceTest.java │ ├── JnaPathUtilsTest.java │ ├── callback │ ├── PrintDeviceExceptionCallback.java │ └── PrintFaceSnapHandler.java │ ├── model │ └── ResponseStatusTest.java │ ├── options │ ├── MaintainOptionsImplTest.java │ ├── PtzOptionsImplTest.java │ └── SdkOptionsImplTest.java │ └── test │ └── DeviceConstants.java └── resources └── natives ├── dll ├── D3DCompiler_43.dll ├── D3DX9_43.dll ├── HCAlarm.dll ├── HCCore.dll ├── HCNetSDK.dll ├── HCNetSDKCom │ ├── AnalyzeData.dll │ ├── AudioIntercom.dll │ ├── HCAlarm.dll │ ├── HCAlarm.lib │ ├── HCCoreDevCfg.dll │ ├── HCDisplay.dll │ ├── HCGeneralCfgMgr.dll │ ├── HCGeneralCfgMgr.lib │ ├── HCIndustry.dll │ ├── HCPlayBack.dll │ ├── HCPreview.dll │ ├── HCPreview.lib │ ├── HCVoiceTalk.dll │ ├── OpenAL32.dll │ ├── StreamTransClient.dll │ ├── SystemTransform.dll │ ├── libiconv2.dll │ └── msvcr90.dll ├── PlayCtrl.dll ├── SuperRender.dll └── gdiplus.dll ├── dylib ├── changePath.sh ├── libHCNetSDK.dylib ├── libNPQos.dylib ├── libPlayCtrl.dylib ├── libStreamTransClient.dylib ├── libSuperRender.dylib ├── libSystemTransform.dylib └── libhpr.dylib └── so ├── HCNetSDKCom ├── libAudioIntercom.so ├── libHCAlarm.so ├── libHCCoreDevCfg.so ├── libHCDisplay.so ├── libHCGeneralCfgMgr.so ├── libHCIndustry.so ├── libHCPlayBack.so ├── libHCPreview.so ├── libHCVoiceTalk.so ├── libStreamTransClient.so ├── libSystemTransform.so ├── libanalyzedata.so └── libiconv2.so ├── HCNetSDK_Log_Switch.xml ├── libAudioRender.so ├── libHCCore.so ├── libNPQos.so ├── libPlayCtrl.so ├── libSuperRender.so ├── libcrypto.so ├── libcrypto.so.1.0.0 ├── libhcnetsdk.so ├── libhpr.so ├── libopenal.so.1 ├── libssl.so └── libz.so /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | *.jar 4 | !.mvn/wrapper/* 5 | *.war 6 | *.zip 7 | *.tar 8 | *.tar.gz 9 | 10 | # eclipse ignore 11 | .settings/ 12 | .project 13 | .classpath 14 | 15 | # idea ignore 16 | .idea/ 17 | *.ipr 18 | *.iml 19 | *.iws 20 | 21 | # temp ignore 22 | *.log 23 | *.cache 24 | *.diff 25 | *.patch 26 | *.tmp 27 | 28 | # system ignore 29 | .DS_Store 30 | Thumbs.db 31 | *.orig 32 | *.out -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hcnetsdk 2 | 海康网络HCNetSDK二次封装的Java库. 3 | 4 | 5 | ``` 6 | 7 | com.github.lkqm 8 | hcnetsdk-java 9 | 0.0.5 10 | 11 | ``` 12 | 支持: JDK1.7+ 13 | 14 | ## 例子 15 | ``` 16 | HikDevice device = new HikDevice(hcnetsdk, ip, port, user, password); 17 | device.opsForMaintain().reboot(); 18 | ``` 19 | 20 | ## 核心 21 | - HikDevice: 面向对象方式操作设备. 22 | - HikDeviceTemplate: 封装底层sdk提供便捷对设备的操作. 23 | - DispatchMessageCallback: 事件分发的消息处理回调. 24 | - JnaPathUtils: 约定大于配置的本地依赖库加载. 25 | 26 | ## 功能 27 | - 登录 (login) 28 | - 注销 (logout) 29 | - 执行操作 (doAction) 30 | - 获取错误 (lastError) 31 | - 布防 (setupDeploy) 32 | - 透传 (passThrough) 33 | - 设备配置 (getNvrConfig, setNvrConfig) 34 | - 修改密码 (modifyPassword) 35 | - 本地功能 (opsForSdk) 36 | - 设备维护 (opsForMaintain): 升级、重启、校时、配置文件. 37 | - 云台控制 (opsForPtz) 38 | - ... 39 | 40 | ## 事件 41 | `DispatchMessageCallback`通过事件分发处理回调消息, 提供特定事件的抽象处理类: 42 | - 人脸抓拍事件: `AbstractFaceSnapHandler` 43 | - 刷证事件: `AbstractFreshCardHandler` 44 | 45 | 46 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.github.lkqm 6 | hcnetsdk-java 7 | 0.0.6-SNAPSHOT 8 | 9 | ${project.artifactId} 10 | 海康网络HCNetSDK二次封装的Java库 11 | https://github.com/lkqm/hcnetsdk-java 12 | 13 | 14 | The Apache License, Version 2.0 15 | http://www.apache.org/licenses/LICENSE-2.0.txt 16 | 17 | 18 | 19 | 20 | Mario Luo 21 | luokaiqiongmou@foxmail.com 22 | https://github.com/lkqm 23 | 24 | 25 | 26 | scm:git:https://github.com/lkqm/hcnetsdk-java.git 27 | scm:git:git@github.com:lkqm/hcnetsdk-java.git 28 | https://github.com/lkqm/hcnetsdk-java 29 | v0.0.3 30 | 31 | 32 | 33 | 1.7 34 | 1.7 35 | UTF-8 36 | UTF-8 37 | UTF-8 38 | true 39 | 40 | 41 | 42 | 43 | com.sun.jna 44 | jna 45 | 3.0.9 46 | 47 | 48 | net.java.dev.jna 49 | jna 50 | 3.2.1 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 1.18.16 56 | provided 57 | 58 | 59 | org.junit.jupiter 60 | junit-jupiter-api 61 | 5.7.0 62 | test 63 | 64 | 65 | 66 | 67 | 68 | release 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-source-plugin 74 | 2.2.1 75 | 76 | 77 | attach-sources 78 | 79 | jar-no-fork 80 | 81 | 82 | 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-javadoc-plugin 87 | 2.9.1 88 | 89 | 90 | attach-javadocs 91 | 92 | jar 93 | 94 | 95 | -Xdoclint:none 96 | 97 | 98 | 99 | 100 | 101 | org.apache.maven.plugins 102 | maven-gpg-plugin 103 | 1.5 104 | 105 | 106 | sign-artifacts 107 | verify 108 | 109 | sign 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | org.apache.maven.plugins 123 | maven-release-plugin 124 | 2.5.3 125 | 126 | v@{project.version} 127 | true 128 | false 129 | -DskipTests 130 | 131 | 132 | 133 | org.sonatype.plugins 134 | nexus-staging-maven-plugin 135 | 1.6.7 136 | true 137 | 138 | ossrh 139 | https://oss.sonatype.org/ 140 | true 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | ossrh 149 | Nexus Releases Repository 150 | https://oss.sonatype.org/service/local/staging/deploy/maven2 151 | 152 | 153 | ossrh 154 | Nexus Snapshots Repository 155 | https://oss.sonatype.org/content/repositories/snapshots 156 | 157 | 158 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/DeviceOptions.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK.FExceptionCallBack; 4 | import com.github.lkqm.hcnet.model.PassThroughResponse; 5 | import com.github.lkqm.hcnet.options.MaintainOptions; 6 | import com.github.lkqm.hcnet.options.PtzOptions; 7 | import com.github.lkqm.hcnet.util.BiFunction; 8 | import com.sun.jna.Structure; 9 | 10 | /** 11 | * 设备操作接口. 12 | */ 13 | public interface DeviceOptions { 14 | 15 | /** 16 | * 初始化. 17 | */ 18 | HikResult init(); 19 | 20 | /** 21 | * 销毁. 22 | */ 23 | void destroy(); 24 | 25 | /** 26 | * 执行动作. 27 | */ 28 | HikResult doAction(BiFunction> action); 29 | 30 | /** 31 | * 布防. 32 | */ 33 | HikResult setupDeploy(HCNetSDK.FMSGCallBack messageCallback, FExceptionCallBack exceptionCallback); 34 | 35 | /** 36 | * 透传. 37 | */ 38 | HikResult passThrough(String url, String data); 39 | 40 | /** 41 | * 透传. 42 | */ 43 | HikResult passThrough(String url, String data, int exceptOutByteSize); 44 | 45 | /** 46 | * 获取设备配置. 47 | */ 48 | HikResult getDvrConfig(long channel, int command, Class clazz); 49 | 50 | /** 51 | * 设置设备配置. 52 | */ 53 | HikResult setDvrConfig(long channel, int command, Structure settings); 54 | 55 | /** 56 | * 修改指定用户密码. 57 | */ 58 | HikResult modifyPassword(String targetUser, String newPassword); 59 | 60 | /** 61 | * 设备维护. 62 | */ 63 | MaintainOptions opsForMaintain(); 64 | 65 | /** 66 | * 云台操作. 67 | */ 68 | PtzOptions opsForPtz(); 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/DeviceTemplateOptions.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK.FExceptionCallBack; 4 | import com.github.lkqm.hcnet.HCNetSDK.FRealDataCallBack_V30; 5 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_PREVIEWINFO; 6 | import com.github.lkqm.hcnet.model.PassThroughResponse; 7 | import com.github.lkqm.hcnet.options.MaintainOptions; 8 | import com.github.lkqm.hcnet.options.PtzOptions; 9 | import com.github.lkqm.hcnet.options.SdkOptions; 10 | import com.github.lkqm.hcnet.util.BiFunction; 11 | import com.sun.jna.Structure; 12 | import lombok.SneakyThrows; 13 | 14 | public interface DeviceTemplateOptions { 15 | 16 | /** 17 | * 登录设备 18 | */ 19 | HikResult login(String ip, int port, String user, String password); 20 | 21 | /** 22 | * 注销登录 23 | */ 24 | HikResult logout(long userId); 25 | 26 | /** 27 | * 执行动作 28 | */ 29 | HikResult doAction(String ip, int port, String user, String password, 30 | BiFunction> action); 31 | 32 | /** 33 | * 获取最后的错误的执行结果 34 | */ 35 | HikResult lastError(); 36 | 37 | /** 38 | * 设备透传, 实现数据获取或配置修改. 39 | */ 40 | HikResult passThrough(long userId, String url, String input); 41 | 42 | /** 43 | * 设备透传, 实现数据获取或配置修改. 44 | */ 45 | HikResult passThrough(long userId, String url, byte[] inputBytes, 46 | int exceptOutByteSize); 47 | 48 | /** 49 | * 布防. 50 | *

51 | * 包括3个步骤: a.设置回调消息, b.建立上传通道, c.设置异常回调. 52 | */ 53 | HikResult setupDeploy(long userId, HCNetSDK.FMSGCallBack messageCallback, 54 | FExceptionCallBack exceptionCallback); 55 | 56 | /** 57 | * 修改设备密码. 58 | */ 59 | HikResult modifyPassword(long userId, String username, String newPassword); 60 | 61 | /** 62 | * NVR重新绑定通道, 抓拍机修改密码后需要重新绑定. 63 | */ 64 | HikResult nvrRebindChannels(long userId, String dvrUsername, String dvrNewPassword); 65 | 66 | /** 67 | * 获取设备配置数据. 68 | */ 69 | @SneakyThrows 70 | HikResult getDvrConfig(long userId, long channel, int command, 71 | Class clazz); 72 | 73 | /** 74 | * 获取设备配置数据. 75 | */ 76 | HikResult getDvrConfig(long userId, long channel, int command, Structure data); 77 | 78 | /** 79 | * 设置设备配置数据. 80 | */ 81 | HikResult setDvrConfig(long userId, long channel, int command, Structure data); 82 | 83 | /** 84 | * 设置视频实时预览 85 | */ 86 | HikResult realPlay(long userId, FRealDataCallBack_V30 callback); 87 | 88 | /** 89 | * 设置实时预览 90 | */ 91 | HikResult realPlay(long userId, NET_DVR_PREVIEWINFO previewInfo, 92 | FRealDataCallBack_V30 callback); 93 | 94 | /** 95 | * 停止实时预览 96 | */ 97 | HikResult stopRealPlay(long realHandle); 98 | 99 | /** 100 | * 本地sdk操作. 101 | */ 102 | SdkOptions opsForSdk(); 103 | 104 | /** 105 | * 设备维护. 106 | */ 107 | MaintainOptions opsForMaintain(long userId); 108 | 109 | /** 110 | * 云台操作. 111 | */ 112 | PtzOptions opsForPtz(long userId); 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/HikDevice.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK.FExceptionCallBack; 4 | import com.github.lkqm.hcnet.HCNetSDK.FMSGCallBack; 5 | import com.github.lkqm.hcnet.model.PassThroughResponse; 6 | import com.github.lkqm.hcnet.options.MaintainOptions; 7 | import com.github.lkqm.hcnet.options.PtzOptions; 8 | import com.github.lkqm.hcnet.util.BiFunction; 9 | import com.sun.jna.NativeLong; 10 | import com.sun.jna.Structure; 11 | import lombok.Getter; 12 | 13 | /** 14 | * 设备. 15 | *

16 | * 线程安全的. 17 | */ 18 | public class HikDevice implements DeviceOptions { 19 | 20 | @Getter 21 | private final String ip; 22 | @Getter 23 | private final int port; 24 | 25 | private final String user; 26 | private final String password; 27 | @Getter 28 | private final HikDeviceTemplate deviceTemplate; 29 | 30 | @Getter 31 | private volatile Token token; 32 | private volatile Long setupAlarmHandle; 33 | 34 | public HikDevice(HCNetSDK hcnetsdk, String ip, int port, String user, String password) { 35 | this.deviceTemplate = new HikDeviceTemplate(hcnetsdk); 36 | this.ip = ip; 37 | this.port = port; 38 | this.user = user; 39 | this.password = password; 40 | } 41 | 42 | @Override 43 | public HikResult init() { 44 | if (token == null) { 45 | synchronized (this) { 46 | if (token == null) { 47 | HikResult loginResult = deviceTemplate.login(ip, port, user, password); 48 | if (loginResult.isSuccess()) { 49 | token = loginResult.getData(); 50 | } 51 | return loginResult; 52 | } 53 | } 54 | } 55 | return HikResult.ok(); 56 | } 57 | 58 | @Override 59 | public synchronized void destroy() { 60 | // 消息回调取消布防 61 | if (setupAlarmHandle != null) { 62 | deviceTemplate.getHcnetsdk().NET_DVR_CloseAlarmChan_V30(new NativeLong(setupAlarmHandle)); 63 | setupAlarmHandle = null; 64 | } 65 | 66 | // 登录注销 67 | if (token != null && token.getUserId() != null) { 68 | deviceTemplate.logout(token.getUserId()); 69 | } 70 | } 71 | 72 | @Override 73 | public HikResult doAction(BiFunction> action) { 74 | checkInit(); 75 | return action.apply(deviceTemplate.getHcnetsdk(), token); 76 | } 77 | 78 | @Override 79 | public HikResult setupDeploy(FMSGCallBack messageCallback, FExceptionCallBack exceptionCallback) { 80 | checkInit(); 81 | if (setupAlarmHandle != null) { 82 | throw new RuntimeException("重复布防."); 83 | } 84 | HikResult deployResult = deviceTemplate 85 | .setupDeploy(token.getUserId(), messageCallback, exceptionCallback); 86 | if (deployResult.isSuccess() && deployResult.getData() != null) { 87 | setupAlarmHandle = deployResult.getData(); 88 | } 89 | return deployResult; 90 | } 91 | 92 | @Override 93 | public HikResult passThrough(String url, String data) { 94 | checkInit(); 95 | return deviceTemplate.passThrough(token.getUserId(), url, data); 96 | } 97 | 98 | @Override 99 | public HikResult passThrough(String url, String data, int exceptOutByteSize) { 100 | checkInit(); 101 | return deviceTemplate.passThrough(token.getUserId(), url, data.getBytes(), exceptOutByteSize); 102 | } 103 | 104 | @Override 105 | public HikResult getDvrConfig(long channel, int command, Class clazz) { 106 | checkInit(); 107 | return deviceTemplate.getDvrConfig(token.getUserId(), channel, command, clazz); 108 | } 109 | 110 | @Override 111 | public HikResult setDvrConfig(long channel, int type, Structure settings) { 112 | checkInit(); 113 | return deviceTemplate.setDvrConfig(token.getUserId(), channel, type, settings); 114 | } 115 | 116 | @Override 117 | public HikResult modifyPassword(String targetUser, String newPassword) { 118 | checkInit(); 119 | return deviceTemplate.modifyPassword(token.getUserId(), targetUser, newPassword); 120 | } 121 | 122 | @Override 123 | public MaintainOptions opsForMaintain() { 124 | checkInit(); 125 | return deviceTemplate.opsForMaintain(token.getUserId()); 126 | } 127 | 128 | @Override 129 | public PtzOptions opsForPtz() { 130 | checkInit(); 131 | return deviceTemplate.opsForPtz(token.getUserId()); 132 | } 133 | 134 | private void checkInit() { 135 | HikResult result = init(); 136 | if (!result.isSuccess()) { 137 | throw new RuntimeException(result.getError()); 138 | } 139 | } 140 | 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/HikDeviceTemplate.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK.FExceptionCallBack; 4 | import com.github.lkqm.hcnet.HCNetSDK.FRealDataCallBack_V30; 5 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_DEVICEINFO_V40; 6 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_PREVIEWINFO; 7 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_USER_LOGIN_INFO; 8 | import com.github.lkqm.hcnet.model.PassThroughResponse; 9 | import com.github.lkqm.hcnet.model.ResponseStatus; 10 | import com.github.lkqm.hcnet.options.MaintainOptions; 11 | import com.github.lkqm.hcnet.options.MaintainOptionsImpl; 12 | import com.github.lkqm.hcnet.options.PtzOptions; 13 | import com.github.lkqm.hcnet.options.PtzOptionsImpl; 14 | import com.github.lkqm.hcnet.options.SdkOptions; 15 | import com.github.lkqm.hcnet.options.SdkOptionsImpl; 16 | import com.github.lkqm.hcnet.util.BiFunction; 17 | import com.sun.jna.NativeLong; 18 | import com.sun.jna.Pointer; 19 | import com.sun.jna.Structure; 20 | import com.sun.jna.ptr.IntByReference; 21 | import com.sun.jna.ptr.NativeLongByReference; 22 | import java.util.Objects; 23 | import lombok.Getter; 24 | import lombok.NonNull; 25 | import lombok.SneakyThrows; 26 | 27 | /** 28 | * 海康SDK工具类. 29 | */ 30 | public class HikDeviceTemplate implements DeviceTemplateOptions { 31 | 32 | public static final int DEFAULT_PORT = 8000; 33 | 34 | @Getter 35 | @NonNull 36 | private final HCNetSDK hcnetsdk; 37 | 38 | public HikDeviceTemplate(@NonNull HCNetSDK hcnetsdk) { 39 | hcnetsdk.NET_DVR_Init(); 40 | this.hcnetsdk = hcnetsdk; 41 | } 42 | 43 | @Override 44 | public HikResult login(String ip, int port, String user, String password) { 45 | HCNetSDK.NET_DVR_USER_LOGIN_INFO loginInfo = new NET_DVR_USER_LOGIN_INFO(); 46 | System.arraycopy(ip.getBytes(), 0, loginInfo.sDeviceAddress, 0, ip.length()); 47 | loginInfo.wPort = (short) port; 48 | System.arraycopy(user.getBytes(), 0, loginInfo.sUserName, 0, user.length()); 49 | System.arraycopy(password.getBytes(), 0, loginInfo.sPassword, 0, password.length()); 50 | loginInfo.bUseAsynLogin = 0; 51 | loginInfo.write(); 52 | 53 | HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfo = new NET_DVR_DEVICEINFO_V40(); 54 | NativeLong userId = hcnetsdk.NET_DVR_Login_V40(loginInfo.getPointer(), deviceInfo.getPointer()); 55 | deviceInfo.read(); 56 | if (userId.longValue() == -1) { 57 | return lastError(); 58 | } 59 | 60 | Token token = Token.builder() 61 | .userId(userId.longValue()) 62 | .deviceSerialNumber(new String(deviceInfo.struDeviceV30.sSerialNumber).trim()) 63 | .build(); 64 | return HikResult.ok(token); 65 | } 66 | 67 | @Override 68 | public HikResult logout(long userId) { 69 | if (userId > -1) { 70 | boolean result = hcnetsdk.NET_DVR_Logout(new NativeLong(userId)); 71 | if (!result) { 72 | return lastError(); 73 | } 74 | } 75 | return HikResult.ok(); 76 | } 77 | 78 | @Override 79 | public HikResult doAction(String ip, int port, String user, String password, 80 | BiFunction> action) { 81 | HikResult loginResult = login(ip, port, user, password); 82 | if (!loginResult.isSuccess()) { 83 | return loginResult; 84 | } 85 | 86 | Token token = loginResult.getData(); 87 | try { 88 | HikResult result = action.apply(hcnetsdk, token); 89 | if (result == null) { 90 | result = HikResult.ok(); 91 | } 92 | return result; 93 | } finally { 94 | logout(token.getUserId()); 95 | } 96 | } 97 | 98 | @Override 99 | public HikResult lastError() { 100 | int code = hcnetsdk.NET_DVR_GetLastError(); 101 | if (code == 0) { 102 | return null; 103 | } 104 | 105 | String msg; 106 | if (code == 3) { 107 | msg = "sdk not init."; 108 | } else { 109 | msg = hcnetsdk.NET_DVR_GetErrorMsg(new NativeLongByReference(new NativeLong(code))); 110 | } 111 | return HikResult.fail(code, msg); 112 | } 113 | 114 | @Override 115 | public HikResult passThrough(long userId, String url, String input) { 116 | byte[] bytes = input == null ? null : input.getBytes(); 117 | return passThrough(userId, url, bytes, 3 * 1024 * 1024); 118 | } 119 | 120 | @Override 121 | public HikResult passThrough(long userId, String url, byte[] inputBytes, 122 | int exceptOutByteSize) { 123 | byte[] urlBytes = url.getBytes(); 124 | inputBytes = (inputBytes == null || inputBytes.length == 0) ? " ".getBytes() : inputBytes; 125 | // 输入参数 126 | HCNetSDK.NET_DVR_STRING_POINTER urlPointer = new HCNetSDK.NET_DVR_STRING_POINTER(); 127 | urlPointer.byString = urlBytes; 128 | urlPointer.write(); 129 | 130 | HCNetSDK.NET_DVR_STRING_POINTER inputPointer = new HCNetSDK.NET_DVR_STRING_POINTER(); 131 | inputPointer.byString = inputBytes; 132 | inputPointer.write(); 133 | 134 | HCNetSDK.NET_DVR_XML_CONFIG_INPUT inputParams = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT(); 135 | inputParams.dwSize = inputParams.size(); 136 | inputParams.lpRequestUrl = urlPointer.getPointer(); 137 | inputParams.dwRequestUrlLen = urlPointer.byString.length; 138 | inputParams.lpInBuffer = inputPointer.getPointer(); 139 | inputParams.dwInBufferSize = inputPointer.byString.length; 140 | inputParams.write(); 141 | 142 | // 输出参数 143 | HCNetSDK.NET_DVR_STRING_POINTER outputPointer = new HCNetSDK.NET_DVR_STRING_POINTER(); 144 | outputPointer.byString = new byte[exceptOutByteSize]; 145 | HCNetSDK.NET_DVR_STRING_POINTER outputStatusPointer = new HCNetSDK.NET_DVR_STRING_POINTER(); 146 | 147 | HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT outputParams = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT(); 148 | outputParams.dwSize = outputParams.size(); 149 | outputParams.lpOutBuffer = outputPointer.getPointer(); 150 | outputParams.dwOutBufferSize = outputPointer.size(); 151 | outputParams.lpStatusBuffer = outputStatusPointer.getPointer(); 152 | outputParams.dwStatusSize = outputStatusPointer.size(); 153 | inputPointer.write(); 154 | 155 | // 透传 156 | boolean result = hcnetsdk.NET_DVR_STDXMLConfig(new NativeLong(userId), inputParams, outputParams); 157 | outputPointer.read(); 158 | outputStatusPointer.read(); 159 | byte[] data = outputPointer.byString; 160 | byte[] statusData = outputStatusPointer.byString; 161 | String statusXml = new String(statusData).trim(); 162 | 163 | HikResult hikResult = new HikResult<>(); 164 | PassThroughResponse response = new PassThroughResponse(); 165 | if (!result) { 166 | HikResult error = lastError(); 167 | hikResult.set(error); 168 | if (statusXml.trim().length() > 0) { 169 | response.setStatus(ResponseStatus.ofXml(statusXml)); 170 | } 171 | } else { 172 | hikResult.setSuccess(true); 173 | response.setBytes(data); 174 | } 175 | 176 | hikResult.setData(response); 177 | return hikResult; 178 | } 179 | 180 | 181 | @Override 182 | public HikResult setupDeploy(long userId, HCNetSDK.FMSGCallBack messageCallback, 183 | FExceptionCallBack exceptionCallback) { 184 | // 消息回调 185 | if (messageCallback != null) { 186 | boolean result = hcnetsdk.NET_DVR_SetDVRMessageCallBack_V30(messageCallback, null); 187 | if (!result) { 188 | return lastError(); 189 | } 190 | } 191 | 192 | // 建立通道 193 | NativeLong setupAlarmHandle = hcnetsdk.NET_DVR_SetupAlarmChan_V30(new NativeLong(userId)); 194 | if (setupAlarmHandle.longValue() == -1) { 195 | return lastError(); 196 | } 197 | 198 | // 异常回调 199 | if (exceptionCallback != null) { 200 | boolean setExceptionResult = hcnetsdk 201 | .NET_DVR_SetExceptionCallBack_V30(0, setupAlarmHandle.intValue(), exceptionCallback, null); 202 | if (!setExceptionResult) { 203 | hcnetsdk.NET_DVR_CloseAlarmChan_V30(setupAlarmHandle); 204 | return lastError(); 205 | } 206 | } 207 | return HikResult.ok(setupAlarmHandle.longValue()); 208 | } 209 | 210 | @Override 211 | public HikResult modifyPassword(long userId, String username, String newPassword) { 212 | // 获取原始配置 213 | HCNetSDK.NET_DVR_USER_V30 dvrUser = new HCNetSDK.NET_DVR_USER_V30(); 214 | boolean getResult = hcnetsdk.NET_DVR_GetDVRConfig(new NativeLong(userId), HCNetSDK.NET_DVR_GET_USERCFG_V30, 215 | new NativeLong(0), dvrUser.getPointer(), dvrUser.size(), new IntByReference(0)); 216 | if (!getResult) { 217 | HikResult errorResult = lastError(); 218 | errorResult.setSuccess(false); 219 | return errorResult; 220 | } 221 | 222 | // 修改指定用户密码 223 | dvrUser.read(); 224 | for (HCNetSDK.NET_DVR_USER_INFO_V30 userInfo : dvrUser.struUser) { 225 | String name = new String(userInfo.sUserName).trim(); 226 | if (Objects.equals(username, name)) { 227 | userInfo.sPassword = newPassword.getBytes(); 228 | } 229 | } 230 | dvrUser.write(); 231 | boolean setResult = hcnetsdk.NET_DVR_SetDVRConfig(new NativeLong(userId), HCNetSDK.NET_DVR_SET_USERCFG_V30, 232 | new NativeLong(0), dvrUser.getPointer(), dvrUser.dwSize); 233 | if (!setResult) { 234 | HikResult errorResult = lastError(); 235 | errorResult.setSuccess(false); 236 | return errorResult; 237 | } 238 | return HikResult.ok(); 239 | } 240 | 241 | @Override 242 | public HikResult nvrRebindChannels(long userId, String dvrUsername, String dvrNewPassword) { 243 | // 获取已绑定通道配置 244 | IntByReference ibrBytesReturned = new IntByReference(0); 245 | HCNetSDK.NET_DVR_IPPARACFG mStrIpparaCfg = new HCNetSDK.NET_DVR_IPPARACFG(); 246 | mStrIpparaCfg.write(); 247 | Pointer lpIpParaConfig = mStrIpparaCfg.getPointer(); 248 | boolean getResult = hcnetsdk 249 | .NET_DVR_GetDVRConfig(new NativeLong(userId), HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(33), 250 | lpIpParaConfig, mStrIpparaCfg.size(), ibrBytesReturned); 251 | if (!getResult) { 252 | HikResult errorResult = lastError(); 253 | errorResult.setSuccess(false); 254 | return errorResult; 255 | } 256 | mStrIpparaCfg.read(); 257 | 258 | // 修改已绑定通道密码 259 | for (HCNetSDK.NET_DVR_IPDEVINFO deviceInfo : mStrIpparaCfg.struIPDevInfo) { 260 | String user = new String(deviceInfo.sUserName).trim(); 261 | if (Objects.equals(dvrUsername, user)) { 262 | deviceInfo.sPassword = dvrNewPassword.getBytes(); 263 | String ip = new String(deviceInfo.struIP.sIpV4).trim(); 264 | } 265 | } 266 | mStrIpparaCfg.write(); 267 | boolean setResult = hcnetsdk 268 | .NET_DVR_SetDVRConfig(new NativeLong(userId), HCNetSDK.NET_DVR_SET_IPPARACFG, new NativeLong(33), 269 | mStrIpparaCfg.getPointer(), mStrIpparaCfg.dwSize); 270 | if (!setResult) { 271 | HikResult errorResult = lastError(); 272 | errorResult.setSuccess(false); 273 | return errorResult; 274 | } 275 | return HikResult.ok(); 276 | } 277 | 278 | @Override 279 | @SneakyThrows 280 | public HikResult getDvrConfig(long userId, long channel, int command, 281 | Class clazz) { 282 | T data = clazz.newInstance(); 283 | data.write(); 284 | boolean result = hcnetsdk.NET_DVR_GetDVRConfig(new NativeLong(userId), command, new NativeLong(channel), 285 | data.getPointer(), data.size(), new IntByReference(0)); 286 | if (!result) { 287 | return lastError(); 288 | } 289 | data.read(); 290 | return HikResult.ok(data); 291 | } 292 | 293 | @Override 294 | public HikResult getDvrConfig(long userId, long channel, int command, Structure data) { 295 | data.write(); 296 | boolean result = hcnetsdk.NET_DVR_GetDVRConfig(new NativeLong(userId), command, new NativeLong(channel), 297 | data.getPointer(), data.size(), new IntByReference(0)); 298 | if (!result) { 299 | return lastError(); 300 | } 301 | data.read(); 302 | return HikResult.ok(); 303 | } 304 | 305 | @Override 306 | public HikResult setDvrConfig(long userId, long channel, int command, Structure data) { 307 | data.write(); 308 | boolean result = hcnetsdk.NET_DVR_SetDVRConfig(new NativeLong(userId), command, new NativeLong(channel), 309 | data.getPointer(), data.size()); 310 | if (!result) { 311 | return lastError(); 312 | } 313 | return HikResult.ok(); 314 | } 315 | 316 | @Override 317 | public HikResult realPlay(long userId, FRealDataCallBack_V30 callback) { 318 | HCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new HCNetSDK.NET_DVR_PREVIEWINFO(); 319 | previewInfo.lChannel = new NativeLong(1); 320 | previewInfo.dwStreamType = 0; 321 | previewInfo.dwLinkMode = 1; 322 | previewInfo.hPlayWnd = null; 323 | previewInfo.bBlocked = false; 324 | previewInfo.bPassbackRecord = false; 325 | previewInfo.byPreviewMode = 0; 326 | return realPlay(userId, previewInfo, callback); 327 | } 328 | 329 | @Override 330 | public HikResult realPlay(long userId, NET_DVR_PREVIEWINFO previewInfo, 331 | FRealDataCallBack_V30 callback) { 332 | NativeLong realPlayHandle = hcnetsdk.NET_DVR_RealPlay_V40(new NativeLong(userId), previewInfo, callback, null); 333 | if (realPlayHandle.longValue() == -1) { 334 | return lastError(); 335 | } 336 | return HikResult.ok(realPlayHandle.longValue()); 337 | } 338 | 339 | @Override 340 | public HikResult stopRealPlay(long realHandle) { 341 | boolean result = hcnetsdk.NET_DVR_StopRealPlay(new NativeLong(realHandle)); 342 | return result ? HikResult.ok() : lastError(); 343 | } 344 | 345 | @Override 346 | public SdkOptions opsForSdk() { 347 | return new SdkOptionsImpl(this); 348 | } 349 | 350 | @Override 351 | public MaintainOptions opsForMaintain(long userId) { 352 | return new MaintainOptionsImpl(this, userId); 353 | } 354 | 355 | @Override 356 | public PtzOptions opsForPtz(long userId) { 357 | return new PtzOptionsImpl(this, userId); 358 | } 359 | 360 | } 361 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/HikResult.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * 执行海康动作的响应结果 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class HikResult { 15 | 16 | protected boolean success; 17 | protected Integer errorCode; 18 | protected String errorMsg; 19 | protected T data; 20 | 21 | /** 22 | * 返回成功的实例. 23 | */ 24 | public static HikResult ok() { 25 | return new HikResult<>(true, null, null, null); 26 | } 27 | 28 | /** 29 | * 创建成功的实例. 30 | */ 31 | public static HikResult ok(T data) { 32 | return new HikResult<>(true, null, null, data); 33 | } 34 | 35 | /** 36 | * 创建失败的实例. 37 | */ 38 | public static HikResult fail(int code, String msg) { 39 | return new HikResult<>(false, code, msg, null); 40 | } 41 | 42 | /** 43 | * 创建失败的实例. 44 | */ 45 | public static HikResult fail(int code, String msg, T data) { 46 | return new HikResult<>(false, code, msg, data); 47 | } 48 | 49 | /** 50 | * 设置错误码,错误消息,成功状态. 51 | */ 52 | public void set(HikResult data) { 53 | this.success = data.success; 54 | this.errorCode = data.errorCode; 55 | this.errorMsg = data.errorMsg; 56 | } 57 | 58 | /** 59 | * 获取错误信息,格式: $code,$msg 60 | */ 61 | public String getError() { 62 | if (success) { 63 | return ""; 64 | } 65 | return errorCode + "," + errorMsg; 66 | } 67 | 68 | /** 69 | * 是否是密码或用户名错误. 70 | */ 71 | public boolean isPasswordError() { 72 | return errorCode != null && errorCode == 1; 73 | } 74 | 75 | /** 76 | * 是否是设备离线错误. 77 | */ 78 | public boolean isDeviceOfflineError() { 79 | return errorCode != null && errorCode == 7; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/JnaPathUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import java.io.File; 4 | import java.net.MalformedURLException; 5 | import java.net.URISyntaxException; 6 | import java.net.URL; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | import sun.awt.OSInfo; 10 | 11 | /** 12 | * Jna加载本地库相关工具类。 13 | */ 14 | public class JnaPathUtils { 15 | 16 | public static final String JNA_PATH_PROPERTY_NAME = "jna.library.path"; 17 | 18 | 19 | /** 20 | * 检查并设置本地库加载目录系统变量(jna.library.path), 仅开发环境下。 21 | */ 22 | public static boolean initJnaLibraryPathDev() { 23 | return initJnaLibraryPath(null, false); 24 | } 25 | 26 | /** 27 | * 检查并设置本地库加载目录系统变量(jna.library.path), 28 | *

29 | * 设置的路径, 资源目录下: natives/{type}, 其中type在不同操作系统下对应值不一样(so, dll, dylib) 30 | */ 31 | public static boolean initJnaLibraryPath(Class target) { 32 | return initJnaLibraryPath(target, true); 33 | } 34 | 35 | /** 36 | * 检查并设置本地库加载目录系统变量(jna.library.path), 37 | *

38 | * 设置的路径, 资源目录下: natives/{type}, 其中type在不同操作系统下对应值不一样(so, dll, dylib) 39 | */ 40 | public static boolean initJnaLibraryPath(Class target, boolean effectiveJar) { 41 | boolean modifiedPath = false; 42 | String jnaLibPath = System.getProperty(JNA_PATH_PROPERTY_NAME); 43 | boolean isJnaLibEmpty = (jnaLibPath == null || jnaLibPath.trim().length() == 0); 44 | if (isJnaLibEmpty) { 45 | Map libDirMap = new HashMap<>(); 46 | libDirMap.put(OSInfo.OSType.WINDOWS, "natives/dll"); 47 | libDirMap.put(OSInfo.OSType.LINUX, "natives/so"); 48 | libDirMap.put(OSInfo.OSType.MACOSX, "natives/dylib"); 49 | 50 | String libDir = libDirMap.get(OSInfo.getOSType()); 51 | if (libDir == null) { 52 | throw new RuntimeException("Unsupported operator system: " + OSInfo.getOSType().name()); 53 | } 54 | 55 | if (!isRunJar()) { 56 | URL uri = JnaPathUtils.class.getClassLoader().getResource(libDir); 57 | if (uri == null) { 58 | throw new IllegalStateException("Not found relation library: " + libDir); 59 | } 60 | jnaLibPath = uri.getPath(); 61 | System.setProperty(JNA_PATH_PROPERTY_NAME, jnaLibPath); 62 | modifiedPath = true; 63 | } else if (effectiveJar) { 64 | jnaLibPath = getJarDirectoryPath(target) + File.separator + libDir; 65 | System.setProperty(JNA_PATH_PROPERTY_NAME, jnaLibPath); 66 | modifiedPath = true; 67 | } 68 | } 69 | return modifiedPath; 70 | } 71 | 72 | /** 73 | * 是否以可执行jar方式启动 74 | */ 75 | public static boolean isRunJar() { 76 | URL resource = JnaPathUtils.class.getResource("/"); 77 | if (resource == null) { 78 | resource = JnaPathUtils.class.getResource(""); 79 | } 80 | if (resource == null) { 81 | return false; 82 | } 83 | 84 | String protocol = resource.getProtocol(); 85 | return "jar".equals(protocol); 86 | } 87 | 88 | /** 89 | * 获取执行jar所在目录 90 | */ 91 | public static String getJarDirectoryPath(Class target) { 92 | URL url = getLocation(target); 93 | File file = urlToFile(url); 94 | return file.getParent(); 95 | } 96 | 97 | 98 | /** 99 | * Gets the base location of the given class. 100 | *

101 | * If the class is directly on the file system (e.g., "/path/to/my/package/MyClass.class") then it will return the 102 | * base directory (e.g., "file:/path/to"). 103 | *

104 | *

105 | * If the class is within a JAR file (e.g., "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the 106 | * path to the JAR (e.g., "file:/path/to/my-jar.jar"). 107 | *

108 | * 109 | * @param c The class whose location is desired. 110 | */ 111 | private static URL getLocation(final Class c) { 112 | if (c == null) { 113 | return null; // could not load the class 114 | } 115 | 116 | // try the easy way first 117 | try { 118 | final URL codeSourceLocation = 119 | c.getProtectionDomain().getCodeSource().getLocation(); 120 | if (codeSourceLocation != null) { 121 | return codeSourceLocation; 122 | } 123 | } catch (final SecurityException e) { 124 | // NB: Cannot access protection domain. 125 | } catch (final NullPointerException e) { 126 | // NB: Protection domain or code source is null. 127 | } 128 | 129 | // NB: The easy way failed, so we try the hard way. We ask for the class 130 | // itself as a resource, then strip the class's path from the URL string, 131 | // leaving the base path. 132 | 133 | // get the class's raw resource path 134 | final URL classResource = c.getResource(c.getSimpleName() + ".class"); 135 | if (classResource == null) { 136 | return null; // cannot find class resource 137 | } 138 | 139 | final String url = classResource.toString(); 140 | final String suffix = c.getCanonicalName().replace('.', '/') + ".class"; 141 | if (!url.endsWith(suffix)) { 142 | return null; // weird URL 143 | } 144 | 145 | // strip the class's path from the URL string 146 | final String base = url.substring(0, url.length() - suffix.length()); 147 | 148 | String path = base; 149 | 150 | // remove the "jar:" prefix and "!/" suffix, if present 151 | if (path.startsWith("jar:")) { 152 | path = path.substring(4, path.length() - 2); 153 | } 154 | 155 | try { 156 | return new URL(path); 157 | } catch (final MalformedURLException e) { 158 | e.printStackTrace(); 159 | return null; 160 | } 161 | } 162 | 163 | /** 164 | * Converts the given {@link URL} to its corresponding {@link File}. 165 | *

166 | * This method is similar to calling {@code new File(url.toURI())} except that it also handles "jar:file:" URLs, 167 | * returning the path to the JAR file. 168 | *

169 | * 170 | * @param url The URL to convert. 171 | * @throws IllegalArgumentException if the URL does not correspond to a file. 172 | */ 173 | private static File urlToFile(final URL url) { 174 | return url == null ? null : urlToFile(url.toString()); 175 | } 176 | 177 | /** 178 | * Converts the given URL string to its corresponding {@link File}. 179 | * 180 | * @param url The URL to convert. 181 | * @throws IllegalArgumentException if the URL does not correspond to a file. 182 | */ 183 | private static File urlToFile(final String url) { 184 | String path = url; 185 | if (path.startsWith("jar:")) { 186 | // remove "jar:" prefix and "!/" suffix 187 | final int index = path.indexOf("!/"); 188 | path = path.substring(4, index); 189 | } 190 | try { 191 | if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS && path.matches("file:[A-Za-z]:.*")) { 192 | path = "file:/" + path.substring(5); 193 | } 194 | return new File(new URL(path).toURI()); 195 | } catch (final MalformedURLException e) { 196 | // NB: URL is not completely well-formed. 197 | } catch (final URISyntaxException e) { 198 | // NB: URL is not completely well-formed. 199 | } 200 | if (path.startsWith("file:")) { 201 | // pass through the URL as-is, minus "file:" prefix 202 | path = path.substring(5); 203 | return new File(path); 204 | } 205 | throw new IllegalArgumentException("Invalid URL: " + url); 206 | } 207 | 208 | } 209 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/Token.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet; 2 | 3 | import com.sun.jna.NativeLong; 4 | import java.io.Serializable; 5 | import lombok.Builder; 6 | import lombok.Data; 7 | 8 | /** 9 | * 海康设备登录响应结果 10 | */ 11 | @Data 12 | @Builder 13 | public class Token implements Serializable { 14 | 15 | /** 16 | * 登录后的用户标识 17 | */ 18 | private final Long userId; 19 | 20 | /** 21 | * 设备序列号 22 | */ 23 | private final String deviceSerialNumber; 24 | 25 | public NativeLong getUserIdNativeLong() { 26 | return new NativeLong(userId); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/handler/AbstractFaceSnapHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet.handler; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK; 4 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_ALARMER; 5 | import com.github.lkqm.hcnet.HCNetSDK.NET_VCA_HUMAN_FEATURE; 6 | import com.github.lkqm.hcnet.HCNetSDK.NET_VCA_RECT; 7 | import com.github.lkqm.hcnet.HCNetSDK.RECV_ALARM; 8 | import com.github.lkqm.hcnet.model.FaceSnapEvent; 9 | import com.github.lkqm.hcnet.model.FaceSnapInfo; 10 | import com.github.lkqm.hcnet.model.FaceSnapInfo.FaceFuture; 11 | import com.github.lkqm.hcnet.model.FaceSnapInfo.FaceRect; 12 | import com.github.lkqm.hcnet.util.InnerUtils; 13 | import com.github.lkqm.hcnet.util.JnaUtils; 14 | import com.sun.jna.NativeLong; 15 | import com.sun.jna.Pointer; 16 | 17 | /** 18 | * 人脸抓拍事件处理. 19 | */ 20 | public abstract class AbstractFaceSnapHandler extends AbstractHandler { 21 | 22 | public abstract void handle(FaceSnapEvent event); 23 | 24 | @Override 25 | public boolean accept(long command) { 26 | return command == HCNetSDK.COMM_UPLOAD_FACESNAP_RESULT; 27 | } 28 | 29 | @Override 30 | public void invoke(NativeLong lCommand, NET_DVR_ALARMER pAlarmer, RECV_ALARM pAlarmInfo, int dwBufLen, 31 | Pointer pUser) { 32 | if (accept(lCommand.longValue())) { 33 | FaceSnapEvent event = new FaceSnapEvent(); 34 | event.setDeviceInfo(resolveDeviceInfo(pAlarmer)); 35 | event.setFaceSnapInfo(resolveFaceSnapInfo(pAlarmInfo)); 36 | this.handle(event); 37 | } 38 | } 39 | 40 | // 解析身份证信息 41 | private FaceSnapInfo resolveFaceSnapInfo(RECV_ALARM pAlarmInfo) { 42 | HCNetSDK.NET_VCA_FACESNAP_RESULT strFaceSnapInfo = new HCNetSDK.NET_VCA_FACESNAP_RESULT(); 43 | JnaUtils.pointerToStructure(pAlarmInfo.getPointer(), strFaceSnapInfo); 44 | NET_VCA_RECT strRect = strFaceSnapInfo.struRect; 45 | NET_VCA_HUMAN_FEATURE strFaceFeature = strFaceSnapInfo.struFeature; 46 | 47 | FaceSnapInfo faceInfo = new FaceSnapInfo(); 48 | 49 | // 抓拍信息 50 | faceInfo.setFaceScore(strFaceSnapInfo.dwFaceScore); 51 | if (strFaceSnapInfo.dwFacePicLen > 0) { 52 | byte[] faceBytes = JnaUtils.pointerToBytes(strFaceSnapInfo.pBuffer1, strFaceSnapInfo.dwFacePicLen); 53 | faceInfo.setFaceImageBytes(faceBytes); 54 | } 55 | if (strFaceSnapInfo.dwBackgroundPicLen > 0) { 56 | byte[] backgroundBytes = JnaUtils 57 | .pointerToBytes(strFaceSnapInfo.pBuffer2, strFaceSnapInfo.dwBackgroundPicLen); 58 | faceInfo.setBackgroundImageBytes(backgroundBytes); 59 | } 60 | int absTime = strFaceSnapInfo.dwAbsTime; 61 | faceInfo.setSnapTimestamp(InnerUtils.hikAbsTimeToTimestamp(absTime)); 62 | faceInfo.setFacePicId(strFaceSnapInfo.dwFacePicID); 63 | faceInfo.setStayDurationMs((long) (strFaceSnapInfo.fStayDuration * 1000)); 64 | faceInfo.setRepeatTimes(strFaceSnapInfo.byRepeatTimes); 65 | 66 | // 人脸位置. 67 | FaceRect faceRect = new FaceRect(strRect.fX, strRect.fY, strRect.fWidth, strRect.fHeight); 68 | faceInfo.setFaceRect(faceRect); 69 | 70 | // 人脸特征 71 | FaceFuture faceFuture = new FaceFuture(); 72 | faceInfo.setFaceFuture(faceFuture); 73 | faceFuture.setAge(strFaceFeature.byAge); 74 | faceFuture.setEyeGlass(strFaceFeature.byEyeGlass); 75 | faceFuture.setAgeGroup(strFaceFeature.byAgeGroup); 76 | faceFuture.setAgeDeviation(strFaceFeature.byAgeDeviation); 77 | return faceInfo; 78 | } 79 | 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/handler/AbstractFreshCardHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet.handler; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK; 4 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_ALARMER; 5 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_DATE; 6 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_ID_CARD_INFO; 7 | import com.github.lkqm.hcnet.HCNetSDK.RECV_ALARM; 8 | import com.github.lkqm.hcnet.model.FreshCardEvent; 9 | import com.github.lkqm.hcnet.model.IDCardInfo; 10 | import com.sun.jna.NativeLong; 11 | import com.sun.jna.Pointer; 12 | import java.util.Calendar; 13 | import java.util.Date; 14 | 15 | /** 16 | * 刷证消息处理方法. 17 | */ 18 | public abstract class AbstractFreshCardHandler extends AbstractHandler { 19 | 20 | /** 21 | * 处理刷证事件 22 | */ 23 | public abstract void handle(FreshCardEvent event); 24 | 25 | @Override 26 | public boolean accept(long command) { 27 | return command == HCNetSDK.COMM_ID_INFO_ALARM; 28 | } 29 | 30 | @Override 31 | public void invoke(NativeLong lCommand, NET_DVR_ALARMER pAlarmer, RECV_ALARM pAlarmInfo, int dwBufLen, 32 | Pointer pUser) { 33 | if (accept(lCommand.longValue())) { 34 | FreshCardEvent event = new FreshCardEvent(); 35 | event.setCardInfo(resolveIdCardInfo(pAlarmInfo)); 36 | event.setDeviceInfo(resolveDeviceInfo(pAlarmer)); 37 | this.handle(event); 38 | } 39 | } 40 | 41 | // 解析身份证信息 42 | private IDCardInfo resolveIdCardInfo(RECV_ALARM pAlarmInfo) { 43 | IDCardInfo cardInfo = new IDCardInfo(); 44 | 45 | HCNetSDK.NET_DVR_ID_CARD_INFO_ALARM idCardInfoAlarm = new HCNetSDK.NET_DVR_ID_CARD_INFO_ALARM(); 46 | idCardInfoAlarm.write(); 47 | Pointer pCardInfo = idCardInfoAlarm.getPointer(); 48 | pCardInfo.write(0, pAlarmInfo.getPointer().getByteArray(0, idCardInfoAlarm.size()), 0, idCardInfoAlarm.size()); 49 | idCardInfoAlarm.read(); 50 | 51 | NET_DVR_ID_CARD_INFO idCardCfg = idCardInfoAlarm.struIDCardCfg; 52 | cardInfo.setIdNumber(new String(idCardCfg.byIDNum).trim()); 53 | cardInfo.setName(new String(idCardCfg.byName).trim()); 54 | cardInfo.setAddress(new String(idCardCfg.byAddr).trim()); 55 | cardInfo.setSex(idCardCfg.bySex); 56 | cardInfo.setNation(idCardCfg.byNation); 57 | cardInfo.setIssuingAuthority(new String(idCardCfg.byIssuingAuthority).trim()); 58 | cardInfo.setTermValidity(idCardCfg.byTermOfValidity); 59 | 60 | cardInfo.setBirth(convertToDate(idCardCfg.struBirth)); 61 | cardInfo.setValidityStartTime(convertToDate(idCardCfg.struStartDate)); 62 | if (idCardCfg.byTermOfValidity == 0) { 63 | cardInfo.setValidityEndTime(convertToDate(idCardCfg.struEndDate)); 64 | } 65 | return cardInfo; 66 | } 67 | 68 | private Date convertToDate(NET_DVR_DATE dvrDate) { 69 | Calendar calendar = Calendar.getInstance(); 70 | calendar.set(dvrDate.wYear, dvrDate.byMonth - 1, dvrDate.byDay); 71 | return calendar.getTime(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/handler/AbstractHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet.handler; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_ALARMER; 4 | import com.github.lkqm.hcnet.model.DeviceInfo; 5 | 6 | public abstract class AbstractHandler implements Handler { 7 | 8 | // 解析设备信息 9 | protected DeviceInfo resolveDeviceInfo(NET_DVR_ALARMER alarm) { 10 | DeviceInfo deviceInfo = new DeviceInfo(); 11 | if (alarm.byUserIDValid == 1) { 12 | deviceInfo.setUserId(alarm.lUserID.longValue()); 13 | } 14 | if (alarm.byDeviceIPValid == 1) { 15 | String deviceIp = new String(alarm.sDeviceIP).trim(); 16 | deviceInfo.setDeviceIp(deviceIp); 17 | } 18 | if (alarm.byDeviceNameValid == 1) { 19 | String deviceName = new String(alarm.sDeviceName).trim(); 20 | deviceInfo.setDeviceIp(deviceName); 21 | } 22 | if (alarm.bySerialValid == 1) { 23 | String serialNumber = new String(alarm.sSerialNumber).trim(); 24 | deviceInfo.setSerialNumber(serialNumber); 25 | } 26 | if (alarm.byMacAddrValid == 1) { 27 | String macAddr = new String(alarm.byMacAddr).trim(); 28 | deviceInfo.setDeviceMacAddr(macAddr); 29 | } 30 | return deviceInfo; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/handler/DispatchMessageCallback.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet.handler; 2 | 3 | import com.github.lkqm.hcnet.HCNetSDK; 4 | import com.github.lkqm.hcnet.HCNetSDK.NET_DVR_ALARMER; 5 | import com.github.lkqm.hcnet.HCNetSDK.RECV_ALARM; 6 | import com.sun.jna.NativeLong; 7 | import com.sun.jna.Pointer; 8 | import java.util.List; 9 | import java.util.concurrent.CopyOnWriteArrayList; 10 | 11 | /** 12 | * 基于分发的海康设备消息回调处理类. 13 | */ 14 | public class DispatchMessageCallback implements HCNetSDK.FMSGCallBack { 15 | 16 | public static final DispatchMessageCallback INSTANCE = new DispatchMessageCallback(); 17 | 18 | private final List handlers = new CopyOnWriteArrayList<>(); 19 | 20 | public DispatchMessageCallback addHandler(Handler handler) { 21 | handlers.add(handler); 22 | return this; 23 | } 24 | 25 | @Override 26 | public void invoke(NativeLong lCommand, NET_DVR_ALARMER pAlarmer, RECV_ALARM pAlarmInfo, int dwBufLen, 27 | Pointer pUser) { 28 | int cmd = lCommand.intValue(); 29 | 30 | for (Handler handler : handlers) { 31 | if (handler.accept(cmd)) { 32 | handler.invoke(lCommand, pAlarmer, pAlarmInfo, dwBufLen, pUser); 33 | return; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/lkqm/hcnet/handler/FaceSnapFileStoreHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.lkqm.hcnet.handler; 2 | 3 | import com.github.lkqm.hcnet.model.DeviceInfo; 4 | import com.github.lkqm.hcnet.model.FaceSnapEvent; 5 | import com.github.lkqm.hcnet.model.FaceSnapInfo; 6 | import com.github.lkqm.hcnet.util.InnerUtils; 7 | import java.io.File; 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | import lombok.AllArgsConstructor; 11 | import lombok.Getter; 12 | 13 | /** 14 | * 人脸抓拍事件处理. 15 | */ 16 | @AllArgsConstructor 17 | @Getter 18 | public class FaceSnapFileStoreHandler extends AbstractFaceSnapHandler { 19 | 20 | /** 21 | * 文件存储基本目录. 22 | */ 23 | protected final String baseDir; 24 | 25 | //