调用海康API预览视频

1. API

java 复制代码
    String rootRegionApi = "/api/resource/v1/regions/root";
    String subRegionApi = "/api/resource/v2/regions/subRegions";
    String cameraByRegionApi = "/api/resource/v1/regions/regionIndexCode/cameras";
    String preUrlByCameraApi = "/api/video/v2/cameras/previewURLs";

1. 获取 region 根节点

java 复制代码
	public IscRegionDO getTopTreeList() {
        String body = getRootJsonBody();
        String listResult = getHKDataByAPI(rootRegionApi, body);
        if (StringUtils.isNotEmpty(listResult)) {
            IscRegionDO obj = JSON.parseObject(listResult, IscRegionDO.class);
            return obj;
        }
        return null;
    }
       private static String getRootJsonBody() {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("treeCode", "0");
        String body = jsonBody.toJSONString();
        return body;
    } 
        private String getHKDataByAPI(String api, String body) {
        JSONObject jsonObject = queryHK(api, body);
        return jsonObject.getString("data");
    }

海康util 方法

java 复制代码
      private static JSONObject queryHK(String api, String body) {
        ArtemisConfig.host = Constant.HK_HOST;//服务器ip端口
        ArtemisConfig.appKey = Constant.HK_KEY;// 秘钥appkey
        ArtemisConfig.appSecret = Constant.HK_SECRET;// 秘钥appSecret
        HashMap<String, String> path = new HashMap<String, String>(8);
        path.put("https://", "/artemis" + api);

        String url = "https://" + Constant.HK_HOST + "/artemis" + api;
        log.info("IscRegionServiceImpl: appKey->{},appSecret->{},host->{},url->{},body->{}", ArtemisConfig.appKey, ArtemisConfig.appSecret, ArtemisConfig.host, url, body);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null,
                null, "application/json", null);

        System.out.println(api + " ====== 请求返回结果:【{" + result + "}】");
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

海康maven依赖包

xml 复制代码
        <dependency>
            <groupId>com.hikvision.ga</groupId>
            <artifactId>artemis-http-client</artifactId>
            <version>1.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. 获取 region 中间节点

java 复制代码
    @Override
    public List<IscRegionDO> getSubTreeList(String indexCode) {
        String body = getSubRegionJsonBody(indexCode);
        String listResult = getHKListByAPI(subRegionApi, body);
        log.info("getSubTreeList listResult {}", listResult);

        if (StringUtils.isEmpty(listResult) || "[]".equals(listResult)) {
            List<IscCameraDO> camerasList = getCamerasByRegionIndexCode(indexCode);
            log.info("getSubTreeList camerasList {}", camerasList);
            List<IscRegionDO> resultList = transferyy(camerasList, indexCode);
            return resultList;
        } else {
            List<IscRegionDO> resultList = JSON.parseArray(listResult, IscRegionDO.class);
            resultList.forEach(o -> o.setLeaf(false));
            return resultList;
        }
    }
        private static String getSubRegionJsonBody(String parentIndexCode) {
        JSONObject jsonBody = new JSONObject();
        int pageNo = 1, pageSize = 500;
        jsonBody.put("parentIndexCode", parentIndexCode);
        jsonBody.put("resourceType", "region");
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("cascadeFlag", "0");
        String body = jsonBody.toJSONString();
        return body;
    }
        private String getHKListByAPI(String api, String body) {
        JSONObject jsonObject = queryHK(api, body);
        String code = jsonObject.getString("code");// 操作成功
        JSONObject dataObject = jsonObject.getJSONObject("data");// 操作成功
        if (dataObject != null && "0".equals(code)) {// 查询成功
            String listResult = dataObject.getString("list");
            return listResult;
        }
        return null;
    }
        private List<IscRegionDO> transferyy(List<IscCameraDO> camerasList, String parentIndexCode) {
        List<IscRegionDO> resultList = new ArrayList<>();
        for (IscCameraDO iscCameraDO : camerasList) {
            IscRegionDO iscRegionDO = new IscRegionDO();
            iscRegionDO.setIndexCode(iscCameraDO.getCameraIndexCode()); // vv
            iscRegionDO.setParentIndexCode(parentIndexCode); // vv

            iscRegionDO.setName(iscCameraDO.getCameraName());//
            iscRegionDO.setStatus(iscCameraDO.getStatus());//
            iscRegionDO.setStatusName(iscCameraDO.getStatusName());//
            iscRegionDO.setLeaf(true); // vv
            resultList.add(iscRegionDO);
        }
        return resultList;
    }

3. 获取 region 叶子下的摄像头

java 复制代码
    @Override
    public List<IscCameraDO> getCamerasByRegionIndexCode(String regionIndexCode) {
        String body = getCameraJsonBody(regionIndexCode);
        String listResult = getHKListByAPI(cameraByRegionApi, body);
        if (StringUtils.isNotEmpty(listResult)) {
            List<IscCameraDO> resultList = JSON.parseArray(listResult, IscCameraDO.class);
            return resultList;
        }
        return Collections.emptyList();
    }

    private static String getCameraJsonBody(String parentIndexCode) {
        JSONObject jsonBody = new JSONObject();
        int pageNo = 1, pageSize = 500;
        jsonBody.put("regionIndexCode", parentIndexCode);
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        String body = jsonBody.toJSONString();
        return body;
    }

4. 获取摄像头的视频预览连接

java 复制代码
    @Override
    public String getPreUrlByCameraIndexCode(String cameraIndexCode, String protocol) {
        String body = getPreUrlBody(cameraIndexCode, protocol);
        String listResult = getHKDataByAPI(preUrlByCameraApi, body);
        if (StringUtils.isNotEmpty(listResult)) {
            JSONObject obj = JSON.parseObject(listResult, JSONObject.class);
            String originStr = obj.getString("url");
            log.info("origin url :{}",originStr);
            String replaced = originStr.replace(Constant.HK_ORIGIN_URL, Constant.HK_REPLACE_URL);
            log.info("replaced url :{}",replaced);
            return replaced;
        }
        return null;
    }

    private static String getPreUrlBody(String cameraIndexCode, String protocol) {
        JSONObject jsonBody = new JSONObject();
        int pageNo = 1, pageSize = 500;
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        if (StringUtils.isNotEmpty(protocol)) {
            jsonBody.put("protocol", protocol);
        } else {
            jsonBody.put("protocol", "hls");
        }
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        String body = jsonBody.toJSONString();
        return body;
    }

其他配置

java 复制代码
    public static final String HK_HOST = "10.10.0.0";
    public static final String HK_KEY = "HK_KEY";
    public static final String HK_SECRET = "HK_SECRET";

    public static final String HK_ORIGIN_URL = "http://HK_HOST:83/openUrl";
    public static final String HK_REPLACE_URL = "https://xxx.company.com:7443/proxy-video";
相关推荐
ytadpole12 分钟前
揭秘设计模式:工厂模式的五级进化之路
java·设计模式
计算机毕业设计木哥13 分钟前
计算机毕设选题:基于Python+Django的B站数据分析系统的设计与实现【源码+文档+调试】
java·开发语言·后端·python·spark·django·课程设计
失散1313 分钟前
分布式专题——1.2 Redis7核心数据结构
java·数据结构·redis·分布式·架构
用户37215742613538 分钟前
Python 实现 HTML 转 Word 和 PDF
java
a5876939 分钟前
Java核心概念精讲:TCP与UDP的区别、Java NIO的几个核心组件与HTTP和HTTPS的区别等(46-50)
java·面试·nio
渣哥1 小时前
ConcurrentHashMap 的 get 要不要加锁?一次“多此一举”的心路历程
java
愿你天黑有灯下雨有伞1 小时前
一种基于注解与AOP的Spring Boot接口限流防刷方案
java·spring boot·后端
MuMuMu#1 小时前
JAVA NIO学习笔记基础强化学习总结
java·学习·nio
拾忆,想起1 小时前
Redis复制延迟全解析:从毫秒到秒级的优化实战指南
java·开发语言·数据库·redis·后端·缓存·性能优化
我登哥MVP1 小时前
Java File 类学习笔记
java·笔记·学习