当前位置:   article > 正文

springboot对接百度人脸识别接口_springboot 整合百度人脸接口

springboot 整合百度人脸接口

下面主要贴出服务端代码

添加依赖pom.xml

  1. <!-- 百度人脸识别 -->
  2. <dependency>
  3. <groupId>com.squareup.okhttp3</groupId>
  4. <artifactId>okhttp</artifactId>
  5. <version>4.9.0</version>
  6. </dependency>

几个参数先配置好配置文件application.yml

  1. # 百度
  2. baidu:
  3. api_key: **********
  4. secret_key: *********************
  5. plan_id: *****

下面贴核心方法代码

  1. package com.ruoyi.ehs.service;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.ruoyi.common.core.redis.RedisCache;
  4. import com.ruoyi.ehs.domain.vo.BaiduUploadUserInfoVo;
  5. import okhttp3.*;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import javax.annotation.Resource;
  9. import java.io.IOException;
  10. /**
  11. * 百度:调用百度接口 service
  12. */
  13. @Service
  14. public class BaiduService {
  15. static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
  16. @Value("${baidu.api_key}")
  17. private String API_KEY;
  18. @Value("${baidu.secret_key}")
  19. private String SECRET_KEY;
  20. @Value("${baidu.plan_id}")
  21. private String PLAN_ID;
  22. //redis 存百度 AccessToken
  23. static final String BAIDU_ACCESS_TOKEN = "baidu_access_token";
  24. @Resource
  25. private RedisCache redisCache;
  26. /**
  27. * 获取 AccessToken
  28. */
  29. public void getAccessToken() {
  30. try {
  31. MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
  32. RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
  33. + "&client_secret=" + SECRET_KEY);
  34. Request request = new Request.Builder()
  35. .url("https://aip.baidubce.com/oauth/2.0/token")
  36. .method("POST", body)
  37. .addHeader("Content-Type", "application/x-www-form-urlencoded")
  38. .build();
  39. Response response = HTTP_CLIENT.newCall(request).execute();
  40. String str = response.body().string();
  41. JSONObject jsonObject = JSONObject.parseObject(str);
  42. String accessToken = jsonObject.getString("access_token");
  43. redisCache.setCacheObject(BAIDU_ACCESS_TOKEN, accessToken);
  44. System.out.println("---------------");
  45. System.out.println("AccessToken:" + accessToken);
  46. System.out.println("---------------");
  47. } catch (IOException e) {
  48. throw new RuntimeException(e);
  49. }
  50. }
  51. /**
  52. * 获取 VerifyToken
  53. */
  54. public String getVerifyToken() {
  55. try {
  56. MediaType mediaType = MediaType.parse("application/json");
  57. RequestBody body = RequestBody.create(mediaType, "{\"plan_id\":" + PLAN_ID + "}");
  58. String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
  59. Request request = new Request.Builder()
  60. .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=" + accessToken)
  61. .method("POST", body)
  62. .addHeader("Content-Type", "application/json")
  63. .addHeader("Accept", "application/json")
  64. .build();
  65. Response response = HTTP_CLIENT.newCall(request).execute();
  66. String str = response.body().string();
  67. JSONObject jsonObject = JSONObject.parseObject(str);
  68. Boolean success = jsonObject.getBoolean("success");
  69. if (success) {
  70. String result = jsonObject.getString("result");
  71. JSONObject resultObject = JSONObject.parseObject(result);
  72. return resultObject.getString("verify_token");
  73. }
  74. } catch (IOException e) {
  75. throw new RuntimeException(e);
  76. }
  77. return null;
  78. }
  79. //======================================================================
  80. /**
  81. * 百度:指定用户信息上传接口
  82. *
  83. * @param idName
  84. * @param idNo
  85. */
  86. public BaiduUploadUserInfoVo getBaiduUploadUserInfoVo(String idName, String idNo) {
  87. try {
  88. MediaType mediaType = MediaType.parse("application/json");
  89. String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
  90. String verifyToken = getVerifyToken();
  91. RequestBody body = RequestBody.create(mediaType, "{\"id_name\":\"" + idName + "\",\"id_no\":\"" + idNo + "\",\"verify_token\":\"" + verifyToken + "\"}");
  92. Request request = new Request.Builder()
  93. .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/idcard/submit?access_token=" + accessToken)
  94. .method("POST", body)
  95. .addHeader("Content-Type", "application/json")
  96. .addHeader("Accept", "application/json")
  97. .build();
  98. Response response = HTTP_CLIENT.newCall(request).execute();
  99. String str = response.body().string();
  100. JSONObject jsonObject = JSONObject.parseObject(str);
  101. Boolean success = jsonObject.getBoolean("success");
  102. BaiduUploadUserInfoVo vo = new BaiduUploadUserInfoVo();
  103. vo.setAccessToken(accessToken);
  104. vo.setVerifyToken(verifyToken);
  105. vo.setSuccess(success);
  106. return vo;
  107. } catch (IOException e) {
  108. throw new RuntimeException(e);
  109. }
  110. }
  111. /**
  112. * 百度:指定用户信息认证结果
  113. *
  114. * @param verifyToken
  115. */
  116. public Boolean faceprintResult(String verifyToken) {
  117. try {
  118. String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
  119. MediaType mediaType = MediaType.parse("application/json");
  120. RequestBody body = RequestBody.create(mediaType, "{\"verify_token\":\""+verifyToken+"\"}");
  121. Request request = new Request.Builder()
  122. .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token="+accessToken)
  123. .method("POST", body)
  124. .addHeader("Content-Type", "application/json")
  125. .build();
  126. Response response = HTTP_CLIENT.newCall(request).execute();
  127. String str = response.body().string();
  128. JSONObject jsonObject = JSONObject.parseObject(str);
  129. Boolean success = jsonObject.getBoolean("success");
  130. return success;
  131. } catch (IOException e) {
  132. throw new RuntimeException(e);
  133. }
  134. }
  135. }

获取 AccessToken 我是用了定时任务是更新这个token,然后放在redis上,这里不贴没用的代码了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/938225
推荐阅读
相关标签
  

闽ICP备14008679号