海康摄像头Sdk的使用 java Demo(idea 车牌识别,报警监听) 您所在的位置:网站首页 海康摄像头带声音吗 海康摄像头Sdk的使用 java Demo(idea 车牌识别,报警监听)

海康摄像头Sdk的使用 java Demo(idea 车牌识别,报警监听)

2024-07-01 00:45| 来源: 网络整理| 查看: 265

最近用到海康摄像头,起初遇到许多问题,发现网上没有比较直观的java demo,现在将其总结下,希望可以帮助大家

初步使用sdk 首先去官方下sdk:http://www.hikvision.com/Cn/download_more_401.html 基本上在这里插入图片描述

导入idea里(这里直接导入了sdk里的,主要是给大家看 文件sdk这些东西应该放哪些位置) 在这里插入图片描述

导入之后,我们需要修改一下HCNetSDK.java 很多人都是在这一步出问题的,网上各种方法都有,本人也尝试很多,最后选择这种简单粗暴的方式`

public interface HCNetSDK extends StdCallLibrary { HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary("E:\\haikang\\CH-HCNetSDKV6.1.4.6_build20191220_Win64\\Demo\\4- Java\\2-alarmDemo\\alarmJavaDemo\\HCNetSDK.dll", HCNetSDK.class); /***宏定义***/ //常量

直接改绝对路径,看清楚,是HCNetSDK.dll的绝对路径,直接在你项目里找到HCNetSDK.dll,右键→Copy Path ,过来粘贴

//播放库函数声明,PlayCtrl.dll interface PlayCtrl extends StdCallLibrary { PlayCtrl INSTANCE = (PlayCtrl) Native.loadLibrary("E:\\haikang\\CH-HCNetSDKV6.1.4.6_build20191220_Win64\\Demo\\4- Java\\2-alarmDemo\\alarmJavaDemo\\lib\\PlayCtrl.dll", PlayCtrl.class);

这个也一样PlayCtrl.dll的路径,用到播放的时候也要这个

4.接下来就是重点了,大体步骤,初始化,注册,登录,不妨,设置回调函数,撤防等,不多说,上码子:

public class HikVisionService { private JTable jTableAlarm; static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE; //alarmjavademo.HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;//设备信息 NET_DVR_DEVICEINFO_V40 m_registerInfo; NET_DVR_USER_LOGIN_INFO m_loginInfo; //static alarmjavademo.HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new alarmjavademo.HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息 //static alarmjavademo.HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new alarmjavademo.HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息 static String m_sDeviceIP = "192.168.x.xxx";//已登录设备的IP地址 static String m_sUsername = "admin";//设备用户名 static String m_sPassword = "xxxxxx";//设备密码,默认的好像是12345 static short m_sPort = 8000;//端口号,这是默认的 public NativeLong lUserID;//用户句柄 public NativeLong lAlarmHandle;//报警布防句柄 public int lListenHandle;//报警监听句柄 public NativeLong RemoteConfig; //撤防 public void CloseAlarmChan() { //报警撤防 if (lAlarmHandle.intValue() > -1) { if (hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle.intValue())) { System.out.println("撤防成功"); lAlarmHandle = new NativeLong(-1); } } } public void initMemberFlowUpload(int remainMinuteTime) throws InterruptedException { // 初始化 Boolean flag = hCNetSDK.NET_DVR_Init(); if (flag){ System.out.println("初始化成功"); }else{ System.out.println("初始化失败"); } //设置连接时间与重连时间 hCNetSDK.NET_DVR_SetConnectTime(2000, 1); hCNetSDK.NET_DVR_SetReconnect(100000, true); //设备信息, 输出参数 NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new NET_DVR_DEVICEINFO_V40(); NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new NET_DVR_USER_LOGIN_INFO(); // 注册设备-登录参数,包括设备地址、登录用户、密码等 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()); 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()); 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 = m_sPort; m_strLoginInfo.bUseAsynLogin = false; //是否异步登录:0- 否,1- 是 m_strLoginInfo.write(); //设备信息, 输出参数 int lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo,m_strDeviceInfo); System.out.println("lUserID.size-->" + lUserID); if(lUserID< 0){ System.out.println("hCNetSDK.NET_DVR_Login_V30()"+"\n" +hCNetSDK.NET_DVR_GetErrorMsg(null)); hCNetSDK.NET_DVR_Cleanup(); return; } /* if (lUserID < 0) { int a = hCNetSDK.NET_DVR_GetLastError(); System.out.println("登录注册失败"+a); return ; }else { System.out.println("登录注册成功"); }*/ //获取能力集 // String xmlInput = ""; // Pointer pInBuf = null; // //pInBuf.setChar(xmlInput.length()); // Pointer pOutBuf = null; // int dwInLength = xmlInput.length(); // if (!hCNetSDK.NET_DVR_GetDeviceAbility(lUserID,0x011,pInBuf,dwInLength,pOutBuf,0)){ // System.out.println("能力集获取失败!"+hCNetSDK.NET_DVR_GetLastError()); // return; // } //设置报警回调函数 System.out.println("331行"); if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(this::MsesGCallBack,null )){ System.out.println("设置回调函数失败"+hCNetSDK.NET_DVR_GetLastError()); return; }else { System.out.println("设置回调函数成功"); } //启用布防 NET_DVR_SETUPALARM_PARAM lpSetupParam = new NET_DVR_SETUPALARM_PARAM(); lpSetupParam.dwSize = 0; lpSetupParam.byLevel = 1;//布防优先级:0- 一等级(高),1- 二等级(中) lpSetupParam.byAlarmInfoType = 1;//上传报警信息类型: 0- 老报警信息(NET_DVR_PLATE_RESULT), 1- 新报警信息(NET_ITS_PLATE_RESULT) int lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID,lpSetupParam); if (lAlarmHandle< 0) { System.out.println("NET_DVR_SetupAlarmChan_V41 error, %d\n"+hCNetSDK.NET_DVR_GetLastError()); hCNetSDK.NET_DVR_Logout(lUserID); //hCNetSDK.NET_DVR_Logout(lUserID); hCNetSDK.NET_DVR_Cleanup(); return; } System.out.println("布防成功"); //启动监听---------------------------------------------- int iListenPort = 8100; String m_sListenIP = "192.168.x.xxx"; System.out.println("366行"); lListenHandle = hCNetSDK.NET_DVR_StartListen_V30(m_sListenIP, (short) iListenPort, this::MsesGCallBack, null); if(lListenHandle < 0) { JOptionPane.showMessageDialog(null, "启动监听失败,错误号:" + hCNetSDK.NET_DVR_GetLastError()); } else { JOptionPane.showMessageDialog(null, "启动监听成功"); } //-------------------识别任务提交----------------- //hCNetSDK.NET_DVR_VEHICLE_RECOG_COND m_struVehRecogCond; Thread.sleep(200000); //等待接收数据 //等待过程中,如果设备上传报警信息,在报警回调函数里面接收和处理报警信息 Timer timer = new Timer();// 实例化Timer类 timer.schedule(new TimerTask() { public void run() { //撤销布防上传通道 if (! hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle)) { System.out.println("! hCNetSDK.NET_DVR_CloseAlarmChan_V31(lAlarmHandle)\n"+ hCNetSDK.NET_DVR_GetLastError() +"\n" +hCNetSDK.NET_DVR_GetErrorMsg(null) ); //hCNetSDK.NET_DVR_Logout(lUserID); hCNetSDK.NET_DVR_Logout(lUserID); hCNetSDK. NET_DVR_Cleanup(); return; } //注销用户 //hCNetSDK.NET_DVR_Logout(lUserID); hCNetSDK.NET_DVR_Logout(lUserID); //释放SDK资源 hCNetSDK.NET_DVR_Cleanup(); this.cancel(); System.gc();//主动回收垃圾 } }, remainMinuteTime * 60 * 1000 );// 这里毫秒 } public boolean MsesGCallBack(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) { System.out.println("进入回调了"); try { String sAlarmType = new String(); String[] newRow = new String[3]; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String[] sIP = new String[2]; // DefaultTableModel alarmTableModel = ((DefaultTableModel) jTableAlarm.getModel());//获取表格模型 switch (lCommand) { case COMM_ITS_PARK_VEHICLE: NET_ITS_PARK_VEHICLE strItsParkVehicle = new NET_ITS_PARK_VEHICLE(); strItsParkVehicle.write(); Pointer pItsParkVehicle = strItsParkVehicle.getPointer(); pItsParkVehicle.write(0, pAlarmInfo.getByteArray(0, strItsParkVehicle.size()), 0, strItsParkVehicle.size()); strItsParkVehicle.read(); String srtParkingNo = null; String srtPlate = null; try { srtParkingNo = new String(strItsParkVehicle.byParkingNo).trim(); //车位编号 srtPlate = new String(strItsParkVehicle.struPlateInfo.sLicense, "GBK").trim(); //车牌号码 sAlarmType = sAlarmType + ",停产场数据,车位编号:" + srtParkingNo + ",车位状态:" + strItsParkVehicle.byLocationStatus + ",车牌:" + srtPlate; System.out.println(sAlarmType + "sAlarmType"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //增加—————————————— // break; case COMM_UPLOAD_PLATE_RESULT: NET_DVR_PLATE_RESULT strPlateResult = new NET_DVR_PLATE_RESULT(); strPlateResult.write(); Pointer pPlateInfo = strPlateResult.getPointer(); pPlateInfo.write(0, pAlarmInfo.getByteArray(0, strPlateResult.size()), 0, strPlateResult.size()); strPlateResult.read(); try { String srt3=new String(strPlateResult.struPlateInfo.sLicense,"GBK"); sAlarmType = sAlarmType + ":交通抓拍上传,车牌:"+ srt3; } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } newRow[0] = dateFormat.format(new Date()); //报警类型 newRow[1] = sAlarmType; //报警设备IP地址 sIP = new String(pAlarmer.sDeviceIP).split("\0", 2); newRow[2] = sIP[0]; // alarmTableModel.insertRow(0, newRow); if(strPlateResult.dwPicLen>0) { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String newName = sf.format(new Date()); FileOutputStream fout; try { fout = new FileOutputStream(".\\pic\\"+ new String(pAlarmer.sDeviceIP).trim() + "_" + newName+"_plateResult.jpg"); //将字节写入文件 long offset = 0; ByteBuffer buffers = strPlateResult.pBuffer1.getByteBuffer(offset, strPlateResult.dwPicLen); byte [] bytes = new byte[strPlateResult.dwPicLen]; 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(); } } break; case COMM_ITS_PLATE_RESULT: NET_ITS_PLATE_RESULT strItsPlateResult = new NET_ITS_PLATE_RESULT(); strItsPlateResult.write(); Pointer pItsPlateInfo = strItsPlateResult.getPointer(); pItsPlateInfo.write(0, pAlarmInfo.getByteArray(0, strItsPlateResult.size()), 0, strItsPlateResult.size()); strItsPlateResult.read(); try { String srt3=new String(strItsPlateResult.struPlateInfo.sLicense,"GBK"); sAlarmType = sAlarmType + ",车辆类型:"+strItsPlateResult.byVehicleType + ",交通抓拍上传,车牌:"+ srt3; } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } newRow[0] = dateFormat.format(new Date()); //报警类型 newRow[1] = sAlarmType; //报警设备IP地址 sIP = new String(pAlarmer.sDeviceIP).split("\0", 2); newRow[2] = sIP[0]; // alarmTableModel.insertRow(0, newRow); for(int i=0;i0) { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String newName = sf.format(new Date()); FileOutputStream fout; try { String filename = ".\\pic\\"+ new String(pAlarmer.sDeviceIP).trim() + "_" + newName+"_type["+strItsPlateResult.struPicInfo[i].byType+"]_ItsPlate.jpg"; fout = new FileOutputStream(filename); //将字节写入文件 long offset = 0; ByteBuffer buffers = strItsPlateResult.struPicInfo[i].pBuffer.getByteBuffer(offset, strItsPlateResult.struPicInfo[i].dwDataLen); byte [] bytes = new byte[strItsPlateResult.struPicInfo[i].dwDataLen]; 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(); } } } break; } System.out.println("sAlarmType---》" +sAlarmType); //报警类型 //报警设备IP地址 //sIP = new String(strPDCResult.struDevInfo.struDevIP.sIpV4).split("\0", 2); return true; }catch (Exception e){ Logger.getLogger(MemberFlowUPloadCallBackImpl.class.getName()).log(Level.SEVERE, null, e); return false; } } public static void main(String[] args) throws InterruptedException { HikVisionService hiKService = new HikVisionService(); hiKService.initMemberFlowUpload(10); } }

希望可以帮助大家,也期待和大家交流。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有