Java Onvif实现预置位跳转和添加

发布于:2023-01-11 ⋅ 阅读:(1522) ⋅ 点赞:(1)

Java Onvif实现预置位跳转和添加

1、基本参数

  • 预置位
    摄像机预置位是已存储的摄像机的水平、垂直、缩放位置。 摄像机预置位是已存储的摄像机位置。 预置摄像机位置并存储该预置位后,可以通过切换不同预置位,方便地调整摄像机位置。

  • Onvif支持预置位操作
    Onvif定义了摄像机预置位的列表获取、新增预置位、修改预置位的名称和位置、删除预置位。

  • 参考资料
    Onvif网络接口规范文档地址:https://www.onvif.org/ver20/ptz/wsdl/ptz.wsdl

2、 获取预置位置列表

接口定义

列表

参数:ProfileToken;
返回:预置位名称;位置;token

代码实现

		//初始化Onvif
        OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
        List<Profile> profiles = onvifDevice.getDevices().getProfiles();
        //从配置文件列表(profiles)中查找出第一个ptz配置不为空的配置文件
        Optional<Profile> first = profiles.stream().filter(p -> p.getPTZConfiguration() != null).findFirst();
        if (first.isPresent()) {
            Profile profile = first.get();
            String profileToken = profile.getToken();
            //获取预置位列表
            List<PTZPreset> presets = onvifDevice.getPtz().getPresets(profileToken);
            }

3、添加 / 修改预置位

接口定义

设置
参数

  1. ProfileToken;
  2. 预置位名称;
  3. 预置位token(非必传、当token为null或不重复时则是新增预置位,否则是修改token对应的预置位)

返回:预置位token(传入参数token为null时,摄像头会默认生成一个不重复的token返回)
预置位位置:无论是修改还是新增操作,都会使用摄像头当前的位置作为此预置位的位置

代码实现

   public static String setPreset(CameraVO cameraVO, String token, String name) {
        try {
           //初始化Onvif
            OnvifDevice onvifDevice = new OnvifDevice(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassword());
            List<Profile> profiles = onvifDevice.getDevices().getProfiles();
            //从配置文件列表(profiles)中查找出第一个ptz配置不为空的配置文件
            Optional<Profile> first = profiles.stream().filter(p -> p.getPTZConfiguration() != null).findFirst();
            if (first.isPresent()) {
                Profile profile = first.get();
                String profileToken = profile.getToken();
                //设置预置位
                return onvifDevice.getPtz().setPreset(name, token, profileToken);
            }
        } catch (Exception e) {
            log.error("setPreset Exception", e);
        }
        return null;
    }

4、预置位跳转

接口定义

跳转

参数

  1. ProfileToken;
  2. 预置位token;
  3. 移动速度

返回:是否成功

代码实现

    public static Boolean gotoPreset(CameraVO cameraVO, String token) {
        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.getPTZConfiguration() != null).findFirst();
            if (first.isPresent()) {
                Profile profile = first.get();
                String profileToken = profile.getToken();
                return onvifDevice.getPtz().gotoPreset(token, profileToken);
            }
        } catch (Exception e) {
            log.error("gotoPreset Exception", e);
        }
        return false;
    }

5、删除预置位

接口定义

删除

参数

  1. ProfileToken;
  2. 预置位token;

返回:是否成功

代码实现

    public static boolean removePreset(CameraVO cameraVO, String token) {
        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.getPTZConfiguration() != null).findFirst();
            if (first.isPresent()) {
                Profile profile = first.get();
                String profileToken = profile.getToken();
                return onvifDevice.getPtz().removePreset(token, profileToken);
            }
        } catch (Exception e) {
            log.error("removePreset Exception", e);
        }
        return false;
    }
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到