海康威视 iSecure Center 平台预览监控

1、需求:基于海康威视的ISC平台做二次开发,以视频预览为例进行简单阐述。

2、版本号:ISC V1.3.0

3、参考并感谢海康威视平台文档:https://open.hikvision.com/docs/5dc5969b3ad3fd808b9f7238bf1fa124

4、请仔细阅读海康提供的公开文档,通过平台的公开API进行访问,屏蔽复杂的逻辑和推流操作。

具体的代码如下所示:

package ga;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import java.util.*;

public class GetCameraPreviewURL {

    static {
        ArtemisConfig.host = "ip,端口不用写";// 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey = "秘钥appkey";// 秘钥appkey
        ArtemisConfig.appSecret = "秘钥appSecret";// 秘钥appSecret
    }

    private static final String ARTEMIS_PATH = "/artemis";

    public static String GetCameraPreviewURL(String cameraIndexCode) {

        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "rtmp");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }

    /**
     * 视频列表
     *
     * @return
     */
    private static String getCameraList() {
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 10);
        jsonBody.put("pageSize", 10);

        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }

    /**
     * 视频在线列表
     *
     * @return
     */
    private static String getCameraListOnline(List<String> strArray) {

        /**
         * STEP3:设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/nms/v1/online/camera/get";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4:设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("regionId", "root000000");
        jsonBody.put("includeSubNode", "1");
        jsonBody.put("indexCodes", strArray);
        jsonBody.put("status", 1);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);

        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }

    /**
     * 获取根区域信息
     *
     * @return
     */
    private static String getRegionsRoot() {
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v1/regions/root";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);

        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }


    /**
     * 获取根区域信息
     *
     * @return
     */
    private static String getSubRegions() {
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v2/regions/subRegions";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("parentIndexCode", "-1");
        jsonBody.put("resourceType", "camera");
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);
        jsonBody.put("cascadeFlag", 0);

        String body = jsonBody.toJSONString();
        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }

    public static void main(String[] args) {
        String result = getCameraList();
        JSONObject object = JSONObject.parseObject(result);
        JSONObject data = (JSONObject) object.get("data");
        JSONArray dataList = (JSONArray) data.get("list");
        List<String> cameraCodeList = new ArrayList<>();
        // 获取视频列表
        for (Object obj : dataList) {
//            System.out.println(obj);
            cameraCodeList.add(((JSONObject) obj).getString("cameraIndexCode"));
        }

        // 判断是否在线
        JSONObject onlineObj = JSONObject.parseObject(getCameraListOnline(cameraCodeList));
        JSONObject jsonObject = (JSONObject) onlineObj.get("data");
        cameraCodeList.clear();
        for (Object onlineCamera : (JSONArray) jsonObject.get("list")) {
            // 区域编号
            String regionIndexCode = ((JSONObject) onlineCamera).getString("regionIndexCode");
            // 区域
            String regionName = ((JSONObject) onlineCamera).getString("regionName");
            // 摄像头编码
            String indexCode = ((JSONObject) onlineCamera).getString("indexCode");
            // 在线
            String online = "1".equals(((JSONObject) onlineCamera).getString("online")) ? "在线" : "不在线";
            // cn
            String cn = ((JSONObject) onlineCamera).getString("cn");
            System.out.println("区域编码:" + regionIndexCode + "\t区域名:" + regionName + "\t摄像头编码:" + indexCode + "\t在线:" + online + "\tcn:" + cn);
            if ("在线".equals(online)) {
                cameraCodeList.add(indexCode);
            }
        }

        for (String cameraCode : cameraCodeList) {
            String url = GetCameraPreviewURL(cameraCode);
        }
    }
}

5、其中秘钥appkey和秘钥appSecret,联系管理员做如下准备:
开发前需要先获取对接的身份认证信息,AK/SK;平台会通过AK/SK认证方式来验证请求发送者的身份。
1.进入运管中心(http://ip:8001/center ),进入【状态监控】模块,选择【API网关】,选择【API管理】功能,进入API管理中心。
2.选择【合作方管理】,平台安装后,默认会创建一个对内合作方,可以使用此合作方进行接口调用测试,实际运行环境需要根据实际的情况创建新的合作方。
3.点击合作方的名称,进入合作方详情界面,获取合作方key(AK)和合作方Secret(SK)。

 

6、程序跑通之后,我们会获取到视频的rtmp协议(我设置的协议是rtmp),日志如下所示:

com.hikvision.artemis.sdk.ArtemisHttpUtil - the Artemis Request is Success,statusCode:200 SuccessMsg:{"code":"0","msg":"success","data":{"url":"rtmp://192.168.20.130:1935/live/openUrl/7SIswj6"}}

7、使用rtmp://192.168.20.130:1935/live/openUrl/7SIswj6协议在vlc中能够正常播放;

8、我们嵌入到vue中或者使用如下代码进行测试,总是无法播放,这个问题困扰了我半个月,一直在找解决办法。直到5月26日得到海康威视二次开发群(150425667)的群友的支持,才得以解决,在此再次感谢这位群友。

     rtmp://192.168.20.130:1935/live/openUrl/7SIswj6 后面 加上斜杠/ 即可正常播放

<!DOCTYPE html>
<html lang="en">
<head>

    <title>Video.js | HTML5 Video Player</title>
    <!-- <link href="video-js-6.2.0/video-js.css" rel="stylesheet">
    <script src="video-js-6.2.0/videojs-ie8.min.js"></script> -->

    <link href="http://vjs.zencdn.net/5.20.1/video-js.css" rel="stylesheet">
    <script src="http://vjs.zencdn.net/5.20.1/videojs-ie8.min.js"></script>

</head>
<body>

<!--<video id="example_video_1" class="video-js vjs-default-skin" controls preload="auto" width="1280" height="720" poster="http://vjs.zencdn.net/v/oceans.png" data-setup="{}">-->
<!--    &lt;!&ndash; 湖南卫视 &ndash;&gt;-->
<!--    <source src="rtmp://58.200.131.2:1935/livetv/hunantv" type="rtmp/flv">-->

<!--    <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>-->
<!--</video>-->

<video id="example_video_1" class="video-js vjs-default-skin" controls preload="auto" width="1280" height="720" poster="http://vjs.zencdn.net/v/oceans.png" data-setup="{}">
    <!-- 湖南卫视 -->
    <source src="rtmp://192.168.20.130:1935/live/openUrl/6uhHAac/" type="rtmp/flv">

    <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video>

<script src="http://vjs.zencdn.net/5.20.1/video.js"></script>
</body>

</html>

 

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值