具体可参考海康官网开发文档说明:
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;
}*/
}