天天看點

從海康ISC擷取視訊預覽位址java

具體可參考海康官網開發文檔說明:

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;
    }*/
}