springboot 对接海康设备
- 新建接口HCNetSDK
import com.stian.util.ClientDemoUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.examples.win32.W32API;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import java.nio.ByteBuffer;
public interface HCNetSDK extends StdCallLibrary {
HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary(ClientDemoUtil.DLL_PATH+"HCNetSDK.dll", HCNetSDK.class);
public static final int SERIALNO_LEN = 48;
public static final int NAME_LEN = 32;
public static final int PASSWD_LEN = 16;
public static final int MAX_DISKNUM_V30 = 33;
public static final int MAX_AUDIO_V30 = 2;
public static final int MAX_LINK = 6;
public static final int MAX_ANALOG_CHANNUM = 32;
public static final int MAX_ANALOG_ALARMOUT = 32;
public static final int MAX_ANALOG_ALARMIN = 32;
public static final int MAX_IP_DEVICE = 32;
public static final int MAX_IP_CHANNEL = 32;
public static final int MAX_IP_ALARMIN = 128;
public static final int MAX_IP_ALARMOUT = 64;
public static final int MAX_CHANNUM_V30 = (MAX_ANALOG_CHANNUM + MAX_IP_CHANNEL);
public static final int MAX_ALARMOUT_V30 = (MAX_ANALOG_ALARMOUT + MAX_IP_ALARMOUT);
public static final int MAX_ALARMIN_V30 = (MAX_ANALOG_ALARMIN + MAX_IP_ALARMIN);
public static final int NET_DVR_GET_IPPARACFG = 1048;
public static final int NET_DVR_SET_IPPARACFG = 1049;
public static final int NET_DVR_SYSHEAD = 1;
public static final int NET_DVR_STREAMDATA = 2;
public static final int NET_DVR_AUDIOSTREAMDATA = 3;
public static final int NET_DVR_STD_VIDEODATA = 4;
public static final int NET_DVR_STD_AUDIODATA = 5;
public static class NET_DVR_DEVICEINFO_V30 extends Structure{
public byte[] sSerialNumber = new byte[SERIALNO_LEN];
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte[] byRes1 = new byte[24];
}
boolean NET_DVR_Logout_V30(NativeLong lUserID);
public static class NET_DVR_IPPARACFG extends Structure {
public int dwSize;
public NET_DVR_IPDEVINFO[] struIPDevInfo = new NET_DVR_IPDEVINFO[MAX_IP_DEVICE];
public byte[] byAnalogChanEnable = new byte[MAX_ANALOG_CHANNUM];
public NET_DVR_IPCHANINFO[] struIPChanInfo = new NET_DVR_IPCHANINFO[MAX_IP_CHANNEL];
}
public static class NET_DVR_IPDEVINFO extends Structure {
public int dwEnable;
public byte[] sUserName = new byte[NAME_LEN];
public byte[] sPassword = new byte[PASSWD_LEN];
public NET_DVR_IPADDR struIP = new NET_DVR_IPADDR();
public short wDVRPort;
public byte[] byres = new byte[34];
}
public static class NET_DVR_IPCHANINFO extends Structure {
public byte byEnable;
public byte byIPID;
public byte byChannel;
public byte[] byres = new byte[33];
}
public static class NET_DVR_IPADDR extends Structure {
public byte[] sIpV4 = new byte[16];
public byte[] byRes = new byte[128];
public String toString() {
return "NET_DVR_IPADDR.sIpV4: " + new String(sIpV4) + "\n" + "NET_DVR_IPADDR.byRes: " + new String(byRes) + "\n";
}
}
boolean NET_DVR_Init();
boolean NET_DVR_SetConnectTime(int dwWaitTime, int dwTryTimes );
boolean NET_DVR_SetReconnect(int dwInterval, boolean bEnableRecon );
int NET_DVR_GetLastError();
NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
public static class NET_DVR_WORKSTATE_V30 extends Structure {
public int dwDeviceStatic;
public NET_DVR_DISKSTATE[] struHardDiskStatic = new NET_DVR_DISKSTATE[MAX_DISKNUM_V30];
public NET_DVR_CHANNELSTATE_V30[] struChanStatic = new NET_DVR_CHANNELSTATE_V30[MAX_CHANNUM_V30];
public byte[] byAlarmInStatic = new byte[MAX_ALARMIN_V30];
public byte[] byAlarmOutStatic = new byte[MAX_ALARMOUT_V30];
public int dwLocalDisplay;
public byte [] byAudioChanStatus = new byte[MAX_AUDIO_V30];
public byte[] byRes = new byte[10];
}
public static class NET_DVR_DISKSTATE extends Structure {
public int dwVolume;
public int dwFreeSpace;
public int dwHardDiskStatic;
}
public static class NET_DVR_CHANNELSTATE_V30 extends Structure {
public byte byRecordStatic;
public byte bySignalStatic;
public byte byHardwareStatic;
public byte reservedData;
public int dwBitRate;
public int dwLinkNum;
public NET_DVR_IPADDR[] struClientIP = new NET_DVR_IPADDR[MAX_LINK];
public int dwIPLinkNum;
public byte[] byRes = new byte[12];
}
boolean NET_DVR_GetDVRWorkState_V30(NativeLong lUserID, NET_DVR_WORKSTATE_V30 lpWorkState);
boolean NET_DVR_GetDVRConfig(NativeLong lUserID, int dwCommand, NativeLong lChannel, Pointer lpOutBuffer, int dwOutBufferSize, IntByReference lpBytesReturned);
boolean NET_DVR_Logout(NativeLong lUserID);
boolean NET_DVR_Cleanup();
public static class NET_DVR_CLIENTINFO extends Structure {
public NativeLong lChannel;
public NativeLong lLinkMode;
public W32API.HWND hPlayWnd;
public String sMultiCastIP;
}
public static interface FRealDataCallBack_V30 extends StdCallCallback {
public void invoke(NativeLong lRealHandle, int dwDataType,
ByteByReference pBuffer, int dwBufSize, Pointer pUser);
}
NativeLong NET_DVR_RealPlay_V30(NativeLong lUserID, NET_DVR_CLIENTINFO lpClientInfo, FRealDataCallBack_V30 fRealDataCallBack_V30, Pointer pUser , boolean bBlocked );
boolean NET_DVR_SaveRealData(NativeLong lRealHandle,String sFileName);
boolean NET_DVR_StopRealPlay(NativeLong lRealHandle);
public static class NET_DVR_JPEGPARA extends Structure {
public short wPicSize;
public short wPicQuality;
}
boolean NET_DVR_CaptureJPEGPicture_NEW(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara, ByteBuffer sJpegPicBuffer, int dwPicSize, IntByReference lpSizeReturned);
boolean NET_DVR_SetExceptionCallBack_V30(int nMessage, int hWnd, FExceptionCallBack fExceptionCallBack, Pointer pUser);
public static interface FExceptionCallBack extends StdCallCallback {
public void invoke(int dwType, NativeLong lUserID, NativeLong lHandle, Pointer pUser);
}
}
- 工具类ClientDemoUtil
import com.stian.sdks.ClientDemo;
import java.io.UnsupportedEncodingException;
public class ClientDemoUtil {
public static String DLL_PATH;
static{
String path=(ClientDemo.class.getResource("/sdk/").getPath()).replaceAll("%20", " ").substring(1).replace("/", "\\");
try {
DLL_PATH= java.net.URLDecoder.decode(path,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
- 新建类TestHikvision
@Configuration
public class TestHikvision {
static HCNetSDK sdk = HCNetSDK.INSTANCE;
private Camera getParameter(Camera cameraInfo) {
NativeLong channel = new NativeLong(1);
cameraInfo.setChannel(channel);
if (!sdk.NET_DVR_Init()) {
System.out.println("初始化失败..................");
}
HCNetSDK.NET_DVR_DEVICEINFO_V30 deInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
NativeLong id = sdk.NET_DVR_Login_V30(cameraInfo.getCameraIp(), (short) cameraInfo.getPort(),
cameraInfo.getUserName(), cameraInfo.getUserPwd(), deInfo);
cameraInfo.setUserId(id);
return cameraInfo;
}
public void getDVRConfig(Camera cameraInfo) {
if (!sdk.NET_DVR_Init()) {
return;
}
HCNetSDK.NET_DVR_DEVICEINFO_V30 deInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
NativeLong id = sdk.NET_DVR_Login_V30(cameraInfo.getCameraIp(), (short) cameraInfo.getPort(),
cameraInfo.getUserName(), cameraInfo.getUserPwd(), deInfo);
cameraInfo.setUserId(id);
if (cameraInfo.getUserId().intValue() < 0) {
System.out.println("设备注册失败"+sdk.NET_DVR_GetLastError());
return;
}
HCNetSDK.NET_DVR_WORKSTATE_V30 devwork = new HCNetSDK.NET_DVR_WORKSTATE_V30();
if (!sdk.NET_DVR_GetDVRWorkState_V30(cameraInfo.getUserId(), devwork)) {
System.out.println("返回设备状态失败");
}
IntByReference ibrBytesReturned = new IntByReference(0);
HCNetSDK.NET_DVR_IPPARACFG ipcfg = new HCNetSDK.NET_DVR_IPPARACFG();
ipcfg.write();
Pointer lpIpParaConfig = ipcfg.getPointer();
sdk.NET_DVR_GetDVRConfig(cameraInfo.getUserId(), HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(0),
lpIpParaConfig, ipcfg.size(), ibrBytesReturned);
ipcfg.read();
System.out.print("IP地址:" + cameraInfo.getCameraIp());
System.out.println("|设备状态:" + devwork.dwDeviceStatic);
for (int i = 0; i < deInfo.byChanNum; i++) {
System.out.print("Camera" + i + 1);
System.out.print("|是否录像:" + devwork.struChanStatic[i].byRecordStatic);
System.out.print("|信号状态:" + devwork.struChanStatic[i].bySignalStatic);
System.out.println("|硬件状态:" + devwork.struChanStatic[i].byHardwareStatic);
}
sdk.NET_DVR_Logout(cameraInfo.getUserId());
sdk.NET_DVR_Cleanup();
}
public void getDVRPic(Camera cameraInfo){
Camera parameter = getParameter(cameraInfo);
if (parameter.getUserId().intValue() < 0) {
System.out.println("设备注册失败"+sdk.NET_DVR_GetLastError());
return;
} else {
System.out.println("id:" + parameter.getUserId().intValue());
}
HCNetSDK.NET_DVR_WORKSTATE_V30 devwork = new HCNetSDK.NET_DVR_WORKSTATE_V30();
if (!sdk.NET_DVR_GetDVRWorkState_V30(parameter.getUserId(), devwork)) {
System.out.println("返回设备状态失败");
}
HCNetSDK.NET_DVR_JPEGPARA jpeg = new HCNetSDK.NET_DVR_JPEGPARA();
jpeg.wPicSize = 2;
jpeg.wPicQuality = 2;
IntByReference a = new IntByReference();
ByteBuffer jpegBuffer = ByteBuffer.allocate(1024 * 1024);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String fileName = parameter.getPath();
File files = new File(fileName);
if(!files.exists()){
files.mkdirs();
}
if (files.isDirectory()){
String[] imgNumber = files.list();
if (imgNumber.length == 50){
A:for (int i = 0; i < imgNumber.length; i++) {
try {
Path path = Paths.get(fileName+imgNumber[i]);
BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
BasicFileAttributes attr = basicview.readAttributes();
Date createDate = new Date(attr.creationTime().toMillis());
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
deleteFile(fileName+imgNumber[0]);
System.out.println(imgNumber[i]+"--------"+sdf1.format(createDate));
break A;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
String fileNameString = fileName+"/"+sdf.format(date)+".jpg";
File file = new File(fileNameString);
boolean is = sdk.NET_DVR_CaptureJPEGPicture_NEW(parameter.getUserId(), parameter.getChannel(), jpeg,
jpegBuffer, 1024 * 1024, a);
if (is) {
System.out.println("抓取成功,返回长度:" + a.getValue());
} else {
System.out.println("抓取失败:"+sdk.NET_DVR_GetLastError());
}
BufferedOutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(file));
outputStream.write(jpegBuffer.array(), 0, a.getValue());
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
sdk.NET_DVR_Logout(parameter.getUserId());
sdk.NET_DVR_Cleanup();
}
public void getImages() throws IOException {
ICameraService cameraService = (ICameraService) ApplicationContextUtil.getBean("cameraService");
QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id","camera_ip","port","user_name","user_pwd","path","create_time");
List<Camera> cameraList = cameraService.list(queryWrapper);
TestHikvision testHikvision = new TestHikvision();
if (cameraList != null){
for (Camera c : cameraList) {
new Thread(new Runnable() {
@Override
public void run() {
testHikvision.getDVRConfig(c);
testHikvision.getDVRPic(c);
}
}).start();
}
}
}
@Scheduled(fixedRate = 500)
public void test1(){
try {
this.getImages();
}catch (IOException e){
e.printStackTrace();
}
}
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
if (file.exists() && file.isFile()) {
if (file.delete()) {
System.out.println("删除单个文件" + fileName + "成功!");
return true;
} else {
System.out.println("删除单个文件" + fileName + "失败!");
return false;
}
} else {
System.out.println("删除单个文件失败:" + fileName + "不存在!");
return false;
}
}
}