WANG пре 5 година
родитељ
комит
d5ca33b9be
1 измењених фајлова са 65 додато и 3 уклоњено
  1. 65 3
      src/main/java/cn/com/qmth/examcloud/web/baidu/BaiduClient.java

+ 65 - 3
src/main/java/cn/com/qmth/examcloud/web/baidu/BaiduClient.java

@@ -3,11 +3,13 @@ package cn.com.qmth.examcloud.web.baidu;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.config.RequestConfig;
 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.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -184,11 +186,11 @@ public class BaiduClient {
 			responseHolder = new JsonHttpResponseHolder(statusCode, obj);
 
 			if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
-				log.error("[Baidu AI]. verifyFaceLiveness; statusCode=" + statusCode
+				log.error("[Baidu AI]. verifyFaceLiveness(String); statusCode=" + statusCode
 						+ "; responseEntity=" + entityStr);
 			} else {
 				if (log.isDebugEnabled()) {
-					log.debug("[Baidu AI]. verifyFaceLiveness; statusCode=" + statusCode
+					log.debug("[Baidu AI]. verifyFaceLiveness(String); statusCode=" + statusCode
 							+ "; responseEntity=" + entityStr);
 				}
 			}
@@ -200,7 +202,7 @@ public class BaiduClient {
 		}
 
 		if (log.isDebugEnabled()) {
-			log.debug("[Baidu AI]. verifyFaceLiveness; imageUrl=" + imageUrl + "; cost "
+			log.debug("[Baidu AI]. verifyFaceLiveness(String); imageUrl=" + imageUrl + "; cost "
 					+ (System.currentTimeMillis() - s) + " ms.");
 		}
 
@@ -261,4 +263,64 @@ public class BaiduClient {
 		return responseHolder;
 	}
 
+	public JsonHttpResponseHolder verifyFaceLiveness(String imageUrl, String backupImageUrl)
+			throws StatusException {
+
+		JsonHttpResponseHolder responseHolder = verifyFaceLiveness(imageUrl);
+
+		if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
+			return responseHolder;
+		}
+
+		JSONObject respBody = responseHolder.getRespBody();
+		String errMsg = respBody.getString("error_message");
+		if (null != errMsg && errMsg.startsWith("INVALID_IMAGE_URL")) {
+			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);
+				}
+
+				byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
+				if (100 > byteArray.length) {
+					throw new StatusException("902", "invalid image size. url=" + backupImageUrl);
+				}
+
+				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;
+	}
+
 }