|
@@ -1,5 +1,6 @@
|
|
|
package cn.com.qmth.examcloud.web.baidu;
|
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -19,6 +20,7 @@ import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
@@ -27,7 +29,6 @@ import cn.com.qmth.examcloud.commons.helpers.JsonHttpResponseHolder;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
-import cn.com.qmth.examcloud.commons.util.UrlUtil;
|
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
|
|
/**
|
|
@@ -142,7 +143,7 @@ public class BaiduClient {
|
|
|
|
|
|
int statusCode = responseHolder.getStatusCode();
|
|
|
if (HttpStatus.SC_OK != statusCode) {
|
|
|
- throw new StatusException("901", "[Baidu AI]. fail to get access_token");
|
|
|
+ throw new StatusException("920", "[Baidu AI]. fail to get access_token");
|
|
|
}
|
|
|
|
|
|
JSONObject respBody = responseHolder.getRespBody();
|
|
@@ -160,19 +161,27 @@ public class BaiduClient {
|
|
|
*/
|
|
|
public JsonHttpResponseHolder verifyFaceLiveness(String imageUrl) {
|
|
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Baidu AI]. verifyFaceLiveness(String); imageUrl=" + imageUrl);
|
|
|
+ }
|
|
|
+
|
|
|
String accessToken = getAccessToken();
|
|
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token="
|
|
|
+ accessToken;
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setConfig(BaiduClient.requestConfig);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
|
|
|
Map<String, String> params = Maps.newHashMap();
|
|
|
|
|
|
params.put("image", imageUrl);
|
|
|
params.put("image_type", "URL");
|
|
|
|
|
|
- httpPost.setEntity(new StringEntity(JsonUtil.toJson(params), "UTF-8"));
|
|
|
+ List<Map<String, String>> list = Lists.newArrayList();
|
|
|
+ list.add(params);
|
|
|
+
|
|
|
+ httpPost.setEntity(new StringEntity(JsonUtil.toJson(list), "UTF-8"));
|
|
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
JsonHttpResponseHolder responseHolder = null;
|
|
@@ -185,14 +194,9 @@ public class BaiduClient {
|
|
|
JSONObject obj = JSON.parseObject(entityStr);
|
|
|
responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
|
|
|
- if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
- log.error("[Baidu AI]. verifyFaceLiveness(String); statusCode=" + statusCode
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Baidu AI]. verifyFaceLiveness(String); statusCode=" + statusCode
|
|
|
+ "; responseEntity=" + entityStr);
|
|
|
- } else {
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
- log.debug("[Baidu AI]. verifyFaceLiveness(String); statusCode=" + statusCode
|
|
|
- + "; responseEntity=" + entityStr);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
@@ -218,21 +222,23 @@ public class BaiduClient {
|
|
|
*/
|
|
|
public JsonHttpResponseHolder verifyFaceLivenessUseBase64(String base64) {
|
|
|
|
|
|
- base64 = UrlUtil.encode(base64);
|
|
|
-
|
|
|
String accessToken = getAccessToken();
|
|
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token="
|
|
|
+ accessToken;
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.setConfig(BaiduClient.requestConfig);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
|
|
|
Map<String, String> params = Maps.newHashMap();
|
|
|
|
|
|
params.put("image", base64);
|
|
|
params.put("image_type", "BASE64");
|
|
|
|
|
|
- httpPost.setEntity(new StringEntity(JsonUtil.toJson(params), "UTF-8"));
|
|
|
+ List<Map<String, String>> list = Lists.newArrayList();
|
|
|
+ list.add(params);
|
|
|
+
|
|
|
+ httpPost.setEntity(new StringEntity(JsonUtil.toJson(list), "UTF-8"));
|
|
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
JsonHttpResponseHolder responseHolder = null;
|
|
@@ -263,63 +269,86 @@ public class BaiduClient {
|
|
|
return responseHolder;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 百度活体检测<br>
|
|
|
+ * 优先使用主地址调用baidu活体检测接口<br>
|
|
|
+ * 主地址无效时,使用备用地址调用baidu活体检测接口<br>
|
|
|
+ * 备用地址也无效时,使用备用地址下载数据,使用下载数据的base64加密串调用baidu活体检测接口<br>
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param imageUrl
|
|
|
+ * 主地址
|
|
|
+ * @param backupImageUrl
|
|
|
+ * 备用地址
|
|
|
+ * @return
|
|
|
+ * @throws StatusException
|
|
|
+ * code为901,902,903表示图片地址无效
|
|
|
+ */
|
|
|
public JsonHttpResponseHolder verifyFaceLiveness(String imageUrl, String backupImageUrl)
|
|
|
throws StatusException {
|
|
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Face++]. verifyFaceLiveness(String,String); imageUrl=" + imageUrl
|
|
|
+ + "; backupImageUrl=" + backupImageUrl);
|
|
|
+ }
|
|
|
+
|
|
|
JsonHttpResponseHolder responseHolder = verifyFaceLiveness(imageUrl);
|
|
|
|
|
|
- if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
+ JSONObject respBody = responseHolder.getRespBody();
|
|
|
+ long errCode = respBody.getLong("error_code");
|
|
|
+
|
|
|
+ if (0 == errCode) {
|
|
|
return responseHolder;
|
|
|
}
|
|
|
|
|
|
- JSONObject respBody = responseHolder.getRespBody();
|
|
|
- String errMsg = respBody.getString("error_message");
|
|
|
- if (null != errMsg && errMsg.startsWith("INVALID_IMAGE_URL")) {
|
|
|
+ if (222204 == errCode || 222013 == errCode) {
|
|
|
responseHolder = verifyFaceLiveness(backupImageUrl);
|
|
|
}
|
|
|
|
|
|
respBody = responseHolder.getRespBody();
|
|
|
- errMsg = respBody.getString("error_message");
|
|
|
-
|
|
|
- if (null != errMsg && errMsg.startsWith("INVALID_IMAGE_URL")) {
|
|
|
- HttpGet get = new HttpGet(backupImageUrl);
|
|
|
- get.setConfig(BaiduClient.requestConfig);
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- String imageBase64 = null;
|
|
|
- long s = System.currentTimeMillis();
|
|
|
- try {
|
|
|
- response = httpclient.execute(get);
|
|
|
-
|
|
|
- if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
|
|
|
- throw new StatusException("901",
|
|
|
- "fail to download file. url=" + backupImageUrl);
|
|
|
- }
|
|
|
+ errCode = respBody.getLong("error_code");
|
|
|
|
|
|
- byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
|
|
|
- if (100 > byteArray.length) {
|
|
|
- throw new StatusException("902", "invalid image size. url=" + backupImageUrl);
|
|
|
- }
|
|
|
+ if (0 == errCode) {
|
|
|
+ return responseHolder;
|
|
|
+ }
|
|
|
|
|
|
- imageBase64 = Base64.encodeBase64String(byteArray);
|
|
|
+ HttpGet get = new HttpGet(backupImageUrl);
|
|
|
+ get.setConfig(BaiduClient.requestConfig);
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ String imageBase64 = null;
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+ response = httpclient.execute(get);
|
|
|
|
|
|
- } catch (StatusException e) {
|
|
|
- log.error("fail to download file. url=" + backupImageUrl, e);
|
|
|
- throw e;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("fail to download file. url=" + backupImageUrl, e);
|
|
|
- throw new StatusException("903", "fail to download file. url=" + backupImageUrl, e);
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(response);
|
|
|
+ if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
|
|
|
+ throw new StatusException("901", "fail to download file. url=" + backupImageUrl);
|
|
|
}
|
|
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
- log.debug("[Face++]. verifyFaceLiveness(String,String); download image; url="
|
|
|
- + backupImageUrl + "; cost " + (System.currentTimeMillis() - s) + " ms.");
|
|
|
+ byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
|
|
|
+ if (100 > byteArray.length) {
|
|
|
+ throw new StatusException("902", "invalid image size. url=" + backupImageUrl);
|
|
|
}
|
|
|
|
|
|
- responseHolder = verifyFaceLivenessUseBase64(imageBase64);
|
|
|
+ imageBase64 = Base64.encodeBase64String(byteArray);
|
|
|
+
|
|
|
+ } catch (StatusException e) {
|
|
|
+ log.error("fail to download file. url=" + backupImageUrl, e);
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("fail to download file. url=" + backupImageUrl, e);
|
|
|
+ throw new StatusException("903", "fail to download file. url=" + backupImageUrl, e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Face++]. verifyFaceLiveness(String,String); download image; url="
|
|
|
+ + backupImageUrl + "; cost " + (System.currentTimeMillis() - s) + " ms.");
|
|
|
}
|
|
|
|
|
|
+ responseHolder = verifyFaceLivenessUseBase64(imageBase64);
|
|
|
+
|
|
|
return responseHolder;
|
|
|
}
|
|
|
|