欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

海康人脸设备接入 例子 人脸布防回调,人脸下发  

程序员文章站 2024-03-15 13:18:29
...
人脸设备类型:DS-K5671-ZU

接入需要开发文档 设备网络SDK编程指南(明眸).pdf

需要的SDK库:HCNetSDK.dll、HCCore.dll、HCNetSDKCom文件夹、PlayCtrl.dll、SuperRender.dll、AudioRender.dll、ssleay32.dll、libeay32.dll等文件均要加载到程序里面,【HCNetSDKCom文件夹】(包含里面的功能组件dll库文件)需要和HCNetSDK.dll、HCCore.dll一起加载,放在同一个目录下,且HCNetSDKCom文件夹名不能修改。

接入流程
1:SDK 初始化

2:设备注册

3:调用相关接口

测试DEMO
package com.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;

public class Test {
	
	HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
	//登录用户
	NativeLong  lUserID = new NativeLong(-1);
	HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;
	NativeLong lAlarmHandle;//报警布防句柄
	FMSGCallBack_V31 fMSFCallBack_V31;//报警回调函数实现
	/***初始化SDK **/
	public Test(){
		boolean initSuc = hCNetSDK.NET_DVR_Init();
		if (initSuc != true){
			System.out.println("初始化失败");
		}
	}
	
	/**注册*/
	public void reg(){
        if (lUserID.longValue() > -1) {
            hCNetSDK.NET_DVR_Logout(lUserID);
            lUserID = new NativeLong(-1);
        }
        m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        int iPort = Integer.parseInt("8000");
        lUserID = hCNetSDK.NET_DVR_Login_V30("IP",(short) 端口,"账号", "密码", m_strDeviceInfo);

        long userID = lUserID.longValue();
        if (userID == -1) {
        	System.out.println("注册失败");
        } else {
        	System.out.println("注册成功");
        }
	}
	
	
	//布防
	public void Ready() throws InterruptedException{
		if (lUserID.intValue() == -1){
			System.out.println("请先注册");
		}
		//尚未布防,需要布防
		if (lAlarmHandle == null || lAlarmHandle.intValue() < 0) {
			if (fMSFCallBack_V31 == null) {
				fMSFCallBack_V31 = new FMSGCallBack_V31();
                  Pointer pUser = null;
                  if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser)) {
                      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;
            m_strAlarmInfo.byAlarmInfoType=1;
            m_strAlarmInfo.byDeployType = 0;//布防类型:0-客户端布防,1-实时布防
            m_strAlarmInfo.write();
            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
            if (lAlarmHandle.intValue() == -1) {
            	System.out.println( "布防失败"+hCNetSDK.NET_DVR_GetLastError());
            }else {
            	System.out.println( "布防成功");
            }
			Thread.sleep(200000);
		}
	}
	
	public void upload(){
		 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 = "111011".getBytes(); //人脸关联的卡号
		m_struFaceSetParam.byEnableCardReader[0]  = 1;
	    m_struFaceSetParam.dwFaceNum = 1;
	    m_struFaceSetParam.byFaceID = 1;
	    m_struFaceSetParam.write();
		Pointer lpInBuffer = m_struFaceSetParam.getPointer();
		Pointer pUserData = null;
		FRemoteCfgCallBackCardGet  fRemoteCfgCallBackFaceSet = new FRemoteCfgCallBackCardGet();
		//设置人脸参数
		NativeLong lHandle = hCNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCNetSDK.NET_DVR_SET_FACE_PARAM_CFG, lpInBuffer, m_struFaceSetParam.size(),fRemoteCfgCallBackFaceSet, pUserData);
		if (lHandle.intValue() < 0){
			iErr = hCNetSDK.NET_DVR_GetLastError();
			System.out.println("建立长连接失败,错误号:" + iErr);
			
		}
		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 = "111011".getBytes();
	    struFaceInfo.byEnableCardReader[0] = 1; //需要下发人脸的读卡器,按数组表示,每位数组表示一个读卡器,数组取值:0-不下发该读卡器,1-下发到该读卡器
	    struFaceInfo.byFaceID  =1; //人脸ID编号,有效取值范围:1~2
	    struFaceInfo.byFaceDataType  = 1; //人脸数据类型:0- 模板(默认),1- 图片
	        /*****************************************
	         * 从本地文件里面读取JPEG图片二进制数据
	         *****************************************/
	        FileInputStream picfile = null;
	        int picdataLength = 0;
	        try{
	                 picfile = new FileInputStream(new File("D:\\testPic\\error.jpg"));
	        }
	        catch(FileNotFoundException e) {
	        	 e.printStackTrace();
	        }
	        try{
	        	picdataLength = picfile.available();
	        } catch(IOException e1) {
	        	e1.printStackTrace();
	        }
	        if(picdataLength < 0){
	        	System.out.println("input file dataSize < 0");
	        	return;
	        }

	        HCNetSDK.BYTE_ARRAY ptrpicByte = new HCNetSDK.BYTE_ARRAY(picdataLength);
	        try {
	        	picfile.read(ptrpicByte.byValue);
	        } catch (IOException e2) {
	            e2.printStackTrace();
	        }
	        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(5000);
	    	} catch (InterruptedException e) {
	    		// TODO Auto-generated catch block
	    		e.printStackTrace();
	    	}

	        if(!hCNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
	        	iErr = hCNetSDK.NET_DVR_GetLastError();
	        	System.out.println("断开长连接失败,错误号:" + iErr);
	        	return;
	         }
	        System.out.println("断开长连接成功!");
	}
	
	/**
	 * 20482
	 * 
	 * 
	 * */
	public static void main(String[] args) throws InterruptedException {
		Test test = new Test();
		test.reg();
//		test.Ready();
		test.upload();
		
	}

}


FMSGCallBack_V31

package com.test;

import java.io.File;
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 FMSGCallBack_V31 implements HCNetSDK.FMSGCallBack_V31{
	
	@Override
	public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
		HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
        strACSInfo.write();
        Pointer pACSInfo = strACSInfo.getPointer();
        pACSInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size());
        strACSInfo.read();
        if(strACSInfo.dwMinor == 0x4b){// 通过:75   不通过76
    		if(strACSInfo.dwPicDataLen>0) { //判断是否刷脸
                  SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                  String newName = sf.format(new Date());
                  FileOutputStream fout;
                  try {
                	  //保存验证成功刷脸的用户并且将照片保存到磁盘当中
                           String filename = newName+"_ACS_card_"+ new String(strACSInfo.struAcsEventInfo.byCardNo).trim()+".jpg";
                  		   File file = new File("D:\\testPic\\"+filename);
                           fout = new FileOutputStream(file);
                           //将字节写入文件
                           long offset = 0;
                           ByteBuffer buffers = strACSInfo.pPicData.getByteBuffer(offset, strACSInfo.dwPicDataLen);
                           byte [] bytes = new byte[strACSInfo.dwPicDataLen];
                           buffers.rewind();
                           buffers.get(bytes);
                           fout.write(bytes);
                           fout.close();
                  } catch (FileNotFoundException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                  } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                  }
             }
            String sAlarmType = ":门禁主机报警信息,卡号:"+  new String(strACSInfo.struAcsEventInfo.byCardNo).trim() + ",卡类型:" +
                    strACSInfo.struAcsEventInfo.byCardType + ",报警主类型:" + strACSInfo.dwMajor + ",报警次类型:" + strACSInfo.dwMinor;
            System.out.println(sAlarmType);
        }
        
		return true;
	}

}





package com.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import com.sun.jna.Pointer;


public class FRemoteCfgCallBackCardGet implements HCNetSDK.FRemoteConfigCallback{
		
	@Override
	public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData){
		 System.out.println("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:" + dwType);
         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 1001:
                         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());
		default:
			break;
		}
     }
}


下发人脸时候 需要要和卡号作绑定

人脸状态码

,0-失败,1-成功,2-重试或人脸质量差,
3-内存已满(人脸数据满),4-已存在该人脸,5-非法人脸 ID,6-算法建模失败,7-未下发卡权限,8-
未定义(保留),9-人眼间距小距小,10-图片数据长度小于 1KB,11-图片格式不符(png/jpg/bmp),
12-图片像素数量超过上限,13-图片像素数量低于下限,14-图片信息校验失败,15-图片解码失败,
16-人脸检测失败,17-人脸评分失败