海康SDK人脸抓拍

1.调用dll

//不使用demo中的jna.jar使用maven导入最新jna
//windows将HCNetSDK.dll重命名hcnetsdk,为了和linux兼容
//linux下的etc/ld.so.conf添加/××/××/HCNetSDKCom/,然后执行ldconfig
HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary("hcnetsdk", HCNetSDK.class);

2.在报警回调函数里面保存图片/获取IP和端口

public void invoke(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        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.dwBackgroundPicLen > 0)
        {
            FileOutputStream fout;
            try {
                //保存图片
                fout = new FileOutputStream("保存的路径");
                //将字节写入文件
                long offset = 0;
                ByteBuffer buffers = strFaceSnapInfo.pBuffer2.getByteBuffer(offset, strFaceSnapInfo.dwBackgroundPicLen);
                byte [] bytes = new byte[strFaceSnapInfo.dwBackgroundPicLen];
                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 ip = pAlarmInfo.struDevInfo.struDevIP.sIpV4, StandardCharsets.UTF_8)
                    .split("\0", 2)[0];
        String port = pAlarmInfo.struDevInfo.wPort;
    }

3.在Web页面预览可以参考Web的demo

https://download.csdn.net/download/qq_34576655/10690769

4.小坑

海康API返回的错误码在Windows下有符号,Linux下无符号

你可能感兴趣的:(Java,EE,Java,海康)