package org.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.entity.Landmark;
import org.json.JSONObject;
import org.util.Json_Parsing;
import com.baidu.aip.face.AipFace;
public class FaceServlet extends HttpServlet {
/**
* @Fields serialVersionUID :该字段的意思:
* 个人博客:https://blog.csdn.net/qq_34137397
*/
private static final long serialVersionUID = 7753313994078255746L;
private String APP_ID = "你的APP_ID ";
private String API_KEY = "你的API_KEY ";
private String SECRET_KEY = "你的SECRET_KEY ";
private AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
private Map<Integer, Object> dataMap = new ConcurrentHashMap<Integer,Object>();
/**
* (非 Javadoc)
* <p>Description(描述):调用dopost的方法 </p>
* <p>Title: doGet</p>
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
* 个人博客:https://blog.csdn.net/qq_34137397
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* (非 Javadoc)
* <p>Description(描述):dopost方法 </p>
* <p>Title: doPost</p>
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
* 个人博客:https://blog.csdn.net/qq_34137397
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String tag = request.getParameter("tag");
String img = request.getParameter("img");
if(tag.equals("mouse")){
face_mouse(img,response,request);
}else if(tag.equals("eye")){
face_eye(img,response,request);
}
}
/**
*
* @Description: 该方法的主要作用:张张嘴验证
* @Title: face_jiance
* @param @param img
* @param @param response
* @param @param request 设定文件
* @return 返回类型:void
* @throws
* 个人博客:https://blog.csdn.net/qq_34137397
*/
private void face_mouse(String img, HttpServletResponse response,
HttpServletRequest request) {
Landmark landmark_next = face_jiance(img);
PrintWriter writer;
if ((landmark_next.getMouse_zhongxin().get(0) - landmark_next
.getMouse__top().get(0)) > 40&&(landmark_next.getMouse__bottom().get(0) - landmark_next
.getMouse_zhongxin().get(0)) > 30) {
try {
writer = response.getWriter();
writer.print("1");
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
writer = response.getWriter();
writer.print("0");
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @Description: 该方法的主要作用:眨眼对比
* @Title: face_eye
* @param @param img
* @param @param response
* @param @param request 设定文件
* @return 返回类型:void
* @throws
* 个人博客:https://blog.csdn.net/qq_34137397
*/
private void face_eye(String img, HttpServletResponse response,
HttpServletRequest request) {
Landmark landmark_next = face_jiance(img);
System.out.println("闭着眼的左眼上边与中间的值:"+((landmark_next.getLeft_eye_zhongxin().get(0))-(landmark_next.getLeft_eye_top().get(0))));
System.out.println("闭着眼的左眼下边与中间的值:"+((landmark_next.getLeft_eye_bottom().get(0))-(landmark_next.getLeft_eye_zhongxin().get(0))));
System.out.println("闭着眼的右眼上边与中间的值:"+((landmark_next.getRight_eye_zhongxin().get(0))-(landmark_next.getRight_eye_top().get(0))));
System.out.println("闭着眼的右眼下边与中间的值:"+((landmark_next.getRight_eye_bottom().get(0))-(landmark_next.getRight_eye_zhongxin().get(0))));
System.out.println("__________________________________________________");
PrintWriter writer;
if ((landmark_next.getLeft_eye_zhongxin().get(0))
- (landmark_next.getLeft_eye_top().get(0)) < 6
&& (landmark_next.getRight_eye_zhongxin().get(0))
- (landmark_next.getRight_eye_top().get(0)) < 6) {
System.out.println("进来了上边验证成功");
if ((landmark_next.getLeft_eye_bottom().get(0))
- (landmark_next.getLeft_eye_zhongxin().get(0)) < 6.6
&& (landmark_next.getRight_eye_bottom().get(0))
- (landmark_next.getRight_eye_zhongxin().get(0)) < 6.6) {
System.out.println("进来了下边验证成功");
try {
writer = response.getWriter();
writer.print("1");
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
writer = response.getWriter();
writer.print("0");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @Description: 该方法的主要作用:活体检测
* @Title: face_jiance
* @param @param img
* @param @return 设定文件
* @return 返回类型:Landmark
* @throws
* 个人博客:https://blog.csdn.net/qq_34137397
*/
private Landmark face_jiance(String img){
HashMap<String, String> options = new HashMap<String, String>();
options.put("face_field", "landmark");
options.put("face_type", "LIVE");
String image = img;
String imageType = "BASE64";
JSONObject res = null;
try {
res = client.detect(image, imageType, options);
} catch (Exception e) {
e.printStackTrace();
}
return Json_Parsing.parsingFaceJson(res);
}
}