个人用到的方法
package wltyx.nyybw.common.face;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.FaceVerifyRequest;
import com.baidu.aip.face.MatchRequest;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class FaceSpot {
private static final String AppID = "";
private static final String APIKey = "";
private static final String SecretKey = "";
static AipFace client = null;
static {
client = new AipFace(AppID, APIKey, SecretKey);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
}
/**
* 人脸检测
*
* @return
* @throws IOException
*/
public static String detectFace(File file, String max_face_num) {
try {
return detectFace(FileToByte(file), max_face_num);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸检测
*
* @return
* @throws IOException
*/
public static String detectFace(byte[] arg0, String max_face_num) {
try {
HashMap<String, String> options = new HashMap<String, String>();
options.put("face_field", "age,beauty,expression,faceshape,gender,glasses,race,qualities");
options.put("max_face_num", "2");
options.put("face_type", "LIVE");
// 图片数据
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
JSONObject res = client.detect(imgStr, imageType, options);
System.out.println(res.toString(2));
return res.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸比对
* @param file1
* @param file2
* @return
*/
public static String matchFace(File file1, File file2) {
try {
return matchFace(FileToByte(file1), FileToByte(file2));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸比对
*
* @param arg0
* 人脸1
* @param arg1
* 人脸2
* @return
*/
public static String matchFace(byte[] arg0, byte[] arg1) {
String imgStr1 = Base64Util.encode(arg0);
String imgStr2 = Base64Util.encode(arg1);
MatchRequest req1 = new MatchRequest(imgStr1, "BASE64");
MatchRequest req2 = new MatchRequest(imgStr2, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
return res.toString();
}
/**
* 人脸比对
*
* @param imgStr1
* 人脸1
* @param arg1
* 人脸2
* @return
*/
public static String matchFace(String imgStr1, byte[] arg1) {
String imgStr2 = Base64Util.encode(arg1);
MatchRequest req1 = new MatchRequest(imgStr1, "BASE64");
MatchRequest req2 = new MatchRequest(imgStr2, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
return res.toString();
}
/**
* 人脸比对
*
* @param imgStr1
* 人脸1
* @param imgStr2
* 人脸2
* @return
*/
public static String matchFace(String imgStr1, String imgStr2) {
MatchRequest req1 = new MatchRequest(imgStr1, "BASE64");
MatchRequest req2 = new MatchRequest(imgStr2, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
return res.toString();
}
/**
* 人脸搜索
* @param file
* @param groupIdList
* @param userId
* @return
*/
public static String searchFace(File file, String groupIdList, String userId) {
try {
return searchFace(FileToByte(file), groupIdList, userId);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸搜索
*
* @param arg0
* @param groupIdList
* @return
*/
public static String searchFace(byte[] arg0, String groupIdList, String userId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap<String, String> options = new HashMap<String, String>();
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
if (userId != null) {
options.put("user_id", userId);
}
options.put("max_user_num", "1");
JSONObject res = client.search(imgStr, imageType, groupIdList, options);
return res.toString(2);
}
/**
* 人脸搜索
*
* @param base64
* @param groupIdList
* @return
*/
public static String searchFace(String base64, String groupIdList, String userId) {
String imageType = "BASE64";
HashMap<String, String> options = new HashMap<String, String>();
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
if (userId != null) {
options.put("user_id", userId);
}
options.put("max_user_num", "1");
JSONObject res = client.search(base64, imageType, groupIdList, options);
return res.toString();
}
/**
* 增加用户
* @param file
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String addUser(File file, String userInfo, String userId, String groupId) {
try {
return addUser(FileToByte(file), userInfo, userId, groupId);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 增加用户
*
* @param arg0
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String addUser(byte[] arg0, String userInfo, String userId, String groupId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap<String, String> options = new HashMap<String, String>();
options.put("user_info", userInfo);
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
JSONObject res = client.addUser(imgStr, imageType, groupId, userId, options);
return res.toString(2);
}
/**
* 增加用户
*
* @param base64
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static JSONObject addUser(String base64, String userInfo, String userId, String groupId) {
String imageType = "BASE64";
HashMap<String, String> options = new HashMap<String, String>();
options.put("user_info", userInfo);
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
//注册人脸
JSONObject res = client.addUser(base64, imageType, groupId, userId, options);
//返回数据
return res;
}
public static String updateUser(File file, String userInfo, String userId, String groupId) {
try {
return updateUser(FileToByte(file), userInfo, userId, groupId);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 更新用户
*
* @param arg0
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String updateUser(byte[] arg0, String userInfo, String userId, String groupId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap<String, String> options = new HashMap<String, String>();
if (userInfo != null) {
options.put("user_info", userInfo);
}
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
JSONObject res = client.updateUser(imgStr, imageType, groupId, userId, options);
return res.toString(2);
}
/**
* 删除用户人脸信息
* @param userId
* @param groupId
* @param faceToken
* @return
*/
public static String deleteUserFace(String userId, String groupId, String faceToken) {
HashMap<String, String> options = new HashMap<String, String>();
// 人脸删除
JSONObject res = client.faceDelete(userId, groupId, faceToken, options);
return res.toString();
}
/**
* 查询用户信息
* @param userId
* @param groupId
* @return
*/
public static String searchUserInfo(String userId, String groupId) {
HashMap<String, String> options = new HashMap<String, String>();
// 用户信息查询
JSONObject res = client.getUser(userId, groupId, options);
return res.toString(2);
}
/**
* 获取用户人脸列表
* @param userId
* @param groupId
* @return
*/
public static String getUserFaceList(String userId, String groupId) {
HashMap<String, String> options = new HashMap<String, String>();
// 获取用户人脸列表
JSONObject res = client.faceGetlist(userId, groupId, options);
return res.toString(2);
}
/**
* 获取一组用户
* @param groupId
* @param returnNum
* @return
*/
public static String getGroupUsers(String groupId, String returnNum) {
HashMap<String, String> options = new HashMap<String, String>();
options.put("start", "0");
if (returnNum != null) {
options.put("length", returnNum);
}
// 获取用户列表
JSONObject res = client.getGroupUsers(groupId, options);
return res.toString(2);
}
/**
* 组用户复制
* @param userId
* @param srcGroupId
* @param dstGroupId
* @return
*/
public static String userCopy(String userId, String srcGroupId, String dstGroupId) {
HashMap<String, String> options = new HashMap<String, String>();
options.put("src_group_id", srcGroupId);
options.put("dst_group_id", dstGroupId);
// 复制用户
JSONObject res = client.userCopy(userId, options);
return res.toString(2);
}
/**
* 删除用户
* @param userId
* @param groupId
* @return
*/
public static String deleteUser(String userId, String groupId) {
HashMap<String, String> options = new HashMap<String, String>();
// 人脸删除
JSONObject res = client.deleteUser(groupId, userId, options);
return res.toString();
}
/**
* 增加组信息
* @param groupId
* @return
*/
public static String addGroup(String groupId) {
HashMap<String, String> options = new HashMap<String, String>();
// 创建用户组
JSONObject res = client.groupAdd(groupId, options);
return res.toString();
}
/**
* 删除
* @param groupId
* @return
*/
public static String deleteGroup(String groupId) {
HashMap<String, String> options = new HashMap<String, String>();
// 创建用户组
JSONObject res = client.groupDelete(groupId, options);
return res.toString();
}
/**
* 获取组列表
* @param length
* @return
*/
public static String getGroupList(String length) {
HashMap<String, String> options = new HashMap<String, String>();
options.put("start", "0");
options.put("length", length);
// 组列表查询
JSONObject res = client.getGroupList(options);
return res.toString();
}
/**
* 活体检测
* @param arg0
* @return
*/
public static String faceverify(byte[] arg0){
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
FaceVerifyRequest req = new FaceVerifyRequest(imgStr, imageType);
ArrayList<FaceVerifyRequest> list = new ArrayList<FaceVerifyRequest>();
list.add(req);
JSONObject res = client.faceverify(list);
return res.toString();
}
private static byte[] FileToByte(File file) throws IOException {
// 将数据转为流
InputStream content = new FileInputStream(file);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = content.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
// 获得二进制数组
return swapStream.toByteArray();
}
}
个人用到的工具类
package wltyx.nyybw.common.face;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FaceUtils {
//图片到byte数组
public static byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
//byte数组到图片
public static void byte2image(byte[] data,String path){
if(data.length<3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
public static String byte2string(byte[] data){
if(data==null||data.length<=1) return "0x";
if(data.length>200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for(int k=0;k<data.length;k++){
buf[k] = data[k]<0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(int k=0;k<buf.length;k++){
if(buf[k]<16) sb.append("0"+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x"+sb.toString().toUpperCase();
}
}