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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user