diff --git a/src/main/java/com/ai/da/common/utils/MinioUtil.java b/src/main/java/com/ai/da/common/utils/MinioUtil.java index 1dc16740..39927ff5 100644 --- a/src/main/java/com/ai/da/common/utils/MinioUtil.java +++ b/src/main/java/com/ai/da/common/utils/MinioUtil.java @@ -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; + } + } diff --git a/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java b/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java index af14e0c7..1969faa5 100644 --- a/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/CollectionServiceImpl.java @@ -280,11 +280,7 @@ public class CollectionServiceImpl extends ServiceImpl 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); diff --git a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java index 0564aac7..60caa5c8 100644 --- a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java @@ -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 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 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 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; + } + } } diff --git a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java index ecc19384..bc407d92 100644 --- a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java @@ -497,7 +497,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl