Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614244
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-02-17 14:38:55


点击(此处)折叠或打开


  1.     /**人脸识别处理中方法
  2.      * @param
  3.      * @throws Exception
  4.      */
  5.     @RequestMapping(value="/faceRecognition")
  6.     @ResponseBody
  7.     public Object faceRecognition() throws Exception{
  8.         Map<String,Object> map = new HashMap<String,Object>();
  9.         String errInfo = "error";
  10.         PageData pd = new PageData();
  11.         pd = this.getPageData();
  12.         String imgData = pd.getString("PHOTODATA").replace("data:image/jpeg;base64,", "").replace("data:image/png;base64,", "");
  13.         String times = pd.getString("times");
  14.         String USERNAME = pd.getString("USERNAME");
  15.         pd.put("USERNAME", Tools.notEmpty(USERNAME)?USERNAME:Jurisdiction.getUsername());
  16.         PageData upd = new PageData();
  17.         upd = photoService.findFace(pd);
  18.         if("1".equals(times)) {                //第一次存,第二次根据两个base64进行头像识别
  19.             pd.put("PHOTO", imgData);        //把base64编码存到PHOTO0字段里面
  20.             pd.put("STATE", "yes");
  21.             if(null == upd) {
  22.                 photoService.addFace(pd);
  23.             }else {
  24.                 photoService.editFace(pd);
  25.             }
  26.             errInfo = "success";
  27.         }else {
  28.             PageData tpd = new PageData();
  29.             tpd.put("USERNAME", "FaceAccessToken");    //用户名为 FaceAccessToken 的在用户头像表SYS_USERPHOTO里,PHOTO2 存的是更新Token的时间,PHOTO3存的是Token的值,此数据所有用户共享
  30.             tpd = photoService.findById(tpd);
  31.             String accessToken = tpd.getString("PHOTO3");
  32.             if(DateUtil.getDaySub(tpd.getString("PHOTO2"),DateUtil.getDay()) > 29) { //access_token有过期时间为30天,过期后重新获取。
  33.                 accessToken = AuthService.getAuth();
  34.                 tpd.put("PHOTO2", DateUtil.getDay());
  35.                 tpd.put("PHOTO3", accessToken);
  36.                 photoService.edit(tpd);    
  37.             }
  38.             if(FaceMatch.getScore(upd.getString("PHOTO"),imgData,accessToken)) {
  39.                  errInfo = "success";
  40.                  upd.put("STATE", "yes");
  41.                  photoService.editFaceState(upd);//更新识别状态
  42.             }        
  43.         }
  44.         map.put("result", errInfo);
  45.         return map;
  46.     }
  47.     

  48. package org.fh.util;

  49. import org.json.JSONObject;

  50. import java.io.BufferedReader;
  51. import java.io.InputStreamReader;
  52. import java.net.HttpURLConnection;
  53. import java.net.URL;
  54. import java.util.List;
  55. import java.util.Map;

  56. /**
  57.  * 说明:获取百度人脸识别token类
  58.  * 作者:FH Admin
  59.  * from:fhadmin.cn
  60.  */
  61. public class AuthService {
  62.     
  63.     /**
  64.      * 获取权限token
  65.      * @return 返回示例:
  66.      * {
  67.      * "access_token": "24.460da4889caad24c1ccxxdb1fea17221975.2592000.1491995545.282335-1234567",
  68.      * "expires_in": 2592000
  69.      * }
  70.      */
  71.     public static String getAuth() {
  72.         // 官网获取的 API Key 更新为你注册的
  73.         String clientId = "GgNx1THTR6kScxx2cQe36jRT2a";
  74.         // 官网获取的 Secret Key 更新为你注册的
  75.         String clientSecret = "I0L1rNSWwIsu6zx3cxczz1hnBGDbCgrjkCvaV";
  76.         return getAuth(clientId, clientSecret);
  77.     }

  78.     /**
  79.      * 获取API访问token
  80.      * 该token有一定的有效期,需要自行管理,当失效时需重新获取.
  81.      * @param ak - 百度云官网获取的 API Key
  82.      * @param sk - 百度云官网获取的 Securet Key
  83.      * @return assess_token 示例:
  84.      * "24.460da4889caad24cc23231fea17221975.259110.14e91995545.282335-1234567"
  85.      */
  86.     public static String getAuth(String ak, String sk) {
  87.         // 获取token地址
  88.         String authHost = "";
  89.         String getAccessTokenUrl = authHost
  90.                 // 1. grant_type为固定参数
  91.                 + "grant_type=client_credentials"
  92.                 // 2. 官网获取的 API Key
  93.                 + "&client_id=" + ak
  94.                 // 3. 官网获取的 Secret Key
  95.                 + "&client_secret=" + sk;
  96.         try {
  97.             URL realUrl = new URL(getAccessTokenUrl);
  98.             // 打开和URL之间的连接
  99.             HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
  100.             connection.setRequestMethod("GET");
  101.             connection.connect();
  102.             // 获取所有响应头字段
  103.             Map<String, List<String>> map = connection.getHeaderFields();
  104.             // 遍历所有的响应头字段
  105.             for (String key : map.keySet()) {
  106.                 System.err.println(key + "--->" + map.get(key));
  107.             }
  108.             // 定义 BufferedReader输入流来读取URL的响应
  109.             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  110.             String result = "";
  111.             String line;
  112.             while ((line = in.readLine()) != null) {
  113.                 result += line;
  114.             }
  115.             /**
  116.              * 返回结果示例
  117.              */
  118.             JSONObject jsonObject = new JSONObject(result);
  119.             String access_token = jsonObject.getString("access_token");
  120.             return access_token;
  121.         } catch (Exception e) {
  122.             System.err.printf("获取token失败!");
  123.             e.printStackTrace(System.err);
  124.         }
  125.         return null;
  126.     }

  127. }


阅读(696) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~