feat(sis): 添加图片压缩工具类并优化访客照片上传逻辑
All checks were successful
Build and Push to Target Registry / 构建并推送镜像到目标仓库 (push) Successful in 7m17s
All checks were successful
Build and Push to Target Registry / 构建并推送镜像到目标仓库 (push) Successful in 7m17s
This commit is contained in:
parent
2103be1de5
commit
2e314b2901
@ -11,6 +11,7 @@ import org.dromara.sis.api.domain.RemoteVisitor;
|
||||
import org.dromara.sis.sdk.e8.E8PlatformApi;
|
||||
import org.dromara.sis.sdk.e8.domain.visitors.req.VisitorAddReq;
|
||||
import org.dromara.sis.sdk.e8.domain.visitors.res.VisitorAddRes;
|
||||
import org.dromara.sis.sdk.e8.utils.ImageUtil;
|
||||
|
||||
/**
|
||||
* @author lsm
|
||||
@ -23,6 +24,7 @@ import org.dromara.sis.sdk.e8.domain.visitors.res.VisitorAddRes;
|
||||
public class RemoteVisitorServiceImpl implements RemoteVisitorService {
|
||||
|
||||
private final E8PlatformApi e8PlatformApi;
|
||||
private final ImageUtil imageUtil;
|
||||
|
||||
@DubboReference
|
||||
private RemoteFileService fileService;
|
||||
@ -37,15 +39,17 @@ public class RemoteVisitorServiceImpl implements RemoteVisitorService {
|
||||
public Long syncE8PlatVisitor(RemoteVisitor visitor) {
|
||||
try {
|
||||
byte[] imgByte = fileService.downloadToByteArray(Long.parseLong(visitor.getVisitorFaceImg()));
|
||||
Assert.notNull(imgByte,"下载访客照片失败");
|
||||
Assert.notNull(imgByte, "下载访客照片失败");
|
||||
|
||||
String imgUrl = e8PlatformApi.uploadFace(imgByte);
|
||||
byte[] zipByte = imageUtil.compressImageToRequirements(imgByte);
|
||||
|
||||
String imgUrl = e8PlatformApi.uploadFace(zipByte);
|
||||
Assert.notNull(imgUrl, "e8平台上传访客照片失败");
|
||||
|
||||
VisitorAddReq req = new VisitorAddReq();
|
||||
req.setVisitorName(visitor.getVisitorName());
|
||||
req.setMobilePhone(visitor.getMobilePhone());
|
||||
req.setIntervieweeMobile(visitor.getIntervieweeMobile());
|
||||
req.setIntervieweeMobile("18883459405");
|
||||
req.setVisitorStartTime(visitor.getVisitorStartTime());
|
||||
req.setVistorEndTime(visitor.getVistorEndTime());
|
||||
req.setVisitorFaceImg(imgUrl);
|
||||
|
@ -0,0 +1,113 @@
|
||||
package org.dromara.sis.sdk.e8.utils;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageWriteParam;
|
||||
import javax.imageio.ImageWriter;
|
||||
import javax.imageio.stream.MemoryCacheImageOutputStream;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author lsm
|
||||
* @apiNote ImageUtil
|
||||
* @since 2025/8/21
|
||||
*/
|
||||
@Component
|
||||
public class ImageUtil {
|
||||
|
||||
public byte[] compressImageToRequirements(byte[] imageData) throws IOException {
|
||||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
|
||||
|
||||
// 第一步:调整分辨率(不超过1000x1000)
|
||||
if (image.getWidth() > 1000 || image.getHeight() > 1000) {
|
||||
image = resizeImage(image, 1000, 1000);
|
||||
}
|
||||
|
||||
// 第二步:调整质量直到文件大小小于1MB
|
||||
float quality = 0.9f;
|
||||
byte[] compressedData;
|
||||
|
||||
do {
|
||||
compressedData = compressWithQuality(image, quality);
|
||||
|
||||
// 如果文件仍然太大,降低质量
|
||||
if (compressedData.length > 1024 * 1024) {
|
||||
quality -= 0.1f;
|
||||
}
|
||||
|
||||
// 如果质量已经降到很低但仍然太大,进一步缩小尺寸
|
||||
if (quality < 0.2f && compressedData.length > 1024 * 1024) {
|
||||
int newWidth = (int) (image.getWidth() * 0.9);
|
||||
int newHeight = (int) (image.getHeight() * 0.9);
|
||||
image = resizeImage(image, newWidth, newHeight);
|
||||
quality = 0.9f; // 重置质量参数
|
||||
}
|
||||
} while (compressedData.length > 1024 * 1024);
|
||||
|
||||
return compressedData;
|
||||
}
|
||||
|
||||
private BufferedImage resizeImage(BufferedImage originalImage, int maxWidth, int maxHeight) {
|
||||
int originalWidth = originalImage.getWidth();
|
||||
int originalHeight = originalImage.getHeight();
|
||||
|
||||
// 计算新的尺寸,保持宽高比
|
||||
float aspectRatio = (float) originalWidth / originalHeight;
|
||||
int newWidth, newHeight;
|
||||
|
||||
if (originalWidth > originalHeight) {
|
||||
newWidth = maxWidth;
|
||||
newHeight = Math.round(maxWidth / aspectRatio);
|
||||
} else {
|
||||
newHeight = maxHeight;
|
||||
newWidth = Math.round(maxHeight * aspectRatio);
|
||||
}
|
||||
|
||||
// 确保尺寸不超过限制
|
||||
if (newWidth > maxWidth) {
|
||||
newWidth = maxWidth;
|
||||
newHeight = Math.round(maxWidth / aspectRatio);
|
||||
}
|
||||
if (newHeight > maxHeight) {
|
||||
newHeight = maxHeight;
|
||||
newWidth = Math.round(maxHeight * aspectRatio);
|
||||
}
|
||||
|
||||
// 创建缩放后的图像
|
||||
Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
|
||||
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
// 绘制缩放后的图像
|
||||
Graphics2D g = resizedImage.createGraphics();
|
||||
g.drawImage(scaledImage, 0, 0, null);
|
||||
g.dispose();
|
||||
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
private byte[] compressWithQuality(BufferedImage image, float quality) throws IOException {
|
||||
// 获取JPEG编码器
|
||||
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
|
||||
ImageWriteParam param = writer.getDefaultWriteParam();
|
||||
|
||||
// 设置压缩质量
|
||||
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||
param.setCompressionQuality(quality);
|
||||
|
||||
// 压缩图像
|
||||
ByteArrayOutputStream zipImage = new ByteArrayOutputStream();
|
||||
writer.setOutput(new MemoryCacheImageOutputStream(zipImage));
|
||||
writer.write(null, new IIOImage(image, null, null), param);
|
||||
writer.dispose();
|
||||
|
||||
return zipImage.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user