|
@@ -2,12 +2,14 @@ package cn.com.qmth.examcloud.web.facepp;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
@@ -71,7 +73,7 @@ public class FaceppClient {
|
|
TimeUnit.SECONDS);
|
|
TimeUnit.SECONDS);
|
|
cm.setValidateAfterInactivity(1000);
|
|
cm.setValidateAfterInactivity(1000);
|
|
cm.setMaxTotal(8000);
|
|
cm.setMaxTotal(8000);
|
|
- cm.setDefaultMaxPerRoute(800);
|
|
|
|
|
|
+ cm.setDefaultMaxPerRoute(200);
|
|
httpclient = HttpClients.custom().setConnectionManager(cm).disableAutomaticRetries()
|
|
httpclient = HttpClients.custom().setConnectionManager(cm).disableAutomaticRetries()
|
|
.build();
|
|
.build();
|
|
|
|
|
|
@@ -170,11 +172,89 @@ public class FaceppClient {
|
|
|
|
|
|
JSONObject respBody = responseHolder.getRespBody();
|
|
JSONObject respBody = responseHolder.getRespBody();
|
|
String errMsg = respBody.getString("error_message");
|
|
String errMsg = respBody.getString("error_message");
|
|
- if (errMsg.startsWith("")) {
|
|
|
|
|
|
+ if (null != errMsg && errMsg.startsWith("INVALID_IMAGE_URL")) {
|
|
|
|
+ responseHolder = compareWithTokenAndImageUrl(faceToken, backupImageUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ respBody = responseHolder.getRespBody();
|
|
|
|
+ errMsg = respBody.getString("error_message");
|
|
|
|
|
|
|
|
+ HttpGet get = new HttpGet(backupImageUrl);
|
|
|
|
+ get.setConfig(FaceppClient.requestConfig);
|
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
|
+ String imageBase64 = null;
|
|
|
|
+ try {
|
|
|
|
+ response = httpclient.execute(get);
|
|
|
|
+ byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
|
|
|
|
+ imageBase64 = Base64.encodeBase64String(byteArray);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(response);
|
|
}
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
+ responseHolder = compareWithTokenAndBase64(faceToken, imageBase64);
|
|
|
|
+
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 人脸识别
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param faceToken
|
|
|
|
+ * @param imageBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JsonHttpResponseHolder compareWithTokenAndBase64(String faceToken, String imageBase64) {
|
|
|
|
+
|
|
|
|
+ String url = "https://api-cn.faceplusplus.com/facepp/v3/compare";
|
|
|
|
+
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setConfig(FaceppClient.requestConfig);
|
|
|
|
+
|
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
+ builder.addTextBody("api_key", apiKey);
|
|
|
|
+ builder.addTextBody("api_secret", apiSecret);
|
|
|
|
+ builder.addTextBody("face_token1", faceToken);
|
|
|
|
+ builder.addTextBody("image_base64_2", imageBase64);
|
|
|
|
+ HttpEntity httpEntity = builder.build();
|
|
|
|
+
|
|
|
|
+ httpPost.setEntity(httpEntity);
|
|
|
|
+
|
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
|
+
|
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
|
+ log.error("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ } else {
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. faceToken=" + faceToken + "; imageBase64=" + imageBase64
|
|
|
|
+ + "; cost " + (System.currentTimeMillis() - s) + " ms.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return responseHolder;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|