**1.**首先下载官网的sdk开发包,引入HCNetSDK.java,里面定义了很多调用的能力集。
**2.**通过报警回调函数中的黑名单报警这个接口去实现人脸抓拍的。
3.简单来说就是写个类去实现HCNetSDK.FMSGCallBack 然后根据条件函数 case HCNetSDK.COMM_SNAP_MATCH_ALARM。
4进行类的初始化操作、账号和密码的设定,IP地址的绑定。
**5.**下载好了之后创建maven项目.
按照SDK里面的文档要求把下图文件夹和文件引入(在SDK库文件夹里),7个dll文件和一个KCNetSDK文件夹(我这里创建了一个HCNetSDK的文件夹装下这些)
6. 接下来把他的接口类引入 如下在这里插入图片描述
打开接口类 看33行 这里有个引入dll文件的代码,我这里是用的文件引入工具类(这个类后面有给出),你们自己换成目录。resources\HCNetSDK\HCNetSDK的绝对路径!!!!其实这里引入的就是我们引入的7个dll文件中的HCNetSDK.dll这个文件,这里绝对路径引入的时候不要.dll后缀名。
文件也引入了 接下来就是我们自己创建java类去调用它的接口类了,创建两个类 一个是报警回调函数的实现类(用来实现调用海康人脸比对),一个是主类 里面包含 初始化海康接口类、调用第一个函数实现人脸评分抓拍等等。
附上核心代码!
package HikSnap;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.text.SimpleDateFormat;import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;/***@author fengwenyong* 实时进行人脸抓拍保存远近图片*/class FMSGCallBack_V31 implements HCNetSDK.FMSGCallBack_V31
{//实时人脸抓拍上传public boolean invoke(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser){AlarmDataHandle(lCommand, pAlarmer, pAlarmInfo, dwBufLen, pUser);return true;}public void AlarmDataHandle(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser){switch (lCommand.intValue()){case HCNetSDK.COMM_UPLOAD_FACESNAP_RESULT: //实时人脸抓拍上传System.out.println("低于设定拍照阈值,请重新识别");HCNetSDK.NET_VCA_FACESNAP_RESULT strFaceSnapInfo = new HCNetSDK.NET_VCA_FACESNAP_RESULT();strFaceSnapInfo.write();Pointer pFaceSnapInfo = strFaceSnapInfo.getPointer();pFaceSnapInfo.write(0, pAlarmInfo.getByteArray(0, strFaceSnapInfo.size()), 0, strFaceSnapInfo.size());strFaceSnapInfo.read();if(strFaceSnapInfo.dwFaceScore>80) {SimpleDateFormat df = new SimpleDateFormat ("yyyyMMddHHmmss");//设置日期格式String time = df.format (new Date ( ));// new Date()为获取当前系统时间System.out.println("face score is " + strFaceSnapInfo.dwFaceScore+" point");try {//设置图片保存路径FileOutputStream small = new FileOutputStream ("D:\\Picture\\" + time + "small.jpg");FileOutputStream big = new FileOutputStream ("D:\\Picture\\" + time + "big.jpg");try {small.write (strFaceSnapInfo.pBuffer1.getByteArray (0, strFaceSnapInfo.dwFacePicLen), 0, strFaceSnapInfo.dwFacePicLen);small.close ( );} catch (IOException ex) {ex.printStackTrace ( );}try {big.write (strFaceSnapInfo.pBuffer2.getByteArray (0, strFaceSnapInfo.dwBackgroundPicLen), 0, strFaceSnapInfo.dwBackgroundPicLen);big.close ( );} catch (IOException ex) {ex.printStackTrace ( );}} catch (FileNotFoundException ex) {ex.printStackTrace ( );}}break;case HCNetSDK.COMM_SNAP_MATCH_ALARM://人脸黑名单比对报警System.out.println("SNAP_MATCH_ALARM");HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM strFaceSnapMatch = new HCNetSDK.NET_VCA_FACESNAP_MATCH_ALARM();strFaceSnapMatch.write();Pointer pFaceSnapMatch = strFaceSnapMatch.getPointer();pFaceSnapMatch.write(0, pAlarmInfo.getByteArray(0, strFaceSnapMatch.size()), 0, strFaceSnapMatch.size());strFaceSnapMatch.read();if ((strFaceSnapMatch.dwSnapPicLen > 80) && (strFaceSnapMatch.byPicTransType == 80)) {SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");String newName = sf.format(new Date());FileOutputStream fout;try {String filename = "D:\\Picture\\" + newName + "_pSnapPicBuffer" + ".jpg";fout = new FileOutputStream(filename);//将字节写入文件long offset = 0;ByteBuffer buffers = strFaceSnapMatch.pSnapPicBuffer.getByteBuffer(offset, strFaceSnapMatch.dwSnapPicLen);byte[] bytes = new byte[strFaceSnapMatch.dwSnapPicLen];buffers.rewind();buffers.get(bytes);fout.write(bytes);fout.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if ((strFaceSnapMatch.struSnapInfo.dwSnapFacePicLen >80) && (strFaceSnapMatch.byPicTransType == 80)) {SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");String newName = sf.format(new Date());FileOutputStream fout;try {String filename = "D:\\Picture\\" + newName + "_struSnapInfo_pBuffer1" + ".jpg";fout = new FileOutputStream(filename);//?????д?????long offset = 0;ByteBuffer buffers = strFaceSnapMatch.struSnapInfo.pBuffer1.getByteBuffer(offset, strFaceSnapMatch.struSnapInfo.dwSnapFacePicLen);byte[] bytes = new byte[strFaceSnapMatch.struSnapInfo.dwSnapFacePicLen];buffers.rewind();buffers.get(bytes);fout.write(bytes);fout.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if ((strFaceSnapMatch.struBlackListInfo.dwBlackListPicLen > 80) && (strFaceSnapMatch.byPicTransType == 80)) {SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");String newName = sf.format(new Date());FileOutputStream fout;try {String filename = "D:\\Picture\\" + newName + "_fSimilarity_" + strFaceSnapMatch.fSimilarity + "_struBlackListInfo_pBuffer1" + ".jpg";fout = new FileOutputStream(filename);//?????д?????long offset = 0;ByteBuffer buffers = strFaceSnapMatch.struBlackListInfo.pBuffer1.getByteBuffer(offset, strFaceSnapMatch.struBlackListInfo.dwBlackListPicLen);byte[] bytes = new byte[strFaceSnapMatch.struBlackListInfo.dwBlackListPicLen];buffers.rewind();buffers.get(bytes);fout.write(bytes);fout.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;}}}public class Snap {static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;static NativeLong lUserID = new NativeLong(-1);//用户句柄static NativeLong lAlarmHandle = new NativeLong(-1);//报警布防句柄FMSGCallBack_V31 fMSFCallBack_V31 = null;/*** @param args*/public static void main(String[] args) {Snap test = new Snap();hCNetSDK.NET_DVR_Init();test.Login();test.SetAlarm();while(true);}public void Login(){ HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();lUserID = hCNetSDK.NET_DVR_Login_V30("10.118.230.27",(short) 8000, "admin", "zhly2019app", m_strDeviceInfo);if (lUserID.longValue() == -1){System.out.println("login fail,error code "+hCNetSDK.NET_DVR_GetLastError());} else{System.out.println("login successful!");}}public void SetAlarm(){if (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("Set callback function fail");}else{System.out.println("Set callback function successfully!");}}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 =1;m_strAlarmInfo.write();lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);if (lAlarmHandle.intValue() == -1){System.out.println("deployment fail");}else{System.out.println("deployment Successful");}}}}