从海康ISC获取视频预览地址java

该博客展示了如何使用海康威视的OpenAPI库进行摄像头预览URL获取及设备控制。首先,配置API网关的主机、appKey和appSecret。接着,定义API路径并组装请求参数,包括摄像头索引码、流类型等。最后,通过ArtemisHttpUtil调用接口获取预览URL或控制摄像头的PTZ操作。
摘要由CSDN通过智能技术生成

具体可参考海康官网开发文档说明:

OpenAPI安全认证库 (Java)开发指南V1.1.4_20211215182354_20220216173329.pdf

 

package com.xxx;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArtemisPostUtils {
    static int FINAL_TIME = 1;

    public static String getCameraPreviewURL(String host, String appKey, String appSecret, String cameraIndexCode, String addressType, int streamType) {

        /*ArtemisConfig.host ="1xx.x.x.xx:443"; // 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey ="xxxx";  // 秘钥appkey
        ArtemisConfig.appSecret ="xxxxx";// 秘钥appSecret*/

        ArtemisConfig.host = host; // 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey = appKey;  // 秘钥appkey
        ArtemisConfig.appSecret = appSecret;

        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

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

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

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        //jsonBody.put("cameraIndexCode", "a5427e639bd94c638e0a2df5168a43c4");
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", streamType); //0:主码流 1:子码流 2:第三码流  https://open.hikvision.com/docs/8530061f19534a9993e2afeb70e7c96a
        jsonBody.put("protocol", addressType);
        //jsonBody.put("transmode", 1);
        //jsonBody.put("expand", "streamform=gb28181");
        String body = jsonBody.toJSONString();

        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);

        /*
        返回url地址
         */
        if(null != result || !"".equals(result)){
            JSONObject json = JSONObject.parseObject(result);

            json = JSONObject.parseObject(json.getString("data"));

            String url = json.getString("url") ;
            return url;
        }else {
            return null;
        }
    }

    public static String controlCameraByCameraIndexCode(String host, String appKey, String appSecret, String cameraIndexCode,
                                                        int action, String command, int time) {

        /**
         * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = host; // 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey = appKey;  // 秘钥appkey
        ArtemisConfig.appSecret = appSecret;

        /**
         * STEP2:设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

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

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

        /**
         * STEP5:组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("action", action);
        jsonBody.put("command", command);
        //jsonBody.put("speed", speed);
        //jsonBody.put("presetIndex", presetIndex);
        String body = jsonBody.toJSONString();

        /**
         * STEP6:调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
        if (null != result || !"".equals(result)) {
            JSONObject json = JSONObject.parseObject(result);
            if (time != -1 && json.getString("msg").equals("success")) {
                Runnable myRunnable = new Runnable(){
                    @Override
                    public void run(){
                        try {
                            Thread.sleep(time*1000);
                            jsonBody.put("action", 1);
                            String body = jsonBody.toJSONString();
                            String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
                        } catch (InterruptedException e) {
                            System.out.println("Runnable is error");
                        }
                        System.out.println("Runnable running");
                    }
                };
                Thread thread = new Thread(myRunnable);
                thread.start();
                return "success";
            }
            return json.getString("msg");
        } else {
            return null;
        }
    }

/*   //请求萤石云的accessToken
    public static List getAccessToken(String appKey, String appSecret) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "appKey="+ appKey +"&appSecret="+appSecret);
        Request request = new Request.Builder()
                .url("https://open.ys7.com/api/lapp/token/get")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();
        String st = response.body().string();
        JSONObject json =  JSONObject.parseObject(st);
        String res = json.getString("data");
        json =  JSONObject.parseObject(res);
        res = json.getString("accessToken");
        return getEzvizList(res);
    }

    //获取萤石云视频列表
    public static List getEzvizList(String accessToken) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "accessToken="+accessToken+"&pageStart=0&pageSize=50");
        Request request = new Request.Builder()
                .url("https://open.ys7.com/api/lapp/live/video/list")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();

        List list = new ArrayList();
        String st = response.body().string();
        JSONObject json =  JSONObject.parseObject(st);
        list = json.getJSONArray("data");
        return list;
    }*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨两点钟同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值