小工具知识网

当前位置: 首页 > 行业知识 > 对接海康,萤石摄像头步骤(java版)

行业知识

对接海康,萤石摄像头步骤(java版)

2026-10-27 09:24:34 来源:互联网转载

1:如果有手机端萤石视频账号的直接登陆萤石开放平台-提供持续稳定的以音视频为主的全场景、多功能综合性服务 萤石云开发平台进行认证,没有账号需要注册,可以进行公司或者个人认证,公司认证需要对应的营业执照,个人认证需要发送相关的信息到萤石云开发平台的邮箱;

()

2:认证通过后需要创建应用,应用创建后通过应用的appkey,appsecret访问应用下的摄像头,录像机等设备。如果新注册的账号需要根据设备编码以及验证码添加录像机或者摄像头。

3:对应的接口文档在文档概述 · 萤石开放平台API文档。

()

常用接口:

1:获取token的接口,https://open.ys7.com/api/lapp/token/get 

参数为:appKey,appSecret

2:获取萤石云播放地址,https://open.ys7.com/api/lapp/v2/live/address/get

参数为:accessToken,deviceSerial,channelNo

token,设备编码,通道号

具体实现方法:

1:获取token

public static String getToken(String appKey, String appSecret) throws IOException {    MultiValueMap params = new LinkedMultiValueMap();    String result = "";    params.set("appKey", appKey);    params.set("appSecret", appSecret);    try {        String s = HttpRestUtils.post(ys7Token, params);        JSONObject jsonObject = JSONObject.parseObject(s);        String code = (String) jsonObject.get("code");        String msg = (String) jsonObject.get("msg");        Map data = (Map) jsonObject.get("data");        String accessToken = data.get("accessToken");        if ("200".equals(code)) {            result = accessToken;        } else {            result = "-1";//获取token错误        }    } catch (Exception e) {        e.printStackTrace();    }    return result;}

2:获取播放地址

public static Map getPlayAdress(String appKey, String appSecret, String deviceSerial,String channelNo) throws IOException {    MultiValueMap params = new LinkedMultiValueMap();    Map resultMap = new HashMap();    String result = "";    String accessToken = getToken(appKey, appSecret);    resultMap.put("accessToken",accessToken);    if ("-1".equals(accessToken)) {        throw new RuntimeException("获取token失败");    }    params.set("accessToken", accessToken);    params.set("deviceSerial", deviceSerial);    params.set("channelNo", channelNo);    try {        String s = HttpRestUtils.post(ys7PlayAddress, params);        JSONObject jsonObject = JSONObject.parseObject(s);        String code = (String) jsonObject.get("code");        String msg = (String) jsonObject.get("msg");        Map data = (Map) jsonObject.get("data");        String url = data.get("url");        if ("200".equals(code)) {            result = url;        } else {            result = "-1";//获取token错误        }    } catch (Exception e) {        e.printStackTrace();    }    resultMap.put("url",result);    return resultMap;}

3:请求工具类源码

package com.gjzx.common.core.utils;import org.springframework.http.*;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.util.MultiValueMap;import org.springframework.web.client.HttpClientErrorException;import org.springframework.web.client.RestTemplate;import java.io.IOException;public class HttpRestUtils {    /**     * post     * @param url 访问路径     * @param params 参数     * @return 返回响应体     * @throws IOException     */    public static String post(String url, MultiValueMap params) throws IOException {        return  httpRestClient(url, HttpMethod.POST, params);    }    /**     * get     * @param url 访问路径     * @param params 参数     * @return 返回响应体     * @throws IOException     */    public static String get(String url, MultiValueMap params) throws IOException {        return  httpRestClient(url, HttpMethod.GET, params);    }    /**     * HttpMethod  post/get     * */    private static String httpRestClient(String url, HttpMethod method, MultiValueMap params) throws IOException {        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();        requestFactory.setConnectTimeout(10*1000); //设置连接超时时间        requestFactory.setReadTimeout(10*1000); //设置读取超时时间        RestTemplate client = new RestTemplate(requestFactory);        HttpHeaders headers = new HttpHeaders();//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);        /**         * public static final MediaType APPLICATION_ATOM_XML = new MediaType("application", "atom+xml");         * public static final MediaType APPLICATION_CBOR = new MediaType("application", "cbor");         * public static final MediaType APPLICATION_FORM_URLENCODED = new MediaType("application", "x-www-form-urlencoded");         * public static final MediaType APPLICATION_JSON = new MediaType("application", "json");         */        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //json格式,可以切换成其他数据传输格式        HttpEntity requestEntity = new HttpEntity(params, headers);        //  执行HTTP请求        ResponseEntity response = null;        try{            response = client.exchange(url, method, requestEntity, String.class);            return response.getBody();        }        catch (HttpClientErrorException e){            System.out.println("HttpClientErrorException" + e.getMessage());            System.out.println(e.getStatusText());            System.out.println(e.getResponseBodyAsString());            e.printStackTrace();            return "";        }        catch (Exception e) {            System.out.println("Exception" + e.getMessage());            e.printStackTrace();            return "";        }    }}

上一篇:揭秘NAT类型,网络地址转换的全面解析与特点探究

下一篇:石家庄金冠中医骨病医院麻烦问一下周末双休吗?