springboot 实现接入海康威视摄像头预览画面到前端展示
时间: 2023-08-05 12:52:01 浏览: 229
要实现接入海康威视摄像头预览画面到前端展示,可以按照以下步骤进行:
1. 在 Spring Boot 项目中引入海康威视 Java SDK 的依赖,例如:
```
<dependency>
<groupId>com.hikvision.ivms</groupId>
<artifactId>ivms-8700-sdk</artifactId>
<version>1.0.0</version>
</dependency>
```
2. 在 Spring Boot 项目中编写代码,通过海康威视 SDK 获取摄像头的预览流,并将其转换为 Base64 编码的字符串,例如:
```
// 获取海康威视 SDK 的实例
HCNetSDK hcNetSDK = HCNetSDK.INSTANCE;
// 登录摄像头
NativeLong userId = new NativeLong(-1);
HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
userId = hcNetSDK.NET_DVR_Login_V30("10.0.0.1", (short) 8000, "admin", "password", deviceInfo);
// 获取预览流
HCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
previewInfo.lChannel = new NativeLong(1);
previewInfo.dwStreamType = 1;
previewInfo.dwLinkMode = 0;
NativeLong lRealPlayHandle = hcNetSDK.NET_DVR_RealPlay_V40(userId, previewInfo, null);
// 获取预览流数据
HCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new HCNetSDK.NET_DVR_PREVIEWINFO();
previewInfo.lChannel = new NativeLong(1);
previewInfo.dwStreamType = 1;
previewInfo.dwLinkMode = 0;
NativeLong lRealPlayHandle = hcNetSDK.NET_DVR_RealPlay_V40(userId, previewInfo, null);
// 将预览流数据转换为 BufferedImage 对象
HCNetSDK.NET_DVR_GetRealPlayerIndex(lRealPlayHandle);
HCNetSDK.NET_DVR_CLIENTINFO clientInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
clientInfo.lChannel = new NativeLong(1);
clientInfo.hPlayWnd = null;
byte[] bJpegPicBuffer = new byte[1024 * 1024];
IntByReference dwPicSize = new IntByReference();
if (hcNetSDK.NET_DVR_GetRealPlayerIndex(lRealPlayHandle) >= 0
&& hcNetSDK.NET_DVR_ClientGetVideoEffect(lRealPlayHandle, HCNetSDK.VIDEOEFFECT_GET, clientInfo)) {
if (hcNetSDK.NET_DVR_GetPlayerBackShare(lRealPlayHandle, bJpegPicBuffer, 1024 * 1024, dwPicSize)) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bJpegPicBuffer, 0, dwPicSize.getValue());
BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
// 将 BufferedImage 对象转换为 Base64 编码的字符串
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
String base64String = Base64.getEncoder().encodeToString(bytes);
}
}
// 停止预览并注销登录
hcNetSDK.NET_DVR_StopRealPlay(lRealPlayHandle);
hcNetSDK.NET_DVR_Logout_V30(userId);
```
3. 将 Base64 编码的字符串返回到前端,并在前端页面中使用 JavaScript 将其解码并显示为图片,例如:
```
<img src="data:image/jpeg;base64,${base64String}" />
```
这样就可以将海康威视摄像头的预览画面展示到前端页面中了。