新增谷歌模型违规提示词或图片报错

模型扣除积分修改
压缩转产品图的输入
fix:sort时有类型转换异常
This commit is contained in:
litianxiang
2025-10-14 17:20:23 +08:00
parent 6ac9680c64
commit e6a0706d61
7 changed files with 328 additions and 65 deletions

View File

@@ -41,7 +41,7 @@ public enum CreditsEventsEnum {
MOOD_BOARD("MoodBoard","5"),
SKETCH_BOARD("SketchBoard","5"),
TO_PRODUCT_IMAGE("ToProductImage","5"),
TO_PRODUCT_IMAGE_ADVANCED("ToProductImageAdvanced","10"),
TO_PRODUCT_IMAGE_ADVANCED("ToProductImageAdvanced","15"),
RELIGHT("Relight","5"),
RELIGHT_FLUX("RelightFlux","10"),
QUESTIONNAIRE("Questionnaire","100"),
@@ -50,9 +50,13 @@ public enum CreditsEventsEnum {
POSE_TRANSFORMATION("PoseTransformation","10"),
OTHER("Other","5"),
SCETCH_TEXT2IMG("SketchText2Image","10"),
SCETCH_IMG2IMG("SketchImg2Image","15"),
WX_TEXT2IMG("WX_Text2Image", "5"),
QWEN_TEXT2IMG("QWEN_Text2Image", "5"),
DOUBAO_TEXT2IMG("Doubao_Text2Image", "5"),
QWEN_TEXT2IMG("QWEN_Text2Image", "10"),
DOUBAO_TEXT2IMG("Doubao_Text2Image", "10"),
DOUBAO_IMG2IMG_ADVANCED("Doubao_img2image_advanced", "10"),
DOUBAO_IMG2IMG_HIGH("Doubao_img2image_high", "15"),
WX_ANIMATION("WX_Animation", "30"),
FREEPIK_IMG2IMG("Freepik_img2img", "20"),
FLUX_IMG2IMG("Flux_img2img","10"),

View File

@@ -20,6 +20,8 @@ import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.*;
import java.net.URL;
import java.security.InvalidKeyException;
@@ -666,6 +668,168 @@ public class MinioUtil {
}
}
/**
* 获取压缩后的图片Base64编码
* @param path 图片的minio路径
* @param targetWidth 目标宽度
* @param targetHeight 目标高度
* @return 压缩后的图片Base64编码
* @throws IOException
*/
public String getCompressedImageAsBase64(String path, int targetWidth, int targetHeight) throws IOException {
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
// 检查桶是否存在
boolean found = doesObjectExist(bucketName, fileName);
if (!found) {
throw new IOException("Bucket " + bucketName + " does not exist");
}
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.build())) {
// 读取原始图片
BufferedImage originalImage = ImageIO.read(stream);
if (originalImage == null) {
throw new IOException("无法读取图片: " + path);
}
// 计算压缩比例,保持宽高比
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
double scaleX = (double) targetWidth / originalWidth;
double scaleY = (double) targetHeight / originalHeight;
double scale = Math.min(scaleX, scaleY); // 选择较小的缩放比例以保持宽高比
int newWidth = (int) (originalWidth * scale);
int newHeight = (int) (originalHeight * scale);
// 创建压缩后的图片
BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = compressedImage.createGraphics();
// 设置高质量渲染
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 绘制压缩后的图片
g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g2d.dispose();
// 转换为Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "JPEG", baos); // 使用JPEG格式以减小文件大小
byte[] imageBytes = baos.toByteArray();
log.info("图片压缩完成: {} -> {}x{} (原始: {}x{})", path, newWidth, newHeight, originalWidth, originalHeight);
return Base64.getEncoder().encodeToString(imageBytes);
} catch (ServerException e) {
throw new RuntimeException(e);
} catch (InsufficientDataException e) {
throw new RuntimeException(e);
} catch (ErrorResponseException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (InvalidResponseException e) {
throw new RuntimeException(e);
} catch (XmlParserException e) {
throw new RuntimeException(e);
} catch (InternalException e) {
throw new RuntimeException(e);
}
}
/**
* 压缩base64格式的图片
* @param base64Image 包含前缀的base64图片字符串 (如: "data:image/png;base64,...")
* @param targetWidth 目标宽度
* @param targetHeight 目标高度
* @return 压缩后的base64图片字符串保持原有前缀格式
*/
public String compressBase64Image(String base64Image, int targetWidth, int targetHeight) {
if (base64Image == null || base64Image.isEmpty()) {
return base64Image;
}
try {
// 解析base64字符串
String[] parts = base64Image.split(",");
if (parts.length != 2) {
log.warn("无效的base64图片格式: {}", base64Image.substring(0, Math.min(50, base64Image.length())));
return base64Image;
}
String prefix = parts[0] + ","; // 保留前缀,如 "data:image/png;base64,"
String base64Data = parts[1];
// 解码base64数据
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
// 读取图片
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageBytes));
if (originalImage == null) {
log.warn("无法读取base64图片数据");
return base64Image;
}
// 计算压缩比例,保持宽高比
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
double scaleX = (double) targetWidth / originalWidth;
double scaleY = (double) targetHeight / originalHeight;
double scale = Math.min(scaleX, scaleY); // 选择较小的缩放比例以保持宽高比
int newWidth = (int) (originalWidth * scale);
int newHeight = (int) (originalHeight * scale);
// 如果图片已经比目标尺寸小,则不进行压缩
if (scale >= 1.0) {
log.info("图片尺寸 {}x{} 已小于目标尺寸 {}x{},无需压缩", originalWidth, originalHeight, targetWidth, targetHeight);
return base64Image;
}
// 创建压缩后的图片
BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = compressedImage.createGraphics();
// 设置高质量渲染
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 绘制压缩后的图片
g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g2d.dispose();
// 转换为Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(compressedImage, "PNG", baos); // 保持PNG格式以支持透明度
byte[] compressedBytes = baos.toByteArray();
String compressedBase64 = Base64.getEncoder().encodeToString(compressedBytes);
log.info("Base64图片压缩完成: {}x{} -> {}x{} (压缩比: {:.2f})",
originalWidth, originalHeight, newWidth, newHeight, scale);
return prefix + compressedBase64;
} catch (Exception e) {
log.error("压缩base64图片失败", e);
return base64Image; // 出错时返回原图
}
}
public void uploadToMinio(byte[] data, String bucket, String objectName, String contentType) /*throws Exception*/ {
try {
minioClient.putObject(PutObjectArgs.builder()