1.前置准备工作
1.1下载h5播放器
https://open.hikvision.com/download/5c67f1e2f05948198c909700?type=10
安装包解压
1.2配置nginx反向代理
将解压的h5放到nginx目录下
配置nginx配置文件,修改如下两个配置
server {listen 9051;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root F:/nginx/nginx-1.22.1/h5play/demo;index /demo.html /index.htm;}
浏览器访问,可以出来页面说明显示正常
http://localhost:9051/demo.html
1.3获取安防平台的ip,两个密钥,需要找对接相关人员,并同时要求开放你的ip可以访问安防平台
官方文档,可以参考,用处不大
https://open.hikvision.com/docs/docId?productId=5c67f1e2f05948198c909700&version=%2F9e6b1870e25348608d01b5669a7f3595&tagPath=%E6%96%87%E6%A1%A3%E8%AF%B4%E6%98%8E
2.java后台实现
2.1获取实时监控url
package com.example.demo.haikang;import com.example.demo.util.ArtemisUtil;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.json.JSONException;
import org.json.JSONObject;import java.util.HashMap;
import java.util.List;
import java.util.Map;public class GetCameraPreviewURL {public static String GetCameraPreviewURL(){/*** STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.*/ArtemisConfig.host = "ip:端口,一般是433"; // 平台的ip端口ArtemisConfig.appKey = "密钥1"; // 密钥appkeyArtemisConfig.appSecret = "密钥2";// 密钥appSecret/*** STEP2:设置OpenAPI接口的上下文*/final String ARTEMIS_PATH = "/artemis";/*** 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();try {//监控点唯一标识jsonBody.put("cameraIndexCode", "587f55d033dcdd73306");//码流类型,0:主码流,1:子码流,2:第三码流,参数不填,默认为主码流jsonBody.put("streamType", 0);//取流协议,“rtsp”:RTSP协议,“rtmp”:RTMP协议,“hls”:HLS协议jsonBody.put("protocol", "ws");//传输协议,0:UDP,1:TCP,默认是TCP "playType":"back-",jsonBody.put("transmode", 1);//扩展内容,格式:key=valuejsonBody.put("expand", "streamform=ps");} catch (JSONException e) {e.printStackTrace();}String body = jsonBody.toString();/*** STEP6:调用接口*/String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数Map map = com.alibaba.fastjson.JSONObject.parseObject(result, Map.class);String data = map.get("data").toString();Map mapurl = com.alibaba.fastjson.JSONObject.parseObject(data, Map.class);Object url = mapurl.get("url");System.out.println("url路径:"+url);return result;}public static void main(String[] args) {//String result = GetCameraPreviewURL(); 2022-12-12T00:00:00.000+08:00//String result = getCameraPlayBackURL("533dcdd73306","2022-12-11T00:00:00.000+08:00","2022-12-13T00:00:00.000+08:00");String result = ArtemisUtil.getCameraPlayBackURL("71db88fc77179", "2022-12-11T00:00:00.000+08:00", "2022-12-13T00:00:00.000+08:00");System.out.println("***********************************************************************8");System.out.println("result结果示例: " + result);}
}
复制后面的url路径:ws到页面中,选择高级模式
注意:协议部分根据自己的情况选择,本人不清楚这个什么意思,切换后无法看到监控
2.2获取回放视频url
package com.example.demo.util;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.json.JSONException;
import org.json.JSONObject;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class ArtemisUtil {static {// 代理API网关nginx服务器ip端口ArtemisConfig.host = ""ip:端口,一般是433"";// 秘钥appkeyArtemisConfig.appKey = "密钥1";// 秘钥appSecretArtemisConfig.appSecret = "密钥2";}/*** 获取监控点回放取流url*/public static String getCameraPlayBackURL(String cameraIndexCode, String beginTime, String endTime) {String getRootApi = ARTEMIS_PATH + "/api/video/v2/cameras/playbackURLs";Map<String, String> path = new HashMap<String, String>(2) {{put("https://", getRootApi);}};String contentType = "application/json";JSONObject jsonBody = null;try {jsonBody = new JSONObject();jsonBody.put("cameraIndexCode", cameraIndexCode);jsonBody.put("recordLocation", 0);//存储类型,0:中心存储,1:设备存储,默认为中心存储jsonBody.put("protocol", "ws");jsonBody.put("transmode", 1);jsonBody.put("beginTime", beginTime);//开始查询时间jsonBody.put("endTime", endTime);//结束查询时间,IOS8601格式:yyyy-MM-dd’T’HH:mm:ss.SSSXXXjsonBody.put("expand", "streamform=ps");} catch (JSONException e) {e.printStackTrace();}String body = jsonBody.toString();String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);return result;}/*** 时间格式转换* 将时间抓换成IOS8601格式* @param data* @param formatType* @return*/public static String dateToString(Date data, String formatType){return new SimpleDateFormat(formatType).format(data);}
}
ws:开头为回放路径:去掉后面的日期,复制到页面
注意:需要根据自己的情况选择是0还是1