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

发布时间:2024年01月18日

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 "";
        }
    }
}
文章来源:https://blog.csdn.net/weixin_44603328/article/details/135603168
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。