前一段时间在HR系统中做了一个人脸识别考勤的模块,主要功能:设备注册,下发卡号与人脸,获取卡号与人脸,删除卡号与人脸,对设备布防,报警回调函数。
首先在Window上开发,我们项目的JDK是1.6(64位),所以必须要用Win64的SDK开发包,这个很重要,版本对不上,在调用HCNetSDK.dll这个库文件时会报错,现在开始开发。
1.先去海康官网下载最新的SDK 官网地址:https://www.hikvision.com/cn/,下载你JDK版本对应的SDK(里面包含Java的demo),你如果不知道你的JDK对应什么SDK,可以去找海康销售跟他要海康技术支持的邮箱,你在开发过程中所有遇到的问题都可以用邮件询问。
2.在你的项目中新建一个文件夹用来放置库文件,可以直接将SDK中的库文件一股脑全放进来,我这里就是全放进来的,如图:
3.将java的demo中的examples.jar和jna.jar加载到自己的项目中,将java的demo中HCNetSDK.java 直接复制到自己的项目中,我们就是通过实现这个接口的方法来对设备进行操作。
4.根据sdk手册的信息建立相应的类,接口,回调函数,新建立的这些最好也放到HCNetSDK.java中。
图中红圈表示HCNetSDK.java要去加载HCNetSDK.dll文件
所以需要写一个路径加载类:HCNetSDKPath.java
package com.ykl;
import java.io.UnsupportedEncodingException;
public class HCNetSDKPath {
public static String dllPath;//HCNetSDK.dll 文件路径
public static void main(String[] args) {
String path = (HCNetSDKPath.class.getResource("/HCNetSDK/HCNetSDK.dll").getPath())
.replaceAll("%20", " ").substring(1).replace("/","\\");
System.out.println(path);
try {
dllPath = java.net.URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(dllPath);
}
}
在自己的类中需要实例化HCNetSDK,具体写法是:HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
5.接下来写一个工具类,里面有调用设备的基础方法:HCNetDeviceUtil.java
package com.utils;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.ykl.FMSGCallBack_V31;
public class HCNetDeviceUtil {
HCNetSDK hcNetSDK=HCNetSDK.INSTANCE;
HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
static FMSGCallBack_V31 fMSFCallBack_V31;//报警回调函数实现类
FRemoteCfgCallBackCardGet fRemoteCfgCallBackCardGet;//获取卡参数回调函数实现类
FRemoteCfgCallBackCardSet fRemoteCfgCallBackCardSet;//下发卡回调函数实现类
FRemoteCfgCallBackFaceGet fRemoteCfgCallBackFaceGet;//获取人脸回调函数实现类
FRemoteCfgCallBackFaceSet fRemoteCfgCallBackFaceSet;//下发人脸回调函数实现类
//登录设备,设备会返回,唯一用户ID:lUserID
public String loginDevice(String ip,String port,String userName,String password) {
//初始化SDK
boolean status = hcNetSDK.NET_DVR_Init();
if(status!=true) {
System.out.println("初始化失败:"+hcNetSDK.NET_DVR_GetCardLastError_Card());
}else {
System.out.println("初始化成功!");
}
//设置报警回调函数,这个函数将会上次人脸识别比对结果
if (fMSFCallBack_V31 == null){
fMSFCallBack_V31 = new FMSGCallBack_V31();
Pointer pUser = null;
if (!hcNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser)){
System.out.println("设置回调函数失败!");
}
}
//注册
String m_sDeviceIP = ip;//设备ip地址
m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
String m_sUsername = userName;//设备用户名
m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
String m_sPassword = password;//设备密码
m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
m_strLoginInfo.wPort = (short)Integer.parseInt(port);
m_strLoginInfo.bUseAsynLogin = false; //是否异步登录:0- 否,1- 是
m_strLoginInfo.write();
int lUserID = hcNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
if (lUserID == -1) {
System.out.println( "注册失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
//这里自己做错误处理
}
return lUserID+"";
}
//获取卡参数
public void GetCardInfo(int lUserID){
int iErr = 0;
HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParam = new HCNetSDK.NET_DVR_CARD_CFG_COND();
m_struCardInputParam.dwSize = m_struCardInputParam.size();
m_struCardInputParam.dwCardNum = 0xffffffff; //查找全部
m_struCardInputParam.byCheckCardNo = 1;
Pointer lpInBuffer = m_struCardInputParam.getPointer();
fRemoteCfgCallBackCardGet = new FRemoteCfgCallBackCardGet();
m_struCardInputParam.write();
HCNetSDK.MY_USER_DATA userData = new HCNetSDK.MY_USER_DATA();
userData.dwSize = userData.size();
userData.byteData = "1234567".getBytes();
Pointer pUserData = userData.getPointer();
userData.write();
int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_CFG_V50, lpInBuffer, m_struCardInputParam.size(),fRemoteCfgCallBackCardGet, pUserData);
if (lHandle < 0){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println("建立长连接失败:"+hcNetSDK.NET_DVR_GetCardLastError_Card());
return;
}
System.out.println("建立长连接成功!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle)){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println("断开长连接失败,错误号:" + iErr);
return;
}
System.out.println("断开长连接成功!");
}
//下发卡号
public byte[] setCardInfo(int lUserID,String empCode,String empName) throws Exception {
//设置卡参数
HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParamSet = new HCNetSDK.NET_DVR_CARD_CFG_COND();
m_struCardInputParamSet.read();
m_struCardInputParamSet.dwSize = m_struCardInputParamSet.size();
m_struCardInputParamSet.dwCardNum = 1;
m_struCardInputParamSet.byCheckCardNo = 1;
Pointer lpInBuffer = m_struCardInputParamSet.getPointer();
m_struCardInputParamSet.write();
Pointer pUserData = null;
fRemoteCfgCallBackCardSet = new FRemoteCfgCallBackCardSet();
int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_CFG_V50, lpInBuffer, m_struCardInputParamSet.size(), fRemoteCfgCallBackCardSet, pUserData);
if (lHandle < 0){
System.out.println("建立长连接失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
}
System.out.println( "建立设置卡参数长连接成功!");
HCNetSDK.NET_DVR_CARD_CFG_V50 struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50(); //卡参数
struCardInfo.read();
struCardInfo.dwSize = struCardInfo.size();
struCardInfo.dwModifyParamType = 0x3fff;
String strCardNo = empCode;
for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++){
struCardInfo.byCardNo[i] = 0;
}
for (int i = 0; i < strCardNo.length(); i++){
struCardInfo.byCardNo[i] = strCardNo.getBytes()[i];
}
struCardInfo.byCardValid = 1;
struCardInfo.byCardType =1;
struCardInfo.byLeaderCard = 0;
struCardInfo.byDoorRight[0] = 1; //门1有权限
struCardInfo.wCardRightPlan[0].wRightPlan[0] = 1; //门1关联卡参数计划模板1
//卡有效期
struCardInfo.struValid.byEnable = 1;
struCardInfo.struValid.struBeginTime.wYear = 2019;
struCardInfo.struValid.struBeginTime.byMonth = 12;
struCardInfo.struValid.struBeginTime.byDay = 1;
struCardInfo.struValid.struBeginTime.byHour = 0;
struCardInfo.struValid.struBeginTime.byMinute = 0;
struCardInfo.struValid.struBeginTime.bySecond = 0;
struCardInfo.struValid.struEndTime.wYear = 2036;
struCardInfo.struValid.struEndTime.byMonth = 12;
struCardInfo.struValid.struEndTime.byDay = 1;
struCardInfo.struValid.struEndTime.byHour = 0;
struCardInfo.struValid.struEndTime.byMinute = 0;
struCardInfo.struValid.struEndTime.bySecond = 0;
struCardInfo.dwMaxSwipeTime = 0; //无次数限制
struCardInfo.dwSwipeTime = 0;
struCardInfo.byCardPassword = "12345".getBytes();
struCardInfo.dwEmployeeNo = 22;
byte[] strCardName = empName.getBytes("GBK");
for (int i = 0; i < HCNetSDK.NAME_LEN; i++){
struCardInfo.byName[i] = 0;
}
for (int i = 0; i < strCardName.length; i++){
struCardInfo.byName[i] = strCardName[i];
}
struCardInfo.write();
Pointer pSendBufSet = struCardInfo.getPointer();
if(!hcNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBufSet, struCardInfo.size())){
System.out.println( "发送长连接失败:" + hcNetSDK.NET_DVR_GetLastError());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle))
{
System.out.println("断开长连接失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
}
System.out.println( "断开长连接成功!");
return struCardInfo.byCardNo;
}
//获取人脸信息
public void getFaceInfo(int lUserID) {
int iErr = 0;
HCNetSDK.NET_DVR_FACE_PARAM_COND m_struFaceInputParam = new HCNetSDK.NET_DVR_FACE_PARAM_COND();
m_struFaceInputParam.dwSize = m_struFaceInputParam.size();
m_struFaceInputParam.byCardNo = "111011".getBytes(); //人脸关联的卡号
m_struFaceInputParam.byEnableCardReader[0] = 1;
m_struFaceInputParam.dwFaceNum = 1;
m_struFaceInputParam.byFaceID = 1;
m_struFaceInputParam.write();
Pointer lpInBuffer = m_struFaceInputParam.getPointer();
Pointer pUserData = null;
fRemoteCfgCallBackFaceGet = new FRemoteCfgCallBackFaceGet();
int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_GET_FACE_PARAM_CFG, lpInBuffer, m_struFaceInputParam.size(),fRemoteCfgCallBackFaceGet, pUserData);
if (lHandle < 0){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println( "建立长连接失败,错误号:" + iErr);
return;
}
System.out.println("建立获取卡参数长连接成功!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle)){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println( "断开长连接失败,错误号:" + iErr);
return;
}
System.out.println("断开长连接成功!");
}
//下发人脸
public void setFaceInfo(int lUserID,byte[] cardNo,byte[] faceByte) {
int iErr = 0;
//设置人脸参数
HCNetSDK.NET_DVR_FACE_PARAM_COND m_struFaceSetParam = new HCNetSDK.NET_DVR_FACE_PARAM_COND();
m_struFaceSetParam.dwSize = m_struFaceSetParam.size();
m_struFaceSetParam.byCardNo = cardNo; //人脸关联的卡号
m_struFaceSetParam.byEnableCardReader[0] = 1;
m_struFaceSetParam.dwFaceNum = 1;
m_struFaceSetParam.byFaceID = 1;
m_struFaceSetParam.write();
Pointer lpInBuffer = m_struFaceSetParam.getPointer();
Pointer pUserData = null;
fRemoteCfgCallBackFaceSet = new FRemoteCfgCallBackFaceSet();
int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_SET_FACE_PARAM_CFG, lpInBuffer, m_struFaceSetParam.size(), fRemoteCfgCallBackFaceSet, pUserData);
if (lHandle < 0){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println( "建立长连接失败,错误号:" + iErr);
return;
}
System.out.println("建立设置卡参数长连接成功!");
HCNetSDK.NET_DVR_FACE_PARAM_CFG struFaceInfo = new HCNetSDK.NET_DVR_FACE_PARAM_CFG(); //卡参数
struFaceInfo.read();
struFaceInfo.dwSize = struFaceInfo.size();
struFaceInfo.byCardNo = cardNo;
struFaceInfo.byEnableCardReader[0] = 1; //需要下发人脸的读卡器,按数组表示,每位数组表示一个读卡器,数组取值:0-不下发该读卡器,1-下发到该读卡器
struFaceInfo.byFaceID =1; //人脸ID编号,有效取值范围:1~2
struFaceInfo.byFaceDataType = 1; //人脸数据类型:0- 模板(默认),1- 图片
/*****************************************
* 读取员工照片二进制数据,我的数据库存的是BLOB类型
*****************************************/
ByteArrayInputStream picfile=new ByteArrayInputStream(faceByte);
int picdataLength=picfile.available();
if(picdataLength<0) {
System.out.println("input file dataSize < 0");
return;
}
if(picdataLength>200*1024) {
System.out.println("input file dataSize >200kb");
return;
}
HCNetSDK.BYTE_ARRAY ptrpicByte=new HCNetSDK.BYTE_ARRAY(picdataLength);
picfile.read(ptrpicByte.byValue,0,ptrpicByte.byValue.length);
ptrpicByte.write();
/**************************/
struFaceInfo.dwFaceLen = picdataLength;
struFaceInfo.pFaceBuffer = ptrpicByte.getPointer();
struFaceInfo.write();
Pointer pSendBufSet = struFaceInfo.getPointer();
//ENUM_ACS_INTELLIGENT_IDENTITY_DATA = 9, //智能身份识别终端数据类型,下发人脸图片数据
if(!hcNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x9, pSendBufSet, struFaceInfo.size())){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println( "NET_DVR_SendRemoteConfig失败,错误号:" + iErr);
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle))
{
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println("断开长连接失败,错误号:" + iErr);
return;
}
System.out.println("断开长连接成功!");
}
//报警布防
public void SetupAlarmChan(int lUserID) {
//初始化SDK
boolean status = hcNetSDK.NET_DVR_Init();
if(status!=true) {
System.out.println("初始化失败:"+hcNetSDK.NET_DVR_GetCardLastError_Card());
}else {
System.out.println("初始化成功!");
}
HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
m_strAlarmInfo.dwSize=m_strAlarmInfo.size();
m_strAlarmInfo.byLevel=1;//智能交通布防优先级:0- 一等级(高),1- 二等级(中),2- 三等级(低)
m_strAlarmInfo.byAlarmInfoType=1;//智能交通报警信息上传类型:0- 老报警信息(NET_DVR_PLATE_RESULT),1- 新报警信息(NET_ITS_PLATE_RESULT)
m_strAlarmInfo.byDeployType =0; //布防类型(仅针对门禁主机、人证设备):0-客户端布防(会断网续传),1-实时布防(只上传实时数据)
m_strAlarmInfo.write();
int lAlarmHandle = hcNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
if (lAlarmHandle == -1){
System.out.println("布防失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
}
else{
System.out.println("布防成功");
}
}
//根据卡号删除人脸
public void delFace(int lUserID,String cardNo) {
int iErr = 0;
//删除人脸数据
HCNetSDK.NET_DVR_FACE_PARAM_CTRL m_struFaceDel = new HCNetSDK.NET_DVR_FACE_PARAM_CTRL();
m_struFaceDel.dwSize = m_struFaceDel.size();
m_struFaceDel.byMode = 0; //删除方式:0- 按卡号方式删除,1- 按读卡器删除
m_struFaceDel.struProcessMode.setType(HCNetSDK.NET_DVR_FACE_PARAM_BYCARD.class);
m_struFaceDel.struProcessMode.struByCard.byCardNo = cardNo.getBytes();//需要删除人脸关联的卡号
m_struFaceDel.struProcessMode.struByCard.byEnableCardReader[0] = 1; //读卡器
m_struFaceDel.struProcessMode.struByCard.byFaceID[0] = 1; //人脸ID
m_struFaceDel.write();
Pointer lpInBuffer = m_struFaceDel.getPointer();
boolean lRemoteCtrl = hcNetSDK.NET_DVR_RemoteControl(lUserID, HCNetSDK.NET_DVR_DEL_FACE_PARAM_CFG, lpInBuffer, m_struFaceDel.size());
if (!lRemoteCtrl){
iErr = hcNetSDK.NET_DVR_GetLastError();
System.out.println("NET_DVR_DEL_FACE_PARAM_CFG删除人脸图片失败,错误号:" + iErr);
}
else{
System.out.println("NET_DVR_DEL_FACE_PARAM_CFG成功!");
}
}
//删除卡号
public void delCardInfo(int lUserID,String cardNo) {
//设置卡参数
HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParamSet = new HCNetSDK.NET_DVR_CARD_CFG_COND();
m_struCardInputParamSet.read();
m_struCardInputParamSet.byCheckCardNo = 1;
Pointer lpInBuffer = m_struCardInputParamSet.getPointer();
m_struCardInputParamSet.write();
Pointer pUserData = null;
fRemoteCfgCallBackCardSet = new FRemoteCfgCallBackCardSet();
int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_CFG_V50, lpInBuffer, m_struCardInputParamSet.size(), fRemoteCfgCallBackCardSet, pUserData);
if (lHandle < 0){
System.out.println("建立长连接失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
}
System.out.println( "建立设置卡参数长连接成功!");
HCNetSDK.NET_DVR_CARD_CFG_V50 struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50(); //卡参数
struCardInfo.read();
struCardInfo.dwSize = struCardInfo.size();
struCardInfo.dwModifyParamType = 0x3fff;
struCardInfo.byCardNo =cardNo.getBytes();
struCardInfo.byCardValid = 0;//卡是否有效,0-无效,1-有效
struCardInfo.write();
Pointer pSendBufSet = struCardInfo.getPointer();
if(!hcNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBufSet, struCardInfo.size())){
System.out.println( "发送长连接失败:" + hcNetSDK.NET_DVR_GetLastError());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle))
{
System.out.println("断开长连接失败,错误号:" + hcNetSDK.NET_DVR_GetLastError());
}
System.out.println( "断开长连接成功!");
}
}
6.接下来还要编写HCNetDeviceUtil类中的定义的几个回调函数实现类
package com.utils;
import com.sun.jna.Pointer;
//下发卡号,回调函数实现类
public class FRemoteCfgCallBackCardSet implements HCNetSDK.FRemoteConfigCallback{
public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData){
System.out.println("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:" + dwType);
if(dwType==0) {
HCNetSDK.REMOTECONFIGSTATUS_CARD struCardStatus = new
HCNetSDK.REMOTECONFIGSTATUS_CARD();
struCardStatus.write();
Pointer pInfoV30 = struCardStatus.getPointer();
pInfoV30.write(0, lpBuffer.getByteArray(0, struCardStatus.size()),
0,struCardStatus.size());
struCardStatus.read();
int iStatus = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCardStatus.byStatus[i]&0xff;
iStatus = iStatus + (iByte << ioffset);
}
if(iStatus==1000) {
System.out.println("下发卡参数成功,dwStatus:" + iStatus);
}else if(iStatus==1002) {
int iErrorCode = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCardStatus.byErrorCode[i]&0xff;
iErrorCode = iErrorCode + (iByte << ioffset);
}
System.out.println("下发卡参数失败, dwStatus:" + iStatus + "错误号:" +
iErrorCode);
}
}
}
}
package com.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import com.sun.jna.Pointer;
//获取卡号,回调函数实现类
public class FRemoteCfgCallBackCardGet implements HCNetSDK.FRemoteConfigCallback{
public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData){
HCNetSDK.MY_USER_DATA m_userData = new HCNetSDK.MY_USER_DATA();
m_userData.write();
Pointer pUserVData = m_userData.getPointer();
pUserVData.write(0, pUserData.getByteArray(0, m_userData.size()), 0, m_userData.size());
m_userData.read();
switch (dwType){
case 0: //NET_SDK_CALLBACK_TYPE_STATUS
HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
struCfgStatus.write();
Pointer pCfgStatus = struCfgStatus.getPointer();
pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0,struCfgStatus.size());
struCfgStatus.read();
int iStatus = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCfgStatus.byStatus[i]&0xff;
iStatus = iStatus + (iByte << ioffset);
}
switch (iStatus){
case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
System.out.println("查询卡参数成功,dwStatus:" + iStatus);
break;
case 1002:
int iErrorCode = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCfgStatus.byErrorCode[i]&0xff;
iErrorCode = iErrorCode + (iByte << ioffset);
}
System.out.println("查询卡参数失败, dwStatus:" + iStatus + "错误号:" + iErrorCode);
break;
}
break;
case 2: //NET_SDK_CALLBACK_TYPE_DATA
HCNetSDK.NET_DVR_CARD_CFG_V50 m_struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50();
m_struCardInfo.write();
Pointer pInfoV30 = m_struCardInfo.getPointer();
pInfoV30.write(0, lpBuffer.getByteArray(0, m_struCardInfo.size()), 0,m_struCardInfo.size());
m_struCardInfo.read();
String str = new String(m_struCardInfo.byCardNo).trim();
try {
String srtName=new String(m_struCardInfo.byName,"GBK").trim(); //姓名
System.out.println("查询到的卡号,getCardNo:" + str + "姓名:" + srtName);
}
catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
package com.utils;
import com.sun.jna.Pointer;
//下发人脸,回调函数实现类
public class FRemoteCfgCallBackFaceSet implements HCNetSDK.FRemoteConfigCallback{
public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData){
switch (dwType){
case 0:// NET_SDK_CALLBACK_TYPE_STATUS
HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
struCfgStatus.write();
Pointer pCfgStatus = struCfgStatus.getPointer();
pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0,struCfgStatus.size());
struCfgStatus.read();
int iStatus = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCfgStatus.byStatus[i]&0xff;
iStatus = iStatus + (iByte << ioffset);
}
switch (iStatus){
case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
System.out.println("下发人脸参数成功,dwStatus:" + iStatus);
break;
case 1002:
int iErrorCode = 0;
for(int i=0;i<4;i++)
{
int ioffset = i*8;
int iByte = struCfgStatus.byErrorCode[i]&0xff;
iErrorCode = iErrorCode + (iByte << ioffset);
}
System.out.println("下发人脸参数失败, dwStatus:" + iStatus + "错误号:" + iErrorCode);
break;
}
break;
case 2:// 获取状态数据
HCNetSDK.NET_DVR_FACE_PARAM_STATUS m_struFaceStatus = new HCNetSDK.NET_DVR_FACE_PARAM_STATUS();
m_struFaceStatus.write();
Pointer pStatusInfo = m_struFaceStatus.getPointer();
pStatusInfo.write(0, lpBuffer.getByteArray(0, m_struFaceStatus.size()), 0,m_struFaceStatus.size());
m_struFaceStatus.read();
String str = new String(m_struFaceStatus.byCardNo).trim();
System.out.println("下发人脸数据关联的卡号:" + str + ",人脸读卡器状态:" +
m_struFaceStatus.byCardReaderRecvStatus[0] + ",错误描述:" + new String(m_struFaceStatus.byErrorMsg).trim());
break;
default:
break;
}
}
}
package com.utils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.sun.jna.Pointer;
//获取人脸,回调函数实现类
public class FRemoteCfgCallBackFaceGet implements HCNetSDK.FRemoteConfigCallback{
public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData){
switch (dwType){
case 0:// NET_SDK_CALLBACK_TYPE_STATUS
HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
struCfgStatus.write();
Pointer pCfgStatus = struCfgStatus.getPointer();
pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0,struCfgStatus.size());
struCfgStatus.read();
int iStatus = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCfgStatus.byStatus[i]&0xff;
iStatus = iStatus + (iByte << ioffset);
}
switch (iStatus){
case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
System.out.println("查询人脸参数成功,dwStatus:" + iStatus);
break;
case 1002:
int iErrorCode = 0;
for(int i=0;i<4;i++){
int ioffset = i*8;
int iByte = struCfgStatus.byErrorCode[i]&0xff;
iErrorCode = iErrorCode + (iByte << ioffset);
}
System.out.println("查询人脸参数失败, dwStatus:" + iStatus + "错误号:" + iErrorCode);
break;
}
break;
case 2: //NET_SDK_CALLBACK_TYPE_DATA
HCNetSDK.NET_DVR_FACE_PARAM_CFG m_struFaceInfo = new HCNetSDK.NET_DVR_FACE_PARAM_CFG();
m_struFaceInfo.write();
Pointer pInfoV30 = m_struFaceInfo.getPointer();
pInfoV30.write(0, lpBuffer.getByteArray(0, m_struFaceInfo.size()), 0,m_struFaceInfo.size());
m_struFaceInfo.read();
String str = new String(m_struFaceInfo.byCardNo).trim();
System.out.println("查询到人脸数据关联的卡号,getCardNo:" + str + ",人脸数据类型:" + m_struFaceInfo.byFaceDataType);
if(m_struFaceInfo.dwFaceLen >0){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
fout = new FileOutputStream(newName +"_Card[" + str + "]_ACSFaceCfg.jpg");
//将字节写入文件
long offset = 0;
ByteBuffer buffers = m_struFaceInfo.pFaceBuffer.getByteBuffer(offset, m_struFaceInfo.dwFaceLen);
byte [] bytes = new byte[m_struFaceInfo.dwFaceLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
准备工作就到这里为止了,接下来就是具体怎么操作设备了
我将代码从项目提取出来了,开发工具是Eclipse创建的是web项目:
https://pan.baidu.com/s/1O3_m-ydCENFoMmcSZr2Ytg 提取码: jr0x