TASK: 使用Flux生成前,to Product Image,为输入透明图添加白色背景
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -2119,7 +2120,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));
|
||||
@@ -2220,5 +2227,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user