Merge remote-tracking branch 'origin/dev/dev' into dev/dev

This commit is contained in:
shahaibo
2025-06-26 11:53:53 +08:00
6 changed files with 99 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package com.ai.da.common.utils;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.constant.CommonConstant;
import com.ai.da.mapper.primary.entity.ObjectItem;
import io.minio.*;
import io.minio.errors.*;
@@ -16,11 +17,14 @@ import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -675,6 +679,70 @@ public class MinioUtil {
}
}
public String changeToWhiteBackground(String minioPath){
try {
// 1. 使用URL读取远程图片
BufferedImage originalImage = ImageIO.read(new URL(getPreSignedUrl(minioPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)));
// 2. 透明图检查(新增核心逻辑)
if (!hasTransparency(originalImage)) {
log.info("图片 {} 无透明通道,无需添加白底", minioPath);
return null; // 返回空
}
// 3. 处理图片(例如:转换为白底)
BufferedImage newImage = new BufferedImage(
originalImage.getWidth(),
originalImage.getHeight(),
BufferedImage.TYPE_INT_RGB
);
// 4. 填充白色背景
java.awt.Graphics2D g = newImage.createGraphics();
g.setColor(java.awt.Color.WHITE);
g.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
g.drawImage(originalImage, 0, 0, null);
g.dispose();
// 5. 输出为Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newImage, "PNG", baos);
String base64Image = java.util.Base64.getEncoder().encodeToString(baos.toByteArray());
log.info("为图片 {} 添加白色背景", minioPath);
// System.out.println("data:image/png;base64," + base64Image);
return "data:image/png;base64," + base64Image;
// return base64Image;
} catch (Exception e) {
log.error(e.getMessage());
throw new BusinessException("透明图添加白色背景失败");
}
}
/**
* 检测图片是否包含透明通道
*/
private boolean hasTransparency(BufferedImage image) {
// 情况1图像本身支持透明如TYPE_INT_ARGB
if (image.getTransparency() == Transparency.TRANSLUCENT) {
return true;
}
// 情况2检查像素级透明度适用于TYPE_INT_RGB等格式
if (image.getColorModel().hasAlpha()) {
// 抽样检查前100x100像素避免全图扫描的性能问题
int width = Math.min(image.getWidth(), 100);
int height = Math.min(image.getHeight(), 100);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if ((image.getRGB(x, y) >> 24) == 0x00) {
return true; // 发现透明像素
}
}
}
}
return false;
}
}

View File

@@ -280,11 +280,7 @@ public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collect
}
d.setRgbValue(o.getColorRgb());
// 渐变色
Gradient gradient = JSONObject.parseObject(o.getGradientString(), Gradient.class);
if (!Objects.isNull(gradient) && !StringUtil.isNullOrEmpty(gradient.getColorImg())){
gradient.setColorImg(null);
}
d.setGradient(gradient);
d.setGradient(JSONObject.parseObject(o.getGradientString(), Gradient.class));
});
}
}

View File

@@ -2790,6 +2790,7 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
vo.setDesignOutfitId(tDesignPythonOutfit.getId());
vo.setDesignOutfitUrl(minioUtil.getPreSignedUrl(tDesignPythonOutfit.getDesignUrl(), 24 * 60));
vo.setDesignItemId(tDesignPythonOutfit.getDesignItemId());
vo.setResultType(CollectionType.DESIGN.getValue());
voList.add(vo);
}
result.setDesign(voList);

View File

@@ -65,6 +65,7 @@ import java.util.regex.Pattern;
import static com.ai.da.common.enums.CollectionLevel1TypeEnum.*;
import static com.ai.da.common.enums.CreditsEventsEnum.TO_PRODUCT_IMAGE;
import static com.ai.da.common.enums.CreditsEventsEnum.TO_PRODUCT_IMAGE_FLUX;
@Slf4j
@Service
@@ -1366,7 +1367,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
*/
@Transactional(rollbackFor = Exception.class)
public GenerateResultVO sketchReconstructionGenerate(SketchReconstructionDTO sketchReconstructionDTO){
log.info("sketchReconstructionGenerate params: {}", sketchReconstructionDTO);
// log.info("sketchReconstructionGenerate params: {}", sketchReconstructionDTO);
Long accountId = UserContext.getUserHolder().getId();
// 1、线稿生成
@@ -2124,7 +2125,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
if (!StringUtil.isNullOrEmpty(imagePath)){
try {
String imageAsBase64 = minioUtil.getImageAsBase64(imagePath);
String imageAsBase64 = null;
if (func.equals(TO_PRODUCT_IMAGE_FLUX)){
imageAsBase64 = addWhiteBackground(imagePath);
}
if (StringUtil.isNullOrEmpty(imageAsBase64)){
imageAsBase64 = minioUtil.getImageAsBase64(imagePath);
}
requestBody.set("input_image", imageAsBase64);
} catch (IOException e) {
log.error("获取图片的base64格式失败{}", String.valueOf(e));
@@ -2225,5 +2232,16 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
}
private String addWhiteBackground(String minioPath){
// 1、先通过后缀判断输入图片类型有没有透明通道
String extension = minioPath.substring(minioPath.lastIndexOf(".") + 1);
// 2、如果有为其添加白色背景
if (extension.equals("png")){
return minioUtil.changeToWhiteBackground(minioPath);
}else {
log.info("图片 {} 没有透明通道, 不用添加白底", minioPath);
return null;
}
}
}

View File

@@ -497,7 +497,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
ToProductImageResult toProductImageResult = new ToProductImageResult();
if (fluxTask){
taskId = generateService.flux(CreditsEventsEnum.TO_PRODUCT_IMAGE, sb.toString(), toProductElement.getUrl());
taskId = generateService.flux(creditsEventsEnum, sb.toString(), toProductElement.getUrl());
toProductImageResult.setModelName("flux");
toProductImageResult.setResultType("ToProductImage");
} else {
@@ -1045,7 +1045,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
ToProductImageResult toProductImageResult = new ToProductImageResult();
if (fluxTask){
taskId = generateService.flux(CreditsEventsEnum.RELIGHT_FLUX, s, toProductImageResult1.getUrl());
taskId = generateService.flux(creditsEventsEnum, s, toProductImageResult1.getUrl());
toProductImageResult.setModelName("flux");
toProductImageResult.setResultType("Relight");
} else {
@@ -1074,7 +1074,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
ToProductElement toProductElement = toProductElementMapper.selectById(toProductImageVO.getElementId());
ToProductImageResult toProductImageResult = new ToProductImageResult();
if (fluxTask){
taskId = generateService.flux(CreditsEventsEnum.RELIGHT, s, toProductElement.getUrl());
taskId = generateService.flux(CreditsEventsEnum.RELIGHT_FLUX, s, toProductElement.getUrl());
toProductImageResult.setModelName("flux");
toProductImageResult.setResultType("Relight");
} else {
@@ -2128,6 +2128,10 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
collectionElement.setHasPin((byte) 0);
collectionElement.setColorRgb(board.getRgbValue());
collectionElement.setMd5("0");
// 渐变色
if (!Objects.isNull(board.getGradient()) && !StringUtil.isNullOrEmpty(board.getGradient().getColorImg())){
board.getGradient().setColorImg(null);
}
collectionElement.setGradientString(JSON.toJSONString(board.getGradient()));
collectionElement.setCreateDate(new Date());
collectionElementMapper.insert(collectionElement);