1. 首先是用的百度AI人脸识别接口,去百度申请以下参数作为预备
2.直接导入写好的人脸工具类对人脸进行注册
package cn.abtu.config;
import com.baidu.aip.face.AipFace;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/**
* @author PDX
* @website https://blog.csdn.net/Gaowumao
* @Date 2022-05-08 17:05
* @Description
*/
@Component
public class BaiduAiUtils {
@Value("${ai.appId}")
private String APP_ID;
@Value("${ai.apiKey}")
private String API_KEY;
@Value("${ai.secretKey}")
private String SECRET_KEY;
@Value("${ai.imageType}")
private String IMAGE_TYPE;
@Value("${ai.groupId}")
private String groupId;
private AipFace client;
private HashMap<String, String> map = new HashMap<>();
private BaiduAiUtils() {
map.put("quality_control", "NORMAL");//图片质量
map.put("liveness_control", "NONE");//活体检测
}
@PostConstruct
public void init() {
client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
}
/**
* 人脸人注册,将用户照片存入脸库中
*
* @param userId
* @param image
* @return
*/
public Boolean faceRegister(String userId, String image) {
//人脸注册
JSONObject res = client.addUser(image, IMAGE_TYPE, groupId, userId, map);
Integer errorCode = res.getInt("error_code");
if (errorCode != 0) {
throw new BusinessExeption("106", "人脸注册失败");
}else if (errorCode == 223113) {
throw new BusinessException(223113,"人脸有被遮挡");
} else if (errorCode==223114) {
throw new BusinessException(223114,"人脸模糊,拍摄时请不要晃动手机");
} else if (errorCode == 223116) {
throw new BusinessException(223116,"请勿遮挡面部");
}
}
return true;
}
/**
* 人脸更新,更新人脸库中的用户照片
* @param userId
* @param image
* @return
*/
public Boolean faceUpdate(String userId,String image){
//人脸更新
JSONObject res = client.updateUser(image, IMAGE_TYPE, groupId, userId, map);
Integer errorCode = res.getInt("error_code");
System.out.println(res);
return errorCode == 0 ? true : false;
}
/**
* 人脸检测。判断上传的图片中是否具有面部信息
* @param image
* @return
*/
public Boolean faceCheck(String image){
JSONObject res = client.detect(image, IMAGE_TYPE, map);
if (res.has("error_code") && res.getInt("error_code") == 0){
JSONObject resultObject = res.getJSONObject("result");
Integer faceNum = resultObject.getInt("face_num");
return faceNum == 1? true : false;
}else {
return false;
}
}
/**
* 人脸查找:查找人脸库中最相似的人脸并返回数据
* 处理:用户的匹配得分(score)大于80分,即可认为是同一个人
* @param image
* @return
*/
public String faceSearch(String image){
if (image==null|| StringUtils.isEmpty(image)){
throw new BusinessExeption("107","照片为空");
}
JSONObject res = client.search(image, IMAGE_TYPE, groupId, map);
if (res.has("error_code") && res.getInt("error_code") == 0){
JSONObject result = res.getJSONObject("result");
JSONArray userList = result.getJSONArray("user_list");
if (userList.length() > 0){
JSONObject user = userList.getJSONObject(0);
double score = user.getDouble("score");
if (score > 80){
return user.getString("user_id");
}
}
}
throw new BusinessExeption("106","未检测到人脸");
}
/**
* 人脸删除
* @param userId
* @return
*/
public Boolean faceDelete(String userId){
JSONObject res = client.deleteUser(groupId,userId,map);
Integer errorCode = res.getInt("error_code");
return errorCode == 0 ? true : false;
}
public Boolean faceGetUser(String userId){
JSONObject res = client.getUser(userId,groupId, map);
if (res.has("error_code") && res.getInt("error_code") == 0){
JSONObject resultObject = res.getJSONObject("result");
return resultObject.getJSONArray("user_list").length() > 0;
}
return false;
}
}
3.前端传入图片路径base64工具类把图片转码成base64
package cn.abtu.config;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Repository;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Repository
public class Base64Util {
/**
* 图片URL转Base64编码
* @param imgUrl 图片URL
* @return Base64编码
*/
public static String imageUrlToBase64(String imgUrl) {
URL url = null;
InputStream is = null;
ByteArrayOutputStream outStream = null;
HttpURLConnection httpUrl = null;
try {
url = new URL(imgUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
is = httpUrl.getInputStream();
outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用输入流从buffer里把数据读取出来
while( (len = is.read(buffer)) != -1 ){
//用输出流往buffer写里入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 对字节数组Base64编码
return encode(outStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
if(outStream != null) {
outStream.close();
}
if(httpUrl != null) {
httpUrl.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 图片转字符串
* @param image 图片Buffer
* @return Base64编码
*/
public static String encode(byte[] image){
return Base64.encodeBase64String(image);
}
/**
* 字符替换
* @param str 字符串
* @return 替换后的字符串
*/
public static String replaceEnter(String str){
String reg ="[\n-\r]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
return m.replaceAll("");
}
}
4.再插入人员信息时调用工具类 对比信息就可以了,具体需求具体场景自由搭配.