157 lines
5.6 KiB
Java
157 lines
5.6 KiB
Java
/*
|
||
package com.guwan.controller;
|
||
|
||
import com.mapper.UserMapper;
|
||
import com.pojo.Result;
|
||
import com.tencentcloudapi.common.AbstractModel;
|
||
import com.tencentcloudapi.common.Credential;
|
||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||
import com.tencentcloudapi.iai.v20200303.IaiClient;
|
||
import com.tencentcloudapi.iai.v20200303.models.CreatePersonRequest;
|
||
import com.tencentcloudapi.iai.v20200303.models.CreatePersonResponse;
|
||
import com.tencentcloudapi.iai.v20200303.models.PersonExDescriptionInfo;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RequestParam;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.Base64;
|
||
|
||
*/
|
||
/**
|
||
* @author: 顾挽
|
||
* @description:
|
||
* @create: 2024-09-06 15:31
|
||
**//*
|
||
|
||
|
||
@RequestMapping("/face")
|
||
@RestController
|
||
public class FaceController {
|
||
|
||
@Autowired
|
||
private UserMapper userMapper;
|
||
|
||
@PostMapping("/upload")
|
||
public Result uploadFile(
|
||
@RequestParam("file") MultipartFile file,
|
||
@RequestParam("username") String username,
|
||
@RequestParam("realName") String realName,
|
||
@RequestParam("idCard") String idCard) {
|
||
|
||
|
||
System.out.println("username: " + username);
|
||
System.out.println("realName: " + realName);
|
||
System.out.println("idCard: " + idCard);
|
||
|
||
if (file.isEmpty()) {
|
||
return Result.error("File is empty");
|
||
}
|
||
|
||
try{
|
||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||
Credential cred = new Credential("AKIDrhdr2JYdZMzQf2KWiqx47Vps3KsOtQFC",
|
||
"8PByngyJZkgZGsRrGiLviSIKSnGlI427");
|
||
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||
HttpProfile httpProfile = new HttpProfile();
|
||
httpProfile.setEndpoint("iai.tencentcloudapi.com");
|
||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||
ClientProfile clientProfile = new ClientProfile();
|
||
clientProfile.setHttpProfile(httpProfile);
|
||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||
IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);
|
||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||
CreatePersonRequest req = new CreatePersonRequest();
|
||
req.setGroupId("userRe");
|
||
req.setPersonName(username);
|
||
req.setPersonId(idCard);
|
||
|
||
PersonExDescriptionInfo[] personExDescriptionInfos1 = new PersonExDescriptionInfo[1];
|
||
PersonExDescriptionInfo personExDescriptionInfo1 = new PersonExDescriptionInfo();
|
||
personExDescriptionInfo1.setPersonExDescriptionIndex(0L);
|
||
personExDescriptionInfo1.setPersonExDescription(realName);
|
||
personExDescriptionInfos1[0] = personExDescriptionInfo1;
|
||
|
||
req.setPersonExDescriptionInfos(personExDescriptionInfos1);
|
||
|
||
|
||
byte[] fileBytes = file.getBytes();
|
||
|
||
// 将字节数组转换为 Base64 字符串
|
||
String base64Encoded = Base64.getEncoder().encodeToString(fileBytes);
|
||
|
||
|
||
req.setImage(base64Encoded);
|
||
|
||
|
||
// 返回的resp是一个CreatePersonResponse的实例,与请求对象对应
|
||
CreatePersonResponse resp = client.CreatePerson(req);
|
||
// 输出json格式的字符串回包
|
||
System.out.println(AbstractModel.toJsonString(resp));
|
||
|
||
if (resp.getFaceId() != null){
|
||
|
||
|
||
userMapper.changeFaceState(username);
|
||
|
||
try {
|
||
// 获取项目根目录
|
||
String rootDir = System.getProperty("user.dir");
|
||
|
||
// 确保上传目录存在
|
||
File uploadDir = new File(rootDir + File.separator + "uploads");
|
||
if (!uploadDir.exists()) {
|
||
uploadDir.mkdirs();
|
||
}
|
||
|
||
|
||
|
||
// 保存文件到上传目录
|
||
String filePath = uploadDir.getPath() + File.separator + file.getOriginalFilename();
|
||
file.transferTo(new File(filePath));
|
||
|
||
System.out.println("File uploaded successfully: " + filePath);
|
||
|
||
*/
|
||
/* return Result.success("File uploaded successfully: " + filePath);*//*
|
||
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
*/
|
||
/* return Result.error("File upload failed");*//*
|
||
|
||
}
|
||
|
||
*/
|
||
/* return Result.success();*//*
|
||
|
||
}
|
||
|
||
|
||
|
||
return Result.success();
|
||
|
||
|
||
|
||
} catch (TencentCloudSDKException e) {
|
||
System.out.println(e.toString());
|
||
return Result.error(e.toString());
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|
||
*/
|