海康4200平台是海康摄像智能分析平台,包括但不限于交通违规,穿戴违规、行为违规监测,主要是用海康内部训练的AI模型进行监测是否违规,开发阶段海康会给代码示例Demo,主要语言是C++,C#,Java,(Demo示例\4- Java 开发示例\2-报警布防监听\AlarmDemo)
海康SDK链接:海康开放平台
1、Alarm类:实现CommandLineRunner并重写Run方法为了运行程序开始注册设备监听。
把demo示例只能监听注册一台设备更改为多台设备,Camera、CameraMapper这里不进行展示,Camera实体类至少包含摄像头的IP和登陆的账号密码
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.hikvision.entity.Camera;
import com.xxxx.hikvision.mapper.CameraMapper;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
@Slf4j
@Component
public class Alarm implements CommandLineRunner {
static HCNetSDK hCNetSDK = null;
static FMSGCallBack_V31 fMSFCallBack_V31 = null;
static String strDllPath; //全局静态路径类
@Value("${strDllPath}")
public void setStrDllPath(String strDllPath) {
Alarm.strDllPath = strDllPath;
}
// 设备登录句柄数组
static int[] lUserID = new int[402];
// 布防句柄数组
static int[] lAlarmHandle = new int[402];
// 设备登录信息
static String[] sDeviceIP;
static String wPort;
static String sUserName;
static String sPassword;
@Value("${ivmsIp}")
public void setIvmsIp(String[] sDeviceIP) {
Alarm.sDeviceIP = sDeviceIP;
}
@Value("${ivmsPort}")
public void setIvmsPort(String wPort) {
Alarm.wPort = wPort;
}
@Value("${ivmsUser}")
public void setIvmsUser(String sUserName) {
Alarm.sUserName = sUserName;
}
@Value("${ivmsPsw}")
public void setIvmsPsw(String sPassword) {
Alarm.sPassword = sPassword;
}
@Autowired
private CameraMapper cameraMapper;
public Alarm(CameraMapper cameraMapper) {
this.cameraMapper = cameraMapper;
}
private static boolean createSDKInstance() {
if (hCNetSDK == null) {
synchronized (HCNetSDK.class) {
try {
strDllPath = strDllPath + "HCNetSDK.dll";
System.out.println(strDllPath);
hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
} catch (Exception ex) {
System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
return false;
}
}
}
return true;
}
public static void login_V40(int i, String ip, short port, String user, String psw) {
//注册
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();//设备信息
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 = user;//设备用户名
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 = psw;//设备密码
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 = port;
m_strLoginInfo.bUseAsynLogin = true; //是否异步登录:0- 否,1- 是
m_strLoginInfo.byLoginMode = 0; //ISAPI登录
m_strLoginInfo.write();
lUserID[i] = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
if (lUserID[i] == -1) {
System.out.println("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
} else {
System.out.println(ip + ":设备登录成功!" + lUserID[i]);
}
}
@Override
public void run(String... args) {
if (hCNetSDK == null) {
if (!createSDKInstance()) {
System.out.println("Load SDK fail");
return;
}
}
/**初始化*/
hCNetSDK.NET_DVR_Init();
hCNetSDK.NET_DVR_SetLogToFile(3, "./sdklog", false);
//设置报警回调函数
if (fMSFCallBack_V31 == null) {
fMSFCallBack_V31 = new FMSGCallBack_V31();
Pointer pUser = null;
if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser)) {
System.out.println("设置回调函数失败!");
return;
} else {
System.out.println("设置回调函数成功!");
}
}
HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struNET_DVR_LOCAL_GENERAL_CFG = new HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG();
struNET_DVR_LOCAL_GENERAL_CFG.byResumeUpgradeEnable = 1; //断网续传升级使能,0-关闭(默认),1-开启
struNET_DVR_LOCAL_GENERAL_CFG.byExceptionCbDirectly = 0; 0-通过线程池异常回调,1-直接异常回调给上层
struNET_DVR_LOCAL_GENERAL_CFG.byAlarmJsonPictureSeparate = 1; //设置JSON透传报警数据和图片分离,官方为1,自己修改为0
struNET_DVR_LOCAL_GENERAL_CFG.write();
Pointer pStrNET_DVR_LOCAL_GENERAL_CFG = struNET_DVR_LOCAL_GENERAL_CFG.getPointer();
hCNetSDK.NET_DVR_SetSDKLocalCfg(17, pStrNET_DVR_LOCAL_GENERAL_CFG);
//改为从数据库读取摄像头得IP,账号密码来登录设备
QueryWrapper<Camera> wrapper = new QueryWrapper<>();
wrapper.select("userName","password","ip").eq("del_flag","0");
List<Camera> cameraList = cameraMapper.selectList(wrapper);
for (int i = 0; i < cameraList.size(); i++) {
if (!cameraList.get(i).getIp().isEmpty() && !cameraList.get(i).getUsername().isEmpty() && !cameraList.get(i).getPassword().isEmpty()){
login_V40(i, cameraList.get(i).getIp().trim(), Short.parseShort(wPort), cameraList.get(i).getUsername().trim(), cameraList.get(i).getPassword().trim());
if (lUserID[i] < 0) {
System.out.println("登录设备失败,错误代码:" + hCNetSDK.NET_DVR_GetLastError());
return;
}
}
}
for (int i = 0; i < cameraList.size(); i++) {
// 设置布防参数
HCNetSDK.NET_DVR_SETUPALARM_PARAM struSetupParam = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
struSetupParam.dwSize = struSetupParam.size();
if ("0".equals(String.valueOf(i))) {
struSetupParam.byLevel = 0;
} else {
struSetupParam.byLevel = 1;
}
struSetupParam.byAlarmInfoType = 1;
struSetupParam.byDeployType = 0;
struSetupParam.write();
// 布防设备并保存布防句柄
lAlarmHandle[i] = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID[i], struSetupParam);
if (lAlarmHandle[i] < 0) {
System.out.println("布防失败,错误代码:" + hCNetSDK.NET_DVR_GetLastError() + ",设备IP:" + cameraList.get(i).getIp());
// 跳过当前设备,继续下一个设备的布防
continue;
} else {
System.out.println(cameraList.get(i).getIp() + ":设备布防成功");
}
}
// 关闭所有布防句柄和设备登录句柄,不需要执行
// for (int i = 0; i < sDeviceIP.length; i++) {
// System.out.println("关闭布防和登录句柄");
// hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle[i]);
// hCNetSDK.NET_DVR_Logout(lUserID[i]);
// //释放SDK
// hCNetSDK.NET_DVR_Cleanup();
// }
}
}
2、 报警回调函数:FMSGCallBack_V31(版本不同回调函数不同),在回调函数里会调用AlarmDataParse,报警类型和报警内容的主要方法也是在AlarmDataParse中,所以改写AlarmDataParse即可。
import com.sun.jna.Pointer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class AlarmDataParse {
//注入MQ模板用来发送消息
@Resource
private RocketMQTemplate rocketMQTemplate;
//建一个静态的本类
private static AlarmDataParse parse;
private static int lCommands;
//初始化本类,这样下面就可以调用MQ发送消息了
@PostConstruct
public void init() {
parse = this;
}
public static void alarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) throws IOException {
lCommands = lCommand;
String sTime;
String MonitoringSiteID;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//封装所有报警数据
Map<String,Map<String,Object>> map = new HashMap();
//lCommand是传的报警类型
switch (lCommand) {
case HCNetSDK.COMM_ISAPI_ALARM: //ISAPI协议报警信息
Map isapiMap = new HashMap();
System.out.println("发送ISAPI协议报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_DVR_ALARM_ISAPI_INFO struEventISAPI = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO();
struEventISAPI.write();//write() 方法会把结构体的所有字段固定住,使原生函数可以访问
Pointer pEventISAPI = struEventISAPI.getPointer();//在java中都是值传递,但是因为使用JNA框架,目标函数是C/C++是有地址变量的,很多时候都需要将变量的结果带回,此处借用Pointer指针数据类型,用于匹配转换映射函数的指针变量。
pEventISAPI.write(0, pAlarmInfo.getByteArray(0, struEventISAPI.size()), 0, struEventISAPI.size());
struEventISAPI.read();
String sAlarmInfo = new String(pAlarmer.sDeviceIP).trim();
//报警数据类型:0- invalid,1- xml,2- json
sAlarmInfo = "报警设备IP:" + sAlarmInfo + ":ISAPI协议报警信息, 数据格式:" + struEventISAPI.byDataType +
", 图片个数:" + struEventISAPI.byPicturesNumber;
//打印报警基本参数
// System.out.println(sAlarmInfo);
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
String curTime1 = sf1.format(System.currentTimeMillis());
//报警数据打印
HCNetSDK.BYTE_ARRAY m_strISAPIData = new HCNetSDK.BYTE_ARRAY(struEventISAPI.dwAlarmDataLen);
m_strISAPIData.write();
Pointer pPlateInfo = m_strISAPIData.getPointer();
pPlateInfo.write(0, struEventISAPI.pAlarmData.getByteArray(0, m_strISAPIData.size()), 0, m_strISAPIData.size());
m_strISAPIData.read();
//保存图片BASE64
List<String> imgIsapiList = new ArrayList<>();
//图片数据保存
for (int i = 0; i < struEventISAPI.byPicturesNumber; i++) {
HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA struPicData = new HCNetSDK.NET_DVR_ALARM_ISAPI_PICDATA();
struPicData.write();
Pointer pPicData = struPicData.getPointer();
pPicData.write(0, struEventISAPI.pPicPackData.getByteArray(i * struPicData.size(), struPicData.size()), 0, struPicData.size());
struPicData.read();
FileOutputStream fout;
try {
String filename = "E:/pic/" + new String(pAlarmer.sDeviceIP).trim() + curTime1 +
"_ISAPIPic_" + i + "_" + new String(struPicData.szFilename).trim() + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = struPicData.pPicData.getByteBuffer(offset, struPicData.dwPicLen);
byte[] bytes = new byte[struPicData.dwPicLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgIsapiList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//图片BASE64集合
isapiMap.put("imgIsapiList", imgIsapiList);
//主要数据
isapiMap.put("pAlarmData", new String(m_strISAPIData.byValue).trim() + "\n");
//ISAP分析
map.put("isapiMap", isapiMap);
break;
case HCNetSDK.COMM_FIREDETECTION_ALARM://烟火检测
Map fireMap = new HashMap();
System.out.println("发送烟火检测报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_DVR_FIREDETECTION_ALARM struFireDecAlarm = new HCNetSDK.NET_DVR_FIREDETECTION_ALARM();
struFireDecAlarm.write();
Pointer pFireDecAlarm = struFireDecAlarm.getPointer();
pFireDecAlarm.write(0, pAlarmInfo.getByteArray(0, struFireDecAlarm.size()), 0, struFireDecAlarm.size());
struFireDecAlarm.read();
String sFireDecAlarmInfo = "火点最高温度 :" + struFireDecAlarm.wFireMaxTemperature + ",火点目标距离:" + struFireDecAlarm.wTargetDistance;
//保存图片BASE64
List imgFireList = new ArrayList();
List imgHotList = new ArrayList();
//可见光图片保存
if ((struFireDecAlarm.dwVisiblePicLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(System.currentTimeMillis());
FileOutputStream fout;
try {
String filename = "E:/pic/" + newName + "_FireDecAlarm" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = struFireDecAlarm.pVisiblePicBuf.getByteBuffer(offset, struFireDecAlarm.dwVisiblePicLen);
byte[] bytes = new byte[struFireDecAlarm.dwVisiblePicLen];
//保存BASE64到LIST
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgFireList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//热成像图片保存
if ((struFireDecAlarm.dwPicDataLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(System.currentTimeMillis());
FileOutputStream fout;
try {
String filename = "E:/pic/" + newName + "_ThermalFireAlarm" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = struFireDecAlarm.pBuffer.getByteBuffer(offset, struFireDecAlarm.dwPicDataLen);
byte[] bytes = new byte[struFireDecAlarm.dwPicDataLen];
//保存BASE64到LIST
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgHotList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建烟火报警类型, 报警子类型。0~火点检测报警,1~烟雾检测报警,2~烟火报警
Map<String, String> fireTypeMap = new HashMap<>();
fireTypeMap.put("0", "火点检测报警");
fireTypeMap.put("1", "烟雾检测报警");
fireTypeMap.put("2", "烟火报警");
//时间
fireMap.put("dwAbsTime", sdf.format(System.currentTimeMillis()));
//图片BASE64集合
//可见光图片保存
fireMap.put("imgFireList", imgFireList);
//热成像图片保存
fireMap.put("imgHotList", imgHotList);
//设备IP
fireMap.put("ipV4", new String(struFireDecAlarm.struDevInfo.struDevIP.sIpV4).trim());
//设备通道号
fireMap.put("channel", struFireDecAlarm.struDevInfo.byChannel);
//报警内容
fireMap.put("sFireDecAlarmInfo", sFireDecAlarmInfo);
//烟火分析
map.put("fireMap", fireMap);
break;
//AI开放平台接入视频检测报警信息
case HCNetSDK.COMM_UPLOAD_AIOP_VIDEO:
Map aiopMap = new HashMap();
System.out.println("发送AI平台实时报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_AIOP_VIDEO_HEAD struAIOPVideo = new HCNetSDK.NET_AIOP_VIDEO_HEAD();
struAIOPVideo.write();
Pointer apiOPVideo = struAIOPVideo.getPointer();
apiOPVideo.write(0, pAlarmInfo.getByteArray(0, struAIOPVideo.size()), 0, struAIOPVideo.size());
struAIOPVideo.read();
//报警时间
String strTime = String.format("%04d", struAIOPVideo.struTime.wYear) +
String.format("%02d", struAIOPVideo.struTime.wMonth) +
String.format("%02d", struAIOPVideo.struTime.wDay) +
String.format("%02d", struAIOPVideo.struTime.wHour) +
String.format("%02d", struAIOPVideo.struTime.wMinute) +
String.format("%02d", struAIOPVideo.struTime.wSecond) +
String.format("%02d", struAIOPVideo.struTime.wMilliSec);
//AIOPData数据
//将字节写入文件
List imgAiopList = new ArrayList();
//图片数据保存
if (struAIOPVideo.dwPictureSize > 0) {
FileOutputStream fout;
try {
String filename = "E:/pic/" + new String(pAlarmer.sDeviceIP).trim() +
"_" + strTime + "_VideoPic.jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = struAIOPVideo.pBufferPicture.getByteBuffer(offset, struAIOPVideo.dwPictureSize);
byte[] bytes = new byte[struAIOPVideo.dwPictureSize];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgAiopList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装数据
long offsets = 0;
//图片
aiopMap.put("imgAiopList", imgAiopList);
// aiopMap.put("lCommand", "AI平台实时报警");
//报警时间
aiopMap.put("struTime", String.format("%04d", struAIOPVideo.struTime.wYear) + "-" +
String.format("%02d", struAIOPVideo.struTime.wMonth) + "-" +
String.format("%02d", struAIOPVideo.struTime.wDay) + " " +
String.format("%02d", struAIOPVideo.struTime.wHour) + ":" +
String.format("%02d", struAIOPVideo.struTime.wMinute) + ":" +
String.format("%02d", struAIOPVideo.struTime.wSecond));
//主要数据
aiopMap.put("pAlarmData", struAIOPVideo.pBufferAIOPData.getString(offsets));
//设备IP
aiopMap.put("ipV4", new String(pAlarmer.sDeviceIP).trim());
//设备通道号
aiopMap.put("channel", struAIOPVideo.dwChannel);
//AI视频实时分析
map.put("aiopMap", aiopMap);
break;
//行为分析信息
case HCNetSDK.COMM_ALARM_RULE:
Map ruleMap = new HashMap();
System.out.println("发送行为分析报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_VCA_RULE_ALARM strVcaAlarm = new HCNetSDK.NET_VCA_RULE_ALARM();
strVcaAlarm.write();
Pointer pVCAInfo = strVcaAlarm.getPointer();
pVCAInfo.write(0, pAlarmInfo.getByteArray(0, strVcaAlarm.size()), 0, strVcaAlarm.size());
strVcaAlarm.read();
//将字节写入文件
List imgRuleList = new ArrayList();
switch (strVcaAlarm.struRuleInfo.wEventTypeEx) {
case 44: //玩手机
// ruleMap.put("lCommand", "玩手机报警发生");
//图片保存
if ((strVcaAlarm.dwPicDataLen > 0) && (strVcaAlarm.byPicTransType == 0)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(System.currentTimeMillis());
FileOutputStream fout;
try {
String filename = "E:/pic/" + newName + "PLAY_CELLPHONE_" + ".jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strVcaAlarm.pImage.getByteBuffer(offset, strVcaAlarm.dwPicDataLen);
byte[] bytes = new byte[strVcaAlarm.dwPicDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgRuleList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
// ruleMap.put("lCommand", strVcaAlarm.struRuleInfo.wEventTypeEx);
break;
}
//图片
ruleMap.put("imgRuleList", imgRuleList);
//时间
ruleMap.put("dwAbsTime", sdf.format(strVcaAlarm.dwAbsTime));
//设备IP
ruleMap.put("ipV4", new String(strVcaAlarm.struDevInfo.struDevIP.sIpV4).trim());
//设备通道号
ruleMap.put("channel", strVcaAlarm.struDevInfo.byChannel);
//行为分析
map.put("ruleMap", ruleMap);
break;
//AI开放平台接入轮询视频检测报警信息
case HCNetSDK.COMM_UPLOAD_AIOP_POLLING_VIDEO:
Map aiopPollMap = new HashMap();
System.out.println("发送AI平台轮询报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_AIOP_POLLING_VIDEO_HEAD strAiopPollingVideo = new HCNetSDK.NET_AIOP_POLLING_VIDEO_HEAD();
strAiopPollingVideo.write();
Pointer apiopPollingVideo = strAiopPollingVideo.getPointer();
apiopPollingVideo.write(0, pAlarmInfo.getByteArray(0, strAiopPollingVideo.size()), 0, strAiopPollingVideo.size());
strAiopPollingVideo.read();
String AiopPollingVideoTime = "" + String.format("%04d", strAiopPollingVideo.struTime.wYear) +
String.format("%02d", strAiopPollingVideo.struTime.wMonth) +
String.format("%02d", strAiopPollingVideo.struTime.wDay) +
String.format("%02d", strAiopPollingVideo.struTime.wHour) +
String.format("%02d", strAiopPollingVideo.struTime.wMinute) +
String.format("%02d", strAiopPollingVideo.struTime.wSecond) +
String.format("%03d", strAiopPollingVideo.struTime.wMilliSec);
List imgAiopPollList = new ArrayList();
if (strAiopPollingVideo.dwPictureSize > 0) {
FileOutputStream fout;
try {
String filename = "E:/pic/" + new String(pAlarmer.sDeviceIP).trim() +
"_" + AiopPollingVideoTime + "_PollingVideo.jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strAiopPollingVideo.pBufferPicture.getByteBuffer(offset, strAiopPollingVideo.dwPictureSize);
byte[] bytes = new byte[strAiopPollingVideo.dwPictureSize];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgAiopPollList.add(Base64.getEncoder().encodeToString(bytes));
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装数据
long offsetAiop = 0;
//图片
aiopPollMap.put("imgAiopPollList", imgAiopPollList);
// aiopPollMap.put("lCommand", "AI平台轮询报警");
//时间
aiopPollMap.put("AiopPollingVideoTime", String.format("%04d", strAiopPollingVideo.struTime.wYear) + "-" +
String.format("%02d", strAiopPollingVideo.struTime.wMonth) + "-" +
String.format("%02d", strAiopPollingVideo.struTime.wDay) + " " +
String.format("%02d", strAiopPollingVideo.struTime.wHour) + ":" +
String.format("%02d", strAiopPollingVideo.struTime.wMinute) + ":" +
String.format("%02d", strAiopPollingVideo.struTime.wSecond)
);
//主要数据
aiopPollMap.put("pAlarmData", strAiopPollingVideo.pBufferAIOPData.getString(offsetAiop));
//设备IP
aiopPollMap.put("ipV4", new String(pAlarmer.sDeviceIP).trim());
//设备通道号
aiopPollMap.put("channel", strAiopPollingVideo.dwChannel);
//AI视频轮询分析
map.put("aiopPollMap", aiopPollMap);
break;
case HCNetSDK.COMM_ALARM_TFS: //道路违章取证报警
Map<String, Object> tfsMap = new HashMap<>();
System.out.println("发送道路违章报警消息================" + new String(pAlarmer.sDeviceIP));
HCNetSDK.NET_DVR_TFS_ALARM strTfsAlarm = new HCNetSDK.NET_DVR_TFS_ALARM();
strTfsAlarm.write();
Pointer pTfsAlarm = strTfsAlarm.getPointer();
pTfsAlarm.write(0, pAlarmInfo.getByteArray(0, strTfsAlarm.size()), 0, strTfsAlarm.size());
strTfsAlarm.read();
sTime = CommonUtil.parseTime(strTfsAlarm.dwAbsTime); //报警绝对时间
int illegalType = strTfsAlarm.dwIllegalType; // 违章类型,当dwIllegalType值为0xffffffff时使用byIllegalCode
MonitoringSiteID = Arrays.toString(strTfsAlarm.byMonitoringSiteID); //监控点编号
// 车牌信息
String plateInfo = new String(strTfsAlarm.struPlateInfo.sLicense);
//报警图片信息
List<String> imgTfsList = new ArrayList<>();
// for (int i = 0; i < strTfsAlarm.dwPicNum; i++) {
for (int i = 0; i < 1; i++) {
if (strTfsAlarm.struPicInfo[i].dwDataLen > 0) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(System.currentTimeMillis());
try {
String filename = "E:/pic/" + newName + "_type[" + strTfsAlarm.struPicInfo[i].byType + "]_TfsPlate.jpg";
try (FileOutputStream fout = new FileOutputStream(filename)) {
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strTfsAlarm.struPicInfo[i].pBuffer.getByteBuffer(offset, strTfsAlarm.struPicInfo[i].dwDataLen);
byte[] bytes = new byte[strTfsAlarm.struPicInfo[i].dwDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
imgTfsList.add(Base64.getEncoder().encodeToString(bytes));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//违章类型,当dwIllegalType值为0xffffffff时使用byIllegalCode
tfsMap.put("illegalType", illegalType != 0xffffffff ? String.valueOf(illegalType) : new String(strTfsAlarm.byIllegalCode));
//当前车速
tfsMap.put("speed", strTfsAlarm.struAIDInfo.byCurrentSpeed);
//当前限速
tfsMap.put("speedLimit", strTfsAlarm.struAIDInfo.bySpeedLimit);
//车牌号
tfsMap.put("plateInfo", plateInfo);
//时间
tfsMap.put("dwAbsTime", sdf.format(System.currentTimeMillis()));
//图片
tfsMap.put("imgTfsList", imgTfsList);
//设备IP
tfsMap.put("ipV4", new String(pAlarmer.sDeviceIP).trim());
//设备通道号
tfsMap.put("channel", strTfsAlarm.struDevInfo.byChannel);
map.put("tfsMap", tfsMap);
break;
default:
break;
}
//把报警具体数据和图片数据写入MQ,采用异步
if (!map.isEmpty()) {
byte[] bytes = new byte[0];
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(map);
bytes = os.toByteArray();
oos.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
SendCallback sendCallback = new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("消息发送成功:" + "ID:" + sendResult.getMsgId() + ",时间:" + sdf.format(System.currentTimeMillis()) + ",报警类型:" + lCommands);
}
@Override
public void onException(Throwable throwable) {
System.out.println("消息发送失败:" + ",时间:" + sdf.format(System.currentTimeMillis()) + ",报警类型:" + lCommands);
}
};
parse.rocketMQTemplate.asyncSend("ISAPI_FIRE_AI", bytes, sendCallback);
}
}
}