赞
踩
下面主要贴出服务端代码
添加依赖pom.xml
- <!-- 百度人脸识别 -->
- <dependency>
- <groupId>com.squareup.okhttp3</groupId>
- <artifactId>okhttp</artifactId>
- <version>4.9.0</version>
- </dependency>
几个参数先配置好配置文件application.yml
- # 百度
- baidu:
- api_key: **********
- secret_key: *********************
- plan_id: *****
下面贴核心方法代码
- package com.ruoyi.ehs.service;
-
- import com.alibaba.fastjson2.JSONObject;
- import com.ruoyi.common.core.redis.RedisCache;
- import com.ruoyi.ehs.domain.vo.BaiduUploadUserInfoVo;
- import okhttp3.*;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
-
- import javax.annotation.Resource;
- import java.io.IOException;
-
- /**
- * 百度:调用百度接口 service
- */
- @Service
- public class BaiduService {
-
-
- static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
-
- @Value("${baidu.api_key}")
- private String API_KEY;
- @Value("${baidu.secret_key}")
- private String SECRET_KEY;
- @Value("${baidu.plan_id}")
- private String PLAN_ID;
-
-
- //redis 存百度 AccessToken
- static final String BAIDU_ACCESS_TOKEN = "baidu_access_token";
- @Resource
- private RedisCache redisCache;
-
-
- /**
- * 获取 AccessToken
- */
- public void getAccessToken() {
- try {
- MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
- RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
- + "&client_secret=" + SECRET_KEY);
- Request request = new Request.Builder()
- .url("https://aip.baidubce.com/oauth/2.0/token")
- .method("POST", body)
- .addHeader("Content-Type", "application/x-www-form-urlencoded")
- .build();
- Response response = HTTP_CLIENT.newCall(request).execute();
- String str = response.body().string();
- JSONObject jsonObject = JSONObject.parseObject(str);
- String accessToken = jsonObject.getString("access_token");
- redisCache.setCacheObject(BAIDU_ACCESS_TOKEN, accessToken);
- System.out.println("---------------");
- System.out.println("AccessToken:" + accessToken);
- System.out.println("---------------");
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * 获取 VerifyToken
- */
- public String getVerifyToken() {
- try {
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, "{\"plan_id\":" + PLAN_ID + "}");
- String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
- Request request = new Request.Builder()
- .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=" + accessToken)
- .method("POST", body)
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- Response response = HTTP_CLIENT.newCall(request).execute();
- String str = response.body().string();
- JSONObject jsonObject = JSONObject.parseObject(str);
- Boolean success = jsonObject.getBoolean("success");
- if (success) {
- String result = jsonObject.getString("result");
- JSONObject resultObject = JSONObject.parseObject(result);
- return resultObject.getString("verify_token");
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return null;
- }
-
- //======================================================================
-
-
- /**
- * 百度:指定用户信息上传接口
- *
- * @param idName
- * @param idNo
- */
- public BaiduUploadUserInfoVo getBaiduUploadUserInfoVo(String idName, String idNo) {
- try {
- MediaType mediaType = MediaType.parse("application/json");
- String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
- String verifyToken = getVerifyToken();
-
- RequestBody body = RequestBody.create(mediaType, "{\"id_name\":\"" + idName + "\",\"id_no\":\"" + idNo + "\",\"verify_token\":\"" + verifyToken + "\"}");
- Request request = new Request.Builder()
- .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/idcard/submit?access_token=" + accessToken)
- .method("POST", body)
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- Response response = HTTP_CLIENT.newCall(request).execute();
- String str = response.body().string();
- JSONObject jsonObject = JSONObject.parseObject(str);
- Boolean success = jsonObject.getBoolean("success");
- BaiduUploadUserInfoVo vo = new BaiduUploadUserInfoVo();
- vo.setAccessToken(accessToken);
- vo.setVerifyToken(verifyToken);
- vo.setSuccess(success);
- return vo;
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 百度:指定用户信息认证结果
- *
- * @param verifyToken
- */
- public Boolean faceprintResult(String verifyToken) {
- try {
- String accessToken = redisCache.getCacheObject(BAIDU_ACCESS_TOKEN);
-
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, "{\"verify_token\":\""+verifyToken+"\"}");
- Request request = new Request.Builder()
- .url("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token="+accessToken)
- .method("POST", body)
- .addHeader("Content-Type", "application/json")
- .build();
- Response response = HTTP_CLIENT.newCall(request).execute();
- String str = response.body().string();
- JSONObject jsonObject = JSONObject.parseObject(str);
- Boolean success = jsonObject.getBoolean("success");
- return success;
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- }
获取 AccessToken 我是用了定时任务是更新这个token,然后放在redis上,这里不贴没用的代码了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。