You've already forked FrameTour-BE
上报图片直接进行裁切
This commit is contained in:
@@ -12,6 +12,7 @@ import com.ycwl.basic.mapper.DeviceMapper;
|
|||||||
import com.ycwl.basic.mapper.FaceSampleMapper;
|
import com.ycwl.basic.mapper.FaceSampleMapper;
|
||||||
import com.ycwl.basic.mapper.SourceMapper;
|
import com.ycwl.basic.mapper.SourceMapper;
|
||||||
import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
|
import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
|
||||||
|
import com.ycwl.basic.model.pc.device.entity.DeviceCropConfig;
|
||||||
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
|
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
|
||||||
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
|
||||||
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
||||||
@@ -53,6 +54,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import java.awt.image.RasterFormatException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@@ -244,8 +247,14 @@ public class ViidController {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
DeviceConfigEntity deviceConfig = deviceRepository.getDeviceConfig(device.getId());
|
DeviceConfigEntity deviceConfig = deviceRepository.getDeviceConfig(device.getId());
|
||||||
|
if (deviceConfig == null) {
|
||||||
|
log.warn("设备配置不存在:" + deviceID);
|
||||||
|
return new VIIDBaseResp(
|
||||||
|
new ResponseStatusObject(faceId, "/VIID/Faces", "0", "OK", sdfTime.format(new Date()))
|
||||||
|
);
|
||||||
|
}
|
||||||
int viidMode = 0;
|
int viidMode = 0;
|
||||||
if (deviceConfig != null && deviceConfig.getViidType() != null) {
|
if (deviceConfig.getViidType() != null) {
|
||||||
viidMode = deviceConfig.getViidType();
|
viidMode = deviceConfig.getViidType();
|
||||||
}
|
}
|
||||||
Date shotTime = null;
|
Date shotTime = null;
|
||||||
@@ -285,7 +294,7 @@ public class ViidController {
|
|||||||
if (viidMode == 0) {
|
if (viidMode == 0) {
|
||||||
// 遍历每个图片对象
|
// 遍历每个图片对象
|
||||||
// 先找到type14的图片
|
// 先找到type14的图片
|
||||||
List<SubImageInfoObject> type14ImageList = subImageList.getSubImageInfoObject().stream().filter(subImage -> "14".equals(subImage.getType())).collect(Collectors.toList());
|
List<SubImageInfoObject> type14ImageList = subImageList.getSubImageInfoObject().stream().filter(subImage -> "14".equals(subImage.getType())).toList();
|
||||||
for (SubImageInfoObject subImage : subImageList.getSubImageInfoObject()) {
|
for (SubImageInfoObject subImage : subImageList.getSubImageInfoObject()) {
|
||||||
// base64转换成MultipartFIle
|
// base64转换成MultipartFIle
|
||||||
MultipartFile file = ImageUtils.base64ToMultipartFile(subImage.getData());
|
MultipartFile file = ImageUtils.base64ToMultipartFile(subImage.getData());
|
||||||
@@ -318,7 +327,7 @@ public class ViidController {
|
|||||||
faceSampleMapper.updateScore(faceSample.getId(), addFaceResp.getScore());
|
faceSampleMapper.updateScore(faceSample.getId(), addFaceResp.getScore());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (deviceConfig != null && Integer.valueOf(1).equals(deviceConfig.getEnablePreBook())) {
|
if (Integer.valueOf(1).equals(deviceConfig.getEnablePreBook())) {
|
||||||
DynamicTaskGenerator.addTask(faceSample.getId());
|
DynamicTaskGenerator.addTask(faceSample.getId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -337,11 +346,37 @@ public class ViidController {
|
|||||||
// 上传oss
|
// 上传oss
|
||||||
MultipartFile _file = ImageUtils.base64ToMultipartFile(_subImage.getData());
|
MultipartFile _file = ImageUtils.base64ToMultipartFile(_subImage.getData());
|
||||||
String filename = StorageUtil.joinPath(PHOTO_PATH, UUID.randomUUID() + "." + ext);
|
String filename = StorageUtil.joinPath(PHOTO_PATH, UUID.randomUUID() + "." + ext);
|
||||||
String _sourceUrl = scenicStorageAdapter.uploadFile(_file, filename);
|
ThreadPoolExecutor executor = getExecutor(scenicId);
|
||||||
scenicStorageAdapter.setAcl(StorageAcl.PUBLIC_READ, filename);
|
executor.execute(() -> {
|
||||||
source.setUrl(_sourceUrl);
|
DeviceCropConfig cropConfig = deviceConfig.getCropConfig();
|
||||||
source.setPosJson(JSON.toJSONString(facePosition));
|
MultipartFile _finalFile = _file;
|
||||||
sourceMapper.add(source);
|
if (cropConfig != null) {
|
||||||
|
if (cropConfig.getCropType() == 1) {
|
||||||
|
// 按固定位置截图
|
||||||
|
try {
|
||||||
|
_finalFile = ImageUtils.cropImage(_file, cropConfig.getTargetX(), cropConfig.getTargetY(), cropConfig.getTargetWidth(), cropConfig.getTargetHeight());
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
} catch (RasterFormatException ignored) {
|
||||||
|
}
|
||||||
|
} else if (cropConfig.getCropType() == 2) {
|
||||||
|
// 按人脸位置
|
||||||
|
try {
|
||||||
|
int targetX = facePosition.getLtX() - (cropConfig.getTargetWidth() - facePosition.getWidth());
|
||||||
|
int targetY = facePosition.getLtY() - (cropConfig.getTargetHeight() - facePosition.getHeight());
|
||||||
|
_finalFile = ImageUtils.cropImage(_file, targetX, targetY, cropConfig.getTargetWidth(), cropConfig.getTargetHeight());
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
} catch (RasterFormatException ignored) {
|
||||||
|
}
|
||||||
|
facePosition.setImgHeight(cropConfig.getTargetHeight());
|
||||||
|
facePosition.setImgWidth(cropConfig.getTargetWidth());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String _sourceUrl = scenicStorageAdapter.uploadFile(_finalFile, filename);
|
||||||
|
scenicStorageAdapter.setAcl(StorageAcl.PUBLIC_READ, filename);
|
||||||
|
source.setUrl(_sourceUrl);
|
||||||
|
source.setPosJson(JSON.toJSONString(facePosition));
|
||||||
|
sourceMapper.add(source);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
log.info("人脸信息及原图{}张入库成功!设备ID:{}", type14ImageList.size(), deviceID);
|
log.info("人脸信息及原图{}张入库成功!设备ID:{}", type14ImageList.size(), deviceID);
|
||||||
}
|
}
|
||||||
|
@@ -278,7 +278,6 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
|||||||
try {
|
try {
|
||||||
AipFace client = getClient();
|
AipFace client = getClient();
|
||||||
HashMap<String, Object> options = new HashMap<>();
|
HashMap<String, Object> options = new HashMap<>();
|
||||||
options.put("quality_control", "LOW");
|
|
||||||
options.put("max_user_num", "50");
|
options.put("max_user_num", "50");
|
||||||
try {
|
try {
|
||||||
searchFaceLimiter.acquire();
|
searchFaceLimiter.acquire();
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package com.ycwl.basic.model.pc.device.entity;
|
package com.ycwl.basic.model.pc.device.entity;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -62,4 +63,13 @@ public class DeviceConfigEntity {
|
|||||||
private Integer videoFree;
|
private Integer videoFree;
|
||||||
private Long pairDevice;
|
private Long pairDevice;
|
||||||
private String videoCrop;
|
private String videoCrop;
|
||||||
|
private String cropConfig;
|
||||||
|
|
||||||
|
public void setCropConfig(DeviceCropConfig cropConfig) {
|
||||||
|
this.cropConfig = JSON.toJSONString(cropConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceCropConfig getCropConfig() {
|
||||||
|
return JSON.parseObject(cropConfig, DeviceCropConfig.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,13 @@
|
|||||||
|
package com.ycwl.basic.model.pc.device.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class DeviceCropConfig {
|
||||||
|
// 截图类型 0:无截图 1:固定截图 2:按人脸位置截图
|
||||||
|
private Integer cropType;
|
||||||
|
private Integer targetWidth;
|
||||||
|
private Integer targetHeight;
|
||||||
|
private Integer targetX;
|
||||||
|
private Integer targetY;
|
||||||
|
}
|
@@ -3,7 +3,10 @@ package com.ycwl.basic.utils;
|
|||||||
import cn.hutool.core.codec.Base64;
|
import cn.hutool.core.codec.Base64;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -21,6 +24,34 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
return new Base64DecodedMultipartFile(b, baseStrs[0]);
|
return new Base64DecodedMultipartFile(b, baseStrs[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MultipartFile cropImage(MultipartFile file, int x, int y, int w, int h) throws IOException {
|
||||||
|
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||||
|
if (image.getWidth() > w) {
|
||||||
|
w = image.getWidth();
|
||||||
|
}
|
||||||
|
if (image.getHeight() > h) {
|
||||||
|
h = image.getHeight();
|
||||||
|
}
|
||||||
|
int targetX = x;
|
||||||
|
if (x < 0) {
|
||||||
|
targetX = 0;
|
||||||
|
} else if ((x + w) > image.getWidth()) {
|
||||||
|
targetX = image.getWidth() - w;
|
||||||
|
}
|
||||||
|
int targetY = y;
|
||||||
|
if (y < 0) {
|
||||||
|
targetY = 0;
|
||||||
|
} else if ((y + h) > image.getHeight()) {
|
||||||
|
targetY = image.getHeight() - h;
|
||||||
|
}
|
||||||
|
BufferedImage targetImage = image.getSubimage(targetX, targetY, w, h);
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(targetImage, "jpg", baos);
|
||||||
|
baos.close();
|
||||||
|
return new Base64DecodedMultipartFile(baos.toByteArray(), "image/jpeg");
|
||||||
|
}
|
||||||
|
|
||||||
public static class Base64DecodedMultipartFile implements MultipartFile {
|
public static class Base64DecodedMultipartFile implements MultipartFile {
|
||||||
|
|
||||||
private final byte[] imgContent;
|
private final byte[] imgContent;
|
||||||
|
Reference in New Issue
Block a user