add:海康威视截图接入
This commit is contained in:
@@ -1,12 +1,53 @@
|
||||
package org.nl.common.domain.constant;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.nl.common.hikvision.HikvisionSnapshotUtil;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* s
|
||||
* @author ZZQ
|
||||
* @Date 2022/12/26 9:29 上午
|
||||
*/
|
||||
public class DictConstantPool {
|
||||
@Service
|
||||
@Data
|
||||
@ToString
|
||||
public class DictConstantPool {
|
||||
@Value("${hikvison.ins}")
|
||||
public String ins;
|
||||
@Value("${hikvison.path}")
|
||||
public String path;
|
||||
@Value("${hikvison.ip}")
|
||||
public String ip;
|
||||
@Value("${hikvison.port}")
|
||||
public Short port;
|
||||
@Value("${hikvison.username}")
|
||||
public String username;
|
||||
@Value("${hikvison.password}")
|
||||
public String password;
|
||||
@Value("${hikvison.channelHttp}")
|
||||
public String channelHttp;
|
||||
@Value("${hikvison.channelSdk}")
|
||||
public Integer channel;
|
||||
|
||||
public static final String DICT_SYS_CODE = "system_type";
|
||||
public static final String DICT_SYS_NAME = "所属系统";
|
||||
|
||||
@PostConstruct
|
||||
public void initHikvisonConfig(){
|
||||
System.out.println("初始化海康卫视配置:"+ this);
|
||||
HikvisionSnapshotUtil.ins = this.ins;
|
||||
HikvisionSnapshotUtil.path = this.path;
|
||||
HikvisionSnapshotUtil.ip = this.ip;
|
||||
HikvisionSnapshotUtil.port = this.port;
|
||||
HikvisionSnapshotUtil.username = this.username;
|
||||
HikvisionSnapshotUtil.channelHttp = this.channelHttp;
|
||||
HikvisionSnapshotUtil.channel = this.channel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package org.nl.common.hikvision;
|
||||
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Structure;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class HikvisionSnapshotUtil {
|
||||
|
||||
public static String ins; //HCNetSDK.dll文件路径
|
||||
public static String path; //文件存储位置
|
||||
public static String ip; //海康威视设备网络搜索应用中摄像头ip
|
||||
public static Short port;//海康威视设备网络搜索应用中摄像头port
|
||||
public static String username;//海康威视设备网络搜索应用中摄像头管理账号admin
|
||||
public static String password;//海康威视设备网络搜索应用中摄像头管理账密码nl1314520
|
||||
public static String channelHttp;//默认
|
||||
public static Integer channel;//默认1
|
||||
// HCNetSDK 接口映射
|
||||
public interface HCNetSDK extends Library {
|
||||
Hikvision截图Demo.HCNetSDK INSTANCE = Native.load(ins, Hikvision截图Demo.HCNetSDK.class);
|
||||
//初始化 SDK 库
|
||||
boolean NET_DVR_Init();
|
||||
|
||||
//登录设备(传入设备 IP、端口、用户名、密码),返回一个用户 ID(后续调用都要用)
|
||||
int NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, Hikvision截图Demo.NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
|
||||
|
||||
//抓拍一张 JPEG 图片保存到本地。
|
||||
boolean NET_DVR_CaptureJPEGPicture(int lUserID, int lChannel, Hikvision截图Demo.NET_DVR_JPEGPARA lpJpegPara, String sPicFileName);
|
||||
|
||||
//注销登录。
|
||||
boolean NET_DVR_Logout(int lUserID);
|
||||
|
||||
////清理 SDK 库
|
||||
boolean NET_DVR_Cleanup();
|
||||
}
|
||||
|
||||
@Structure.FieldOrder({"sSerialNumber", "byAlarmInPortNum", "byAlarmOutPortNum", "byDiskNum",
|
||||
"byDVRType", "byChanNum", "byStartChan", "byAudioChanNum", "byIPChanNum"})
|
||||
public static class NET_DVR_DEVICEINFO_V30 extends Structure {
|
||||
//序列号
|
||||
public byte[] sSerialNumber = new byte[48];
|
||||
//报警输入口个数
|
||||
public byte byAlarmInPortNum;
|
||||
//报警输出口个数
|
||||
public byte byAlarmOutPortNum;
|
||||
//硬盘个数
|
||||
public byte byDiskNum;
|
||||
//DVR 类型
|
||||
public byte byDVRType;
|
||||
//模拟通道数
|
||||
public byte byChanNum;
|
||||
//起始通道号
|
||||
public byte byStartChan;
|
||||
//音频通道数
|
||||
public byte byAudioChanNum;
|
||||
//IP 通道数
|
||||
public byte byIPChanNum;
|
||||
}
|
||||
|
||||
@Structure.FieldOrder({"wPicSize", "wPicQuality"})
|
||||
public static class NET_DVR_JPEGPARA extends Structure {
|
||||
// 0: QCIF, 1: CIF, 2: D1, 3: UXGA...
|
||||
public short wPicSize;
|
||||
// 0: 最差, 1: 较差, 2: 一般, 3: 较好, 4:最好
|
||||
public short wPicQuality;
|
||||
}
|
||||
|
||||
//HCNetSDK 抓图,图片路径允许指定,不指定则走配置
|
||||
public static SdkResponse 截图(String imagePath, String imageName) {
|
||||
if (StringUtils.isEmpty(imageName)){
|
||||
return SdkResponse.requestFail("图片名称未知");
|
||||
}
|
||||
String sFileUrl = imagePath+"\\"+imageName;
|
||||
if (!Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_Init()) {
|
||||
|
||||
return SdkResponse.requestFail("[SDK] 初始化失败");
|
||||
}
|
||||
Hikvision截图Demo.NET_DVR_DEVICEINFO_V30 deviceInfo = new Hikvision截图Demo.NET_DVR_DEVICEINFO_V30();
|
||||
int userId = Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_Login_V30(ip, port, username, password, deviceInfo);
|
||||
if (userId < 0) {
|
||||
System.err.println("[SDK] 登录失败");
|
||||
Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_Cleanup();
|
||||
return SdkResponse.requestFail("[SDK] 登录失败");
|
||||
}
|
||||
|
||||
Hikvision截图Demo.NET_DVR_JPEGPARA jpegPara = new Hikvision截图Demo.NET_DVR_JPEGPARA();
|
||||
// D1
|
||||
jpegPara.wPicSize = 2;
|
||||
// 一般质量
|
||||
jpegPara.wPicQuality = 0;
|
||||
//命名规则:任务号+载具号+时间.jpg
|
||||
boolean result = Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_CaptureJPEGPicture(userId, channel, jpegPara, sFileUrl);
|
||||
if (!result) {
|
||||
result = Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_CaptureJPEGPicture(userId, channel, jpegPara, sFileUrl);
|
||||
if (result) {
|
||||
System.out.println("[SDK] 抓图成功,保存为 snapshot_sdk.jpg");
|
||||
} else {
|
||||
System.err.println("[SDK] 抓图失败");
|
||||
}
|
||||
}
|
||||
Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_Logout(userId);
|
||||
Hikvision截图Demo.HCNetSDK.INSTANCE.NET_DVR_Cleanup();
|
||||
return result?SdkResponse.requestOk():SdkResponse.requestFail("[SDK] 最终抓图失败");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.nl.common.hikvision;
|
||||
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Structure;
|
||||
|
||||
/**
|
||||
* @Description 海康摄像机抓图
|
||||
* @Author Gengby
|
||||
* @Date 2025/8/18
|
||||
*/
|
||||
public class Hikvision截图Demo {
|
||||
|
||||
// HCNetSDK 接口映射
|
||||
public interface HCNetSDK extends Library {
|
||||
HCNetSDK INSTANCE = Native.load("C:\\Users\\Administrator\\Desktop\\库文件\\HCNetSDK.dll", HCNetSDK.class);
|
||||
|
||||
//初始化 SDK 库
|
||||
boolean NET_DVR_Init();
|
||||
|
||||
//登录设备(传入设备 IP、端口、用户名、密码),返回一个用户 ID(后续调用都要用)
|
||||
int NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
|
||||
|
||||
//抓拍一张 JPEG 图片保存到本地。
|
||||
boolean NET_DVR_CaptureJPEGPicture(int lUserID, int lChannel, NET_DVR_JPEGPARA lpJpegPara, String sPicFileName);
|
||||
|
||||
//注销登录。
|
||||
boolean NET_DVR_Logout(int lUserID);
|
||||
|
||||
////清理 SDK 库
|
||||
boolean NET_DVR_Cleanup();
|
||||
}
|
||||
|
||||
@Structure.FieldOrder({"sSerialNumber", "byAlarmInPortNum", "byAlarmOutPortNum", "byDiskNum",
|
||||
"byDVRType", "byChanNum", "byStartChan", "byAudioChanNum", "byIPChanNum"})
|
||||
public static class NET_DVR_DEVICEINFO_V30 extends Structure {
|
||||
//序列号
|
||||
public byte[] sSerialNumber = new byte[48];
|
||||
//报警输入口个数
|
||||
public byte byAlarmInPortNum;
|
||||
//报警输出口个数
|
||||
public byte byAlarmOutPortNum;
|
||||
//硬盘个数
|
||||
public byte byDiskNum;
|
||||
//DVR 类型
|
||||
public byte byDVRType;
|
||||
//模拟通道数
|
||||
public byte byChanNum;
|
||||
//起始通道号
|
||||
public byte byStartChan;
|
||||
//音频通道数
|
||||
public byte byAudioChanNum;
|
||||
//IP 通道数
|
||||
public byte byIPChanNum;
|
||||
}
|
||||
|
||||
@Structure.FieldOrder({"wPicSize", "wPicQuality"})
|
||||
public static class NET_DVR_JPEGPARA extends Structure {
|
||||
// 0: QCIF, 1: CIF, 2: D1, 3: UXGA...
|
||||
public short wPicSize;
|
||||
// 0: 最差, 1: 较差, 2: 一般, 3: 较好, 4:最好
|
||||
public short wPicQuality;
|
||||
}
|
||||
|
||||
//HCNetSDK 抓图
|
||||
public static boolean snapshotSdk(String ip, short port, String username, String password, int channel) {
|
||||
if (!HCNetSDK.INSTANCE.NET_DVR_Init()) {
|
||||
System.err.println("[SDK] 初始化失败");
|
||||
return false;
|
||||
}
|
||||
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
|
||||
int userId = HCNetSDK.INSTANCE.NET_DVR_Login_V30(ip, port, username, password, deviceInfo);
|
||||
if (userId < 0) {
|
||||
System.err.println("[SDK] 登录失败");
|
||||
HCNetSDK.INSTANCE.NET_DVR_Cleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
NET_DVR_JPEGPARA jpegPara = new NET_DVR_JPEGPARA();
|
||||
// D1
|
||||
jpegPara.wPicSize = 2;
|
||||
// 一般质量
|
||||
jpegPara.wPicQuality = 0;
|
||||
//命名规则:任务号+载具号+时间.jpg
|
||||
boolean result = HCNetSDK.INSTANCE.NET_DVR_CaptureJPEGPicture(userId, channel, jpegPara, "E:\\lms\\2025\\snapshot_sdk2.jpg");
|
||||
if (result) {
|
||||
System.out.println("[SDK] 抓图成功,保存为 snapshot_sdk.jpg");
|
||||
} else {
|
||||
System.err.println("[SDK] 抓图失败");
|
||||
}
|
||||
boolean result2 = HCNetSDK.INSTANCE.NET_DVR_CaptureJPEGPicture(userId, channel, jpegPara, "E:\\lms\\snapshot_sdk3.jpg");
|
||||
if (result2) {
|
||||
System.out.println("[SDK] 抓图成功,保存为 snapshot_sdk.jpg");
|
||||
} else {
|
||||
System.err.println("[SDK] 抓图失败");
|
||||
}
|
||||
|
||||
HCNetSDK.INSTANCE.NET_DVR_Logout(userId);
|
||||
HCNetSDK.INSTANCE.NET_DVR_Cleanup();
|
||||
return result;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
// IP
|
||||
String ip = "169.254.43.101";
|
||||
// 端口号
|
||||
int port = 8000;
|
||||
// 账号
|
||||
String username = "admin";
|
||||
// 密码
|
||||
String password = "nl1314520";
|
||||
// HTTP 通道号
|
||||
String channelHttp = "101";
|
||||
// SDK 通道号(一般是 1)
|
||||
int channelSdk = 1;
|
||||
|
||||
Hikvision截图Demo.snapshotSdk(ip, (short) port, username, password, channelSdk);
|
||||
SdkResponse response = HikvisionSnapshotUtil.截图("C://12312/dd", "照片名字");
|
||||
if (!response.getSuc()){
|
||||
System.out.println(response.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.nl.common.hikvision;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 手持 返回结果
|
||||
* </p>
|
||||
*
|
||||
* @author Liuxy
|
||||
* @since 2025-06-05
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class SdkResponse<T> {
|
||||
|
||||
/**
|
||||
* 信息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 返回数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
private Boolean suc;
|
||||
|
||||
/**
|
||||
* 不带数据反馈
|
||||
* @return ErpResponse
|
||||
*/
|
||||
public static SdkResponse requestOk() {
|
||||
return SdkResponse.builder()
|
||||
.message("操作成功!")
|
||||
.suc(true)
|
||||
.build();
|
||||
}
|
||||
public static SdkResponse requestFail(String msg) {
|
||||
return SdkResponse.builder()
|
||||
.message(msg)
|
||||
.suc(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 带数据反馈
|
||||
* @return ErpResponse
|
||||
*/
|
||||
public static <T> SdkResponse requestParamOk(T data) {
|
||||
return SdkResponse.builder()
|
||||
.message("操作成功!")
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
## 1.环境准备
|
||||
1.windows电脑安装hikvision设备网络搜索,会搜索到相关的摄像头,在网络环境下配置ip
|
||||
2.确保通过设备地址能访问到摄像头,账号admin密码nl1314520
|
||||
3.解压hikvision.zip将里面库文件接到到电脑中:用于sdk访问时通过dll文件访问摄像头
|
||||
## 2.测试
|
||||
1.输入配置摄像头配置ip及账号密码,调用Main测试代码确认能否获取到照片
|
||||
2.如果照片指定的目录不存在,照片捕捉会失败
|
||||
## 3.项目
|
||||
yml配置
|
||||
### hikvision:
|
||||
#### ins: C:\Users\Administrator\Desktop\库文件\HCNetSDK.dll 解压的dll地址
|
||||
#### path: E:\lms\2025\ 生成的文件保存在哪个文件夹
|
||||
#### ip: 169.254.43.101 设备网络搜索对应的ip
|
||||
#### port: 8000 设备网络搜索对应端口
|
||||
#### username: admin 设备网络搜索账号
|
||||
#### password: nl1314520 设备网络搜索密码
|
||||
#### channelHttp: 101 默认
|
||||
#### channelSdk: 1 默认
|
||||
@@ -304,3 +304,12 @@ jetcache:
|
||||
maxTotal: 50
|
||||
host: ${nl.config.redis.ip}
|
||||
port: ${nl.config.redis.port}
|
||||
hikvison:
|
||||
ins: C:\Users\Administrator\Desktop\库文件\HCNetSDK.dll
|
||||
path: E:\lms\2025\
|
||||
ip: 169.254.43.101
|
||||
port: 8000
|
||||
username: admin
|
||||
password: nl1314520
|
||||
channelHttp: 101
|
||||
channelSdk: 1
|
||||
|
||||
Reference in New Issue
Block a user