⼤华sdk(java)上传⼈脸图⽚到⼈脸库,订阅⼈脸识别对⽐上传⼈脸图⽚到⼈脸库
controller:
@RestController
@RequestMapping("/facePicture")
public class FacePictureHandleController {
@Autowired
private FacePictureHandleService facePictureHandleService;
@RequestMapping(value = "/handle/uploadFile")
public String padUploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
try {
FacePicture facePicture=new FacePicture();
HttpSession session = Session(true);
Map<String, String> userInfo = (Map<String, String>) session
.getAttribute(SessionConstants.SESSION_KEY_USER_INFO);
facePicture.("loginUser"));
File f=null;
String fileName = OriginalFilename();
try {
//获取⽂件字节数组
byte [] bytes = Bytes();
File pfile = new File("/fileupload/");
//判断⽂件夹是否存在
if(!ists()){
//不存在时,创建⽂件夹
pfile.mkdirs();
}
//创建⽂件
f = new File(pfile, fileName);
//写⼊指定⽂件夹
OutputStream out = new FileOutputStream(f);
out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
return "上传失败";
}
Integer facePictureRes = facePictureHandleService.saveFacePictureHandle(Path());
if(facePictureRes==0){
return "上传失败";
}
return "上传成功";
} catch (Exception e) {
<("上传⽂件异常", e);
}
return "上传失败";
}
}
service:
@Service
public class FacePictureHandleService {
/**
/**
*配置⽂件
*/
@Value("${groupId}")
private String groupId;
@Value("${uploadWaitTime}")
private int uploadWaitTime;
@Value("${groupName}")
private String groupName;
@Autowired
private FacePictureHandleDao facePictureHandleDao;
@Autowired
private FaceDetectService faceDetectService;
@Autowired
private UserInfoDAO userInfoDao;
/**
* 保存⼈脸图⽚到⼈脸库
* @return
*/
public Integer saveFacePictureHandle(FacePicture facePicture, String filePath) throws UnsupportedEncodingException { Memory fileMemory=new Memory(2000000);
try {
ToolKits.ReadAllFileToMemory(filePath,fileMemory);
} catch (IOException e) {
e.printStackTrace();
}
List<UserInfo> byUserNameIn = userInfoDao.findByUserNameIn(Collections.CardId())); facePicture.
(0).getName());
facePicture.setFaceDatabaseName(groupName);
String gender = (0).getGender();
int sex=0;
if("F".equals(gender)){
sex=2;
}else if("M".equals(gender)){
sex=1;
}
Long fId= facePictureHandleDao.CardId());
if(fId==null){ //未上传图⽚
/
/上传图⽚到⼈脸库
boolean updalodRes = addPerson(groupId, fileMemory, Name(), sex, false, "", 0, "",facePicture);
if(updalodRes){
facePictureHandleDao.save(facePicture);
return 1;
}
}else{ //已上传过图⽚
String uId = facePictureHandleDao.queryFaceDatabaseUid(fId);
//修改⼈脸库图⽚
boolean updalodRes = modifyPerson(groupId, uId,fileMemory, Name(), sex, false, "", 0, "");
if(updalodRes){
facePicture.setId(fId);
int i = facePictureHandleDao.updatePictureData(fId);
return i;
}
}
File file=new File(filePath);
if(file!=null){
file.deleteOnExit();
file.deleteOnExit();
}
return 0;
}
public File transferToFile(MultipartFile multipartFile) {
File file = null;
try {
String originalFilename = OriginalFilename();
String[] filename = originalFilename.split("\\.");
ateTempFile(filename[0]+"temp", filename[1]);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 添加⼈员
* @param groupId ⼈脸库ID
* @param memory 图⽚数据
* @param personName 姓名
* @param sex 性别
* @param isBirthday 是否下发⽣⽇
* @param birthday ⽣⽇
* @param byIdType 证件类型
* @param idNo 证件号
* @return
*/
public boolean addPerson(String groupId,
Memory memory,
String personName,
int sex,
boolean isBirthday,
String birthday,
int byIdType,
String idNo,
FacePicture facePicture) throws UnsupportedEncodingException {
/*
* ⼊参
getsavefilename*/
NetSDKLib.NET_IN_OPERATE_FACERECONGNITIONDB stuIn = new NetSDKLib.NET_IN_OPERATE_FACERECONGNITIONDB(); OperateType = NetSDKLib.EM_OPERATE_FACERECONGNITIONDB_TYPE.NET_FACERECONGNITIONDB_ADD;
/ 使⽤⼈员扩展信息 //
stuIn.bUsePersonInfoEx = 1;
// ⼈脸库ID
System.Bytes(), 0, stuIn.stPersonInfoEx.szGroupID, 0, Bytes().leng
th);
// ⽣⽇设置
if(isBirthday) {
String[] birthdays = birthday.split("-");
stuIn.stPersonInfoEx.wYear = (short)Integer.parseInt(birthdays[0]);
stuIn.stPersonInfoEx.byMonth = (byte)Integer.parseInt(birthdays[1]);
stuIn.stPersonInfoEx.byDay = (byte)Integer.parseInt(birthdays[2]);
}
// 性别,1-男,2-⼥,作为查询条件时,此参数填0,则表⽰此参数⽆效
stuIn.stPersonInfoEx.bySex = (byte)sex;
// ⼈员名字
try {
System.Bytes("GBK"), 0, stuIn.stPersonInfoEx.szPersonName, 0, Bytes("GBK").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 证件类型
stuIn.stPersonInfoEx.byIDType = (byte)byIdType;
// 证件号
System.Bytes(), 0, stuIn.stPersonInfoEx.szID, 0, Bytes().length);
// 图⽚张数、⼤⼩、缓存设置
if(memory != null) {
stuIn.stPersonInfoEx.wFacePicNum = 1; // 图⽚张数
stuIn.stPersonInfoEx.szFacePicInfo[0].dwFileLenth = (int)memory.size(); // 图⽚⼤⼩
stuIn.stPersonInfoEx.szFacePicInfo[0].dwOffSet = 0;
stuIn.nBufferLen = (int)memory.size();
stuIn.pBuffer = memory;
}
/*
* 出参
*/
NetSDKLib.NET_OUT_OPERATE_FACERECONGNITIONDB stuOut = new NetSDKLib.NET_OUT_OPERATE_FACERECONGNITIONDB() ;
stuIn.write();
boolean bRet = Netsdk().CLIENT_LoginHandle(), stuIn, stuOut, uploadWaitTime); ad();
if(bRet) {
System.out.println("szUID : " + new String(stuOut.szUID).trim());
} else {
}
facePicture.setFaceDatabaseUid(new String(stuOut.szUID, "GBK").trim());
return bRet;
}
/**
* 修改⼈员信息
* @param groupId ⼈脸库ID
* @param uid ⼈员唯⼀标识符
* @param memory 图⽚数据
* @param personName 姓名
* @param sex 性别
* @param isBirthday 是否下发⽣⽇
* @param birthday ⽣⽇
* @param byIdType 证件类型
* @param idNo 证件号
* @return true:成功 , false:失败
*/
public boolean modifyPerson(String groupId,
String uid,
Memory memory,
String personName,
int sex,
boolean isBirthday,
String birthday,
int byIdType,
String idNo) {
String idNo) {
/
/ ⼊参
NetSDKLib.NET_IN_OPERATE_FACERECONGNITIONDB stuIn = new NetSDKLib.NET_IN_OPERATE_FACERECONGNITIONDB();
/ 使⽤⼈员扩展信息
stuIn.bUsePersonInfoEx = 1;
// ⼈脸库ID
System.Bytes(), 0, stuIn.stPersonInfoEx.szGroupID, 0, Bytes().length);
// ⼈员唯⼀标识符
System.Bytes(), 0, stuIn.stPersonInfoEx.szUID, 0, Bytes().length);
/
/ ⽣⽇设置
if(isBirthday) {
String[] birthdays = birthday.split("-");
stuIn.stPersonInfoEx.wYear = (short)Integer.parseInt(birthdays[0]);
stuIn.stPersonInfoEx.byMonth = (byte)Integer.parseInt(birthdays[1]);
stuIn.stPersonInfoEx.byDay = (byte)Integer.parseInt(birthdays[2]);
}
// 性别,1-男,2-⼥,作为查询条件时,此参数填0,则表⽰此参数⽆效
stuIn.stPersonInfoEx.bySex = (byte)sex;
// ⼈员名字
try {
System.Bytes("GBK"), 0, stuIn.stPersonInfoEx.szPersonName, 0, Bytes("GBK").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 证件类型
stuIn.stPersonInfoEx.byIDType = (byte)byIdType;
// 证件号
System.Bytes(), 0, stuIn.stPersonInfoEx.szID, 0, Bytes().length);
// 图⽚张数、⼤⼩、缓存设置
if(memory != null) {
stuIn.stPersonInfoEx.wFacePicNum = 1; // 图⽚张数
stuIn.stPersonInfoEx.szFacePicInfo[0].dwFileLenth = (int)memory.size(); // 图⽚⼤⼩
stuIn.stPersonInfoEx.szFacePicInfo[0].dwOffSet = 0;
stuIn.nBufferLen = (int)memory.size();
stuIn.pBuffer = memory;
}
// 出参
NetSDKLib.NET_OUT_OPERATE_FACERECONGNITIONDB stuOut = new NetSDKLib.NET_OUT_OPERATE_FACERECONGNITIONDB() ;
stuIn.write();
boolean b = Netsdk().CLIENT_LoginHandle(), stuIn, stuOut, uploadWaitTime);
if(!b) {
return false;
}
return true;
}
/**
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论