jiazx0107@163.com
2023-12-24 8efb5a8cd2c8a6b40e58f4f3fb851d54cf415af9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 
package com.fzzy.protocol.sdkhk.common;
 
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.protocol.sdkhk.HKUtils;
import com.sun.jna.Pointer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
/**
 * 车牌识别回调函数
 */
public class LPRCallBask_V31 implements HCNetSDK.FMSGCallBack_V31 {
 
    Logger log = LoggerFactory.getLogger(LPRCallBask_V31.class);
 
    GatewayRemoteManager gatewayRemoteManager;
 
    //报警信息回调函数
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        AlarmDataHandle(lCommand, pAlarmer, pAlarmInfo, dwBufLen, pUser);
        return true;
    }
 
    public void AlarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        log.info("报警事件类型: lCommand:" + Integer.toHexString(lCommand));
 
        String MonitoringSiteID;
        //lCommand是传的报警类型
        switch (lCommand) {
 
            case HCNetSDK.COMM_ITS_PLATE_RESULT://交通抓拍结果(新报警信息)
                HCNetSDK.NET_ITS_PLATE_RESULT strItsPlateResult = new HCNetSDK.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 sLicense = new String(strItsPlateResult.struPlateInfo.sLicense, "GBK");
                    byte VehicleType = strItsPlateResult.byVehicleType;  //0-其他车辆,1-小型车,2-大型车,3- 行人触发,4- 二轮车触发,5- 三轮车触发,6- 机动车触发
                    MonitoringSiteID = new String(strItsPlateResult.byMonitoringSiteID);
 
                    String deviceIp = new String(pAlarmer.sDeviceIP, "GBK");
                    String deviceSn = new String(pAlarmer.sSerialNumber, "GBK");
 
                    if (StringUtils.isNotEmpty(sLicense)) sLicense = sLicense.trim();
                    if (StringUtils.isNotEmpty(deviceSn)) deviceSn = deviceSn.trim();
                    if (StringUtils.isNotEmpty(deviceIp)) deviceIp = deviceIp.trim();
 
 
                    //log.info("车牌号:" + sLicense + ":车辆类型:" + VehicleType + ":监控点编号:" + MonitoringSiteID + "-设备IP=" + deviceIp + "-SN=" + deviceSn);
 
                    log.info("车牌号:" + sLicense + ":车辆类型:" + VehicleType + "-设备IP=" + deviceIp + "-SN=" + deviceSn);
 
                    //开始执行推送
                    GatewayDevice device = GatewayUtils.getCacheByDeviceSn(deviceSn);
                    if (null == device) {
                        log.error("车牌识别未获取到相关配置设备信息----无法执行推送");
                        return;
                    }
 
                    if (null == gatewayRemoteManager) {
                        gatewayRemoteManager = SpringUtil.getBean(GatewayRemoteManager.class);
                    }
 
                    //数据封装推送
                    BaseReqData reqData = new BaseReqData(device);
 
                    if (sLicense.startsWith("蓝") || sLicense.startsWith("黄")) {
                        sLicense = sLicense.replace("蓝", "").replace("黄", "");
                    }
                    reqData.setCarNumber(sLicense);
                    gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportLprData(reqData);
 
                } catch (IOException e) {
                    e.printStackTrace();
                }
 
 
                //报警图片保存,车牌,车辆图片
                for (int i = 0; i < strItsPlateResult.dwPicNum; i++) {
                    if (strItsPlateResult.struPicInfo[i].dwDataLen > 0) {
                        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                        String newName = sf.format(new Date());
                        FileOutputStream fout;
                        try {
                            // String filename = "../pic/" + newName + "_type[" + strItsPlateResult.struPicInfo[i].byType + "]_ItsPlate.jpg";
 
                            String basePath = HKUtils.getImgBasePath(false);
 
                            String filename = basePath + newName + ".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 (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            case HCNetSDK.COMM_ITS_PARK_VEHICLE: //停车场数据上传
                HCNetSDK.NET_ITS_PARK_VEHICLE strParkVehicle = new HCNetSDK.NET_ITS_PARK_VEHICLE();
                strParkVehicle.write();
                Pointer pstrParkVehicle = strParkVehicle.getPointer();
                pstrParkVehicle.write(0, pAlarmInfo.getByteArray(0, strParkVehicle.size()), 0, strParkVehicle.size());
                strParkVehicle.read();
                try {
                    //  byte ParkError = strParkVehicle.byParkError; //停车异常:0- 正常,1- 异常
                    //   String ParkingNo = new String(strParkVehicle.byParkingNo, "UTF-8"); //车位编号
                    byte LocationStatus = strParkVehicle.byLocationStatus; //车位车辆状态 0- 无车,1- 有车
                    MonitoringSiteID = strParkVehicle.byMonitoringSiteID.toString();
                    String plateNo = new String(strParkVehicle.struPlateInfo.sLicense, "GBK"); //车牌号
 
                    log.info("车牌号:" + plateNo + ":车位状态:" + LocationStatus + ":监控点编号:" + MonitoringSiteID);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //报警图片信息
                for (int i = 0; i < strParkVehicle.dwPicNum; i++) {
                    if (strParkVehicle.struPicInfo[i].dwDataLen > 0) {
                        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                        String newName = sf.format(new Date());
                        FileOutputStream fout;
                        try {
                            String basePath = HKUtils.getImgBasePath(false);
                            String filename = basePath + newName + "_ParkVehicle.jpg";
                            fout = new FileOutputStream(filename);
                            //将字节写入文件
                            long offset = 0;
                            ByteBuffer buffers = strParkVehicle.struPicInfo[i].pBuffer.getByteBuffer(offset, strParkVehicle.struPicInfo[i].dwDataLen);
                            byte[] bytes = new byte[strParkVehicle.struPicInfo[i].dwDataLen];
                            buffers.rewind();
                            buffers.get(bytes);
                            fout.write(bytes);
                            fout.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            default:
                System.out.println("报警类型" + Integer.toHexString(lCommand));
                break;
        }
    }
}