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

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<String, String> 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<String, String> data = (Map<String, String>) 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<String, Object> getPlayAdress(String appKey, String appSecret, String deviceSerial,String channelNo) throws IOException {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    Map<String, Object> 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<String, String> data = (Map<String, String>) 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<String, String> params) throws IOException {
        return  httpRestClient(url, HttpMethod.POST, params);
    }

    /**
     * get
     * @param url 访问路径
     * @param params 参数
     * @return 返回响应体
     * @throws IOException
     */
    public static String get(String url, MultiValueMap<String, String> params) throws IOException {
        return  httpRestClient(url, HttpMethod.GET, params);
    }

    /**
     * HttpMethod  post/get
     * */
    private static String httpRestClient(String url, HttpMethod method, MultiValueMap<String, String> 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<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        //  执行HTTP请求
        ResponseEntity<String> 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 "";
        }
    }
}
  • 41
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值