1.pom中需要的引用:
<!--转到https://search.maven.org/search?q=tencentcloud-sdk-java并获取最新版本。-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.0.67</version>
</dependency>
2.具体案例代码
package com.jian.utils;
import com.google.gson.Gson;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
-
腾讯云接口
*/
public class TencentCloudInterface {
private static final String SECRET_ID = “你在腾讯云申请的apk的id”;//腾讯apk的id
private static final String SECRET_KEY = “你在腾讯申请的apk的key”;//腾讯云apk的key/**
-
腾讯云创建人员的接口
-
@param groupId-------------腾讯云人员库id(在腾讯云中唯一),比如你的项目id
-
@param personName------腾讯云人员的名字,比如这个项目下面的某个人员名字
-
@param personId------------腾讯云人员的id(在腾讯云中唯一),比如你项目下面某个人的id
-
@param url------------------(我是存的7牛云)7牛云暂存库人脸图片的路径(这里是你要传入的人脸照片的路径)
-
@return
*/
public static int createPerson(HttpServletRequest request, HttpServletResponse response,String groupId, String personName, String personId, String url){
try{
Credential cred = new Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("iai.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); IaiClient client = new IaiClient(cred, "ap-chengdu", clientProfile); Map map = new HashMap(); map.put("GroupId",groupId); map.put("PersonName",personName); map.put("PersonId",personId); map.put("Url",url); Gson gson = new Gson(); String params = gson.toJson(map);//格式为map格式 response.setContentType("text/javascript"); response.getWriter().print(params); //更换其它腾讯接口只需要更换此处即可开始 CreatePersonRequest req = CreatePersonRequest.fromJsonString(params, CreatePersonRequest.class); CreatePersonResponse resp = client.CreatePerson(req); System.out.println(CreatePersonRequest.toJsonString(resp)); //更换其它腾讯接口只需要更换此处即可结束 return 1;
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return -1;
} catch (Exception e){
System.out.println(e.toString());
return -2;
}
}
/**
-
腾讯云创建人员库接口
-
@param request---------请求
-
@param response--------返回
-
@param groupId---------人员库id
-
@param groupName-------人员库名字
-
@return
*/
public static int createGroup(HttpServletRequest request, HttpServletResponse response,String groupId, String groupName){
try{
Credential cred = new Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("iai.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); IaiClient client = new IaiClient(cred, "ap-chengdu", clientProfile); Map map = new HashMap(); map.put("GroupName",groupName); map.put("GroupId",groupId); Gson gson = new Gson(); String params = gson.toJson(map); response.setContentType("text/javascript"); response.getWriter().print(params); //更换其它腾讯接口只需要更换此处即可开始 CreateGroupRequest req = CreateGroupRequest.fromJsonString(params, CreateGroupRequest.class); CreateGroupResponse resp = client.CreateGroup(req); System.out.println(CreateGroupRequest.toJsonString(resp)); //更换其它腾讯接口只需要更换此处即可结束 return 1;
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return -1;
} catch (Exception e){
System.out.println(e.toString());
return -2;
}
}
/**
-
腾讯云删除人员接口
-
@param request----------请求
-
@param response---------返回
-
@param groupId----------人员库id
-
@param personId---------人员id
-
@return
*/
public static int deletePersonFromGroup(HttpServletRequest request, HttpServletResponse response,String groupId, String personId) {
try {
Credential cred = new Credential(SECRET_ID, SECRET_KEY);HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("iai.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); IaiClient client = new IaiClient(cred, "ap-chengdu", clientProfile); Map map = new HashMap(); map.put("GroupId", groupId); map.put("PersonId", personId); Gson gson = new Gson(); String params = gson.toJson(map); response.setContentType("text/javascript"); response.getWriter().print(params); //更换其它腾讯接口只需要更换此处即可开始 DeletePersonFromGroupRequest req = DeletePersonFromGroupRequest.fromJsonString(params, DeletePersonFromGroupRequest.class); DeletePersonFromGroupResponse resp = client.DeletePersonFromGroup(req); System.out.println(CreateGroupRequest.toJsonString(resp)); //更换其它腾讯接口只需要更换此处即可结束 return 1;
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return -1;
} catch (Exception e) {
System.out.println(e.toString());
return -2;
}
}
-
}