java调用腾讯云人脸存储(创建人员库,创建人员,删除人员)

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值