|
@@ -164,130 +164,6 @@ public class FaceppClient {
|
|
return responseHolder;
|
|
return responseHolder;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 人脸识别<br>
|
|
|
|
- * 优先使用主地址调用face++<br>
|
|
|
|
- * 主地址无效时,使用备用地址调用face++.<br>
|
|
|
|
- * 备用地址也无效时,使用备用地址下载数据,使用下载数据的base64加密串调用face++<br>
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- *
|
|
|
|
- * @param faceToken
|
|
|
|
- * face++预存照片
|
|
|
|
- * @param imageUrl
|
|
|
|
- * 主地址
|
|
|
|
- * @param backupImageUrl
|
|
|
|
- * 备用地址
|
|
|
|
- * @return
|
|
|
|
- * @throws StatusException
|
|
|
|
- * code为801,802,803表示图片地址无效
|
|
|
|
- */
|
|
|
|
- public JsonHttpResponseHolder compareWithTokenAndImageUrl(String faceToken, String imageUrl,
|
|
|
|
- String backupImageUrl) throws StatusException {
|
|
|
|
-
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
|
- log.debug("[Face++ Request]. faceToken=" + faceToken + "; imageUrl=" + imageUrl
|
|
|
|
- + "; backupImageUrl=" + backupImageUrl);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- JsonHttpResponseHolder responseHolder = null;
|
|
|
|
-
|
|
|
|
- boolean exceptionWhenUsingImageUrl = false;
|
|
|
|
- boolean exceptionWhenUsingBackupImageUrl = false;
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- responseHolder = compareWithTokenAndImageUrl(faceToken, imageUrl);
|
|
|
|
- } catch (ExamCloudRuntimeException e) {
|
|
|
|
- exceptionWhenUsingImageUrl = true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (exceptionWhenUsingImageUrl) {
|
|
|
|
- try {
|
|
|
|
- responseHolder = compareWithTokenAndImageUrl(faceToken, backupImageUrl);
|
|
|
|
- } catch (ExamCloudRuntimeException e) {
|
|
|
|
- exceptionWhenUsingBackupImageUrl = true;
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
|
- return responseHolder;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String errMsg = responseHolder.getRespBody().getString("error_message");
|
|
|
|
- if (retry(errMsg)) {
|
|
|
|
- try {
|
|
|
|
- responseHolder = compareWithTokenAndImageUrl(faceToken, backupImageUrl);
|
|
|
|
- } catch (ExamCloudRuntimeException e) {
|
|
|
|
- exceptionWhenUsingBackupImageUrl = true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
|
- return responseHolder;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String errMsg = responseHolder.getRespBody().getString("error_message");
|
|
|
|
-
|
|
|
|
- if (exceptionWhenUsingBackupImageUrl || retry(errMsg)) {
|
|
|
|
- HttpGet get = new HttpGet(backupImageUrl);
|
|
|
|
- get.setConfig(FaceppClient.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("801",
|
|
|
|
- "fail to download image file. url=" + backupImageUrl);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
|
|
|
|
- if (100 > byteArray.length) {
|
|
|
|
- throw new StatusException("802", "invalid image size. url=" + backupImageUrl);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- imageBase64 = Base64.encodeBase64String(byteArray);
|
|
|
|
-
|
|
|
|
- } catch (StatusException e) {
|
|
|
|
- log.error("fail to download image file. url=" + backupImageUrl, e);
|
|
|
|
- throw e;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("fail to download image file. url=" + backupImageUrl, e);
|
|
|
|
- throw new StatusException("803", "fail to download file. url=" + backupImageUrl, e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(response);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
|
- log.debug("download image file successfully; url=" + backupImageUrl + "; cost "
|
|
|
|
- + (System.currentTimeMillis() - s) + " ms.");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- responseHolder = compareWithTokenAndBase64(faceToken, imageBase64);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return responseHolder;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 是否重试
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param errMsg
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private boolean retry(String errMsg) {
|
|
|
|
- if (null != errMsg) {
|
|
|
|
- if (errMsg.startsWith("INVALID_IMAGE_URL")
|
|
|
|
- || errMsg.startsWith("IMAGE_DOWNLOAD_TIMEOUT")) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 人脸识别
|
|
* 人脸识别
|
|
*
|
|
*
|
|
@@ -422,4 +298,128 @@ public class FaceppClient {
|
|
return responseHolder;
|
|
return responseHolder;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 人脸识别<br>
|
|
|
|
+ * 优先使用主地址调用face++<br>
|
|
|
|
+ * 主地址无效时,使用备用地址调用face++.<br>
|
|
|
|
+ * 备用地址也无效时,使用备用地址下载数据,使用下载数据的base64加密串调用face++<br>
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ *
|
|
|
|
+ * @param faceToken
|
|
|
|
+ * face++预存照片
|
|
|
|
+ * @param imageUrl
|
|
|
|
+ * 主地址
|
|
|
|
+ * @param backupImageUrl
|
|
|
|
+ * 备用地址
|
|
|
|
+ * @return
|
|
|
|
+ * @throws StatusException
|
|
|
|
+ * code为801,802,803表示图片地址无效
|
|
|
|
+ */
|
|
|
|
+ public JsonHttpResponseHolder compareWithTokenAndImageUrl(String faceToken, String imageUrl,
|
|
|
|
+ String backupImageUrl) throws StatusException {
|
|
|
|
+
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++ Request]. faceToken=" + faceToken + "; imageUrl=" + imageUrl
|
|
|
|
+ + "; backupImageUrl=" + backupImageUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
|
+
|
|
|
|
+ boolean exceptionWhenUsingImageUrl = false;
|
|
|
|
+ boolean exceptionWhenUsingBackupImageUrl = false;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ responseHolder = compareWithTokenAndImageUrl(faceToken, imageUrl);
|
|
|
|
+ } catch (ExamCloudRuntimeException e) {
|
|
|
|
+ exceptionWhenUsingImageUrl = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (exceptionWhenUsingImageUrl) {
|
|
|
|
+ try {
|
|
|
|
+ responseHolder = compareWithTokenAndImageUrl(faceToken, backupImageUrl);
|
|
|
|
+ } catch (ExamCloudRuntimeException e) {
|
|
|
|
+ exceptionWhenUsingBackupImageUrl = true;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String errMsg = responseHolder.getRespBody().getString("error_message");
|
|
|
|
+ if (retry(errMsg)) {
|
|
|
|
+ try {
|
|
|
|
+ responseHolder = compareWithTokenAndImageUrl(faceToken, backupImageUrl);
|
|
|
|
+ } catch (ExamCloudRuntimeException e) {
|
|
|
|
+ exceptionWhenUsingBackupImageUrl = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String errMsg = responseHolder.getRespBody().getString("error_message");
|
|
|
|
+
|
|
|
|
+ if (exceptionWhenUsingBackupImageUrl || retry(errMsg)) {
|
|
|
|
+ HttpGet get = new HttpGet(backupImageUrl);
|
|
|
|
+ get.setConfig(FaceppClient.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("801",
|
|
|
|
+ "fail to download image file. url=" + backupImageUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
|
|
|
|
+ if (100 > byteArray.length) {
|
|
|
|
+ throw new StatusException("802", "invalid image size. url=" + backupImageUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ imageBase64 = Base64.encodeBase64String(byteArray);
|
|
|
|
+
|
|
|
|
+ } catch (StatusException e) {
|
|
|
|
+ log.error("fail to download image file. url=" + backupImageUrl, e);
|
|
|
|
+ throw e;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("fail to download image file. url=" + backupImageUrl, e);
|
|
|
|
+ throw new StatusException("803", "fail to download file. url=" + backupImageUrl, e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("download image file successfully; url=" + backupImageUrl + "; cost "
|
|
|
|
+ + (System.currentTimeMillis() - s) + " ms.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ responseHolder = compareWithTokenAndBase64(faceToken, imageBase64);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 是否重试
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param errMsg
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private boolean retry(String errMsg) {
|
|
|
|
+ if (null != errMsg) {
|
|
|
|
+ if (errMsg.startsWith("INVALID_IMAGE_URL")
|
|
|
|
+ || errMsg.startsWith("IMAGE_DOWNLOAD_TIMEOUT")) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|