用Java Onvif 实现OSD获取和添加
1、基本参数
- 坐标系
ONVIF标准文档中OSD坐标系是以图像中心点为原始坐标(0,0),坐标范围为-1~1
参考资料
Onvif文档地址:http://www.onvif.org/ver10/media/wsdl/media.wsdl常见参数
-Type 类型:‘文字’、‘图片’、'扩展
-DateFormat 日期格式:M/d/yyyy 、MM/dd/yyyy 、dd/MM/yyyy 、yyyy/MM/dd 等
-TimeFormat 时间格式:h:mm:ss tt、 hh:mm:ss tt、 H:mm:ss 、 HH:mm:ss
-PositionOption 位置:UpperLeft 、UpperRight 、LowerLeft 、LowerRight 、Custom
-FontSize 字体大小:16、32、48、64
更多参数详见Onvif文档
2、获取configurationToken属性的值
OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
List<Profile> profiles = onvifDevice.getDevices().getProfiles();
//取第一个
Optional<Profile> first = profiles.stream().filter(p -> p.getVideoSourceConfiguration() != null).findFirst();
if (first.isPresent()) {
Profile profile = first.get();
//configurationToken属性
String sourceToken = profile.getVideoSourceConfiguration().getToken();
}
3、获取OSD列表
接口定义
代码实现
try {
OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
List<Profile> profiles = onvifDevice.getDevices().getProfiles();
Optional<Profile> first = profiles.stream().filter(p -> p.getVideoSourceConfiguration() != null).findFirst();
if (first.isPresent()) {
Profile profile = first.get();
String sourceToken = profile.getVideoSourceConfiguration().getToken();
//列表查询
List<OSDConfiguration> osdConfigurations = onvifDevice.getOsdDevices().getOSDs(sourceToken);
}
} catch (Exception e) {
log.error("getOSDs Exception", e);
}
4、添加OSD
接口定义
代码实现
public static String createOSD(CameraVO cameraVO, OSDConfigurationRequest request) {
try {
OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
List<Profile> profiles = onvifDevice.getDevices().getProfiles();
Optional<Profile> first = profiles.stream().filter(p -> p.getVideoSourceConfiguration() != null).findFirst();
if (first.isPresent()) {
Profile profile = first.get();
String sourceToken = profile.getVideoSourceConfiguration().getToken();
OSDConfiguration createOSD = newOSDConfiguration(request, sourceToken);
return onvifDevice.getOsdDevices().createOSD(createOSD);
}
} catch (Exception e) {
log.error("createOSD Exception", e);
}
return null;
}
private static OSDConfiguration newOSDConfiguration(OSDConfigurationRequest request, String sourceToken) {
OSDConfiguration osdConfiguration = new OSDConfiguration();
OSDType osdType = OSDType.fromValue("Text");
osdConfiguration.setType(osdType);
OSDReference videoToken = new OSDReference();
videoToken.setValue(sourceToken);
osdConfiguration.setVideoSourceConfigurationToken(videoToken);
OSDPosConfiguration position = new OSDPosConfiguration();
position.setType("Custom");
Vector vector = new Vector();
vector.setY(request.getY());
vector.setX(request.getX());
position.setPos(vector);
osdConfiguration.setPosition(position);
OSDTextConfiguration textConfig = new OSDTextConfiguration();
textConfig.setFontSize(request.getFontSize());
textConfig.setPlainText(request.getPlainText());
textConfig.setType(request.getType());
textConfig.setDateFormat(request.getDateFormat());
textConfig.setTimeFormat(request.getTimeFormat());
osdConfiguration.setTextString(textConfig);
return osdConfiguration;
}
1.TextConfig.Type 参数
- Plain -文本类型意味着OSD显示为文本字符串定义的“plainText”项目
- Date - 日期类型意味着OSD显示为日期格式,应该是出现在“DateFormat”项目。
- Time - 时间类型意味着OSD显示时间,格式应该出现在“TimeFormat”的项目。
- DateAndTime - 时间和日期类型意味着OSD显示为日期和时间,格式应该出现在“DateFormat”和“TimeFormat”项目。
- Plain类型一般可以设置多个,但Date 、Time 、DateAndTime 一般只能设置一个
2.TextConfig.TextString 参数
- 参数长度一般小于44
5、修改OSD
接口定义
代码实现
public static boolean setOSD(CameraVO cameraVO, OSDConfigurationRequest request) {
try {
OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
List<Profile> profiles = onvifDevice.getDevices().getProfiles();
Optional<Profile> first = profiles.stream().filter(p -> p.getVideoSourceConfiguration() != null).findFirst();
if (first.isPresent()) {
Profile profile = first.get();
String sourceToken = profile.getVideoSourceConfiguration().getToken();
OSDConfiguration createOSD = newOSDConfiguration(request, sourceToken);
createOSD.setToken(request.getOsdToken());
return onvifDevice.getOsdDevices().setOSD(createOSD);
}
} catch (Exception e) {
log.error("setOSD Exception", e);
}
return false;
}
- 修改OSD需要带上osdToken参数
6、删除OSD
接口定义
代码实现
public static boolean deleteOSD(CameraVO cameraVO, String osdToken) {
try {
OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
List<Profile> profiles = onvifDevice.getDevices().getProfiles();
Optional<Profile> first = profiles.stream().filter(p -> p.getVideoSourceConfiguration() != null).findFirst();
if (first.isPresent()) {
return onvifDevice.getOsdDevices().deleteOSD(osdToken);
}
} catch (Exception e) {
log.error("deleteOSD Exception", e);
}
return false;
}
- 删除OSD只需要带上osdToken参数即可