指定压缩后图片的背景色

This commit is contained in:
hiqiuyi 2022-08-04 14:49:03 +08:00
parent cb368e2b6e
commit 6c32295a37
3 changed files with 82 additions and 3 deletions

View File

@ -60,6 +60,10 @@ public class Img implements Serializable {
* 图片输出质量用于压缩
*/
private float quality = -1;
/**
* 图片背景色
*/
private Color backgroundColor;
/**
* 从Path读取图片并开始处理
@ -216,6 +220,17 @@ public class Img implements Serializable {
return this;
}
/**
* 设置图片的背景色
*
* @param backgroundColor{@link Color} 背景色
* @return this
*/
public Img setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
/**
* 缩放图像按比例缩放
*
@ -709,7 +724,7 @@ public class Img implements Serializable {
final Image targetImage = (null == this.targetImage) ? this.srcImage : this.targetImage;
Assert.notNull(targetImage, "Target image is null !");
return ImgUtil.write(targetImage, this.targetImageType, targetImageStream, this.quality);
return ImgUtil.write(targetImage, this.targetImageType, targetImageStream, this.quality, this.backgroundColor);
}
/**

View File

@ -1166,10 +1166,25 @@ public class ImgUtil {
* @since 4.3.2
*/
public static BufferedImage toBufferedImage(Image image, String imageType) {
return toBufferedImage(image, imageType, null);
}
/**
* {@link Image} {@link BufferedImage}<br>
* 如果源图片的RGB模式与目标模式一致则直接转换否则重新绘制<br>
* 默认的png图片使用 {@link BufferedImage#TYPE_INT_ARGB}模式其它使用 {@link BufferedImage#TYPE_INT_RGB} 模式
*
* @param image {@link Image}
* @param imageType 目标图片类型例如jpg或png等
* @param backgroundColor 背景色{@link Color}
* @return {@link BufferedImage}
* @since 4.3.2
*/
public static BufferedImage toBufferedImage(Image image, String imageType, Color backgroundColor) {
final int type = IMAGE_TYPE_PNG.equalsIgnoreCase(imageType)
? BufferedImage.TYPE_INT_ARGB
: BufferedImage.TYPE_INT_RGB;
return toBufferedImage(image, type);
return toBufferedImage(image, type, backgroundColor);
}
/**
@ -1195,6 +1210,30 @@ public class ImgUtil {
return bufferedImage;
}
/**
* {@link Image} {@link BufferedImage}<br>
* 如果源图片的RGB模式与目标模式一致则直接转换否则重新绘制
*
* @param image {@link Image}
* @param imageType 目标图片类型{@link BufferedImage}中的常量例如黑白等
* @param backgroundColor 背景色{@link Color}
* @return {@link BufferedImage}
* @since 5.4.7
*/
public static BufferedImage toBufferedImage(Image image, int imageType, Color backgroundColor) {
BufferedImage bufferedImage;
if (image instanceof BufferedImage) {
bufferedImage = (BufferedImage) image;
if (imageType != bufferedImage.getType()) {
bufferedImage = copyImage(image, imageType, backgroundColor);
}
return bufferedImage;
}
bufferedImage = copyImage(image, imageType, backgroundColor);
return bufferedImage;
}
/**
* 将已有Image复制新的一份出来
*
@ -1562,11 +1601,27 @@ public class ImgUtil {
* @since 4.3.2
*/
public static boolean write(Image image, String imageType, ImageOutputStream destImageStream, float quality) throws IORuntimeException {
return write(image, imageType, destImageStream, quality, null);
}
/**
* 写出图像为指定格式
*
* @param image {@link Image}
* @param imageType 图片类型图片扩展名
* @param destImageStream 写出到的目标流
* @param quality 质量数字为0~1不包括0和1表示质量压缩比除此数字外设置表示不压缩
* @param backgroundColor 背景色{@link Color}
* @return 是否成功写出如果返回false表示未找到合适的Writer
* @throws IORuntimeException IO异常
* @since 4.3.2
*/
public static boolean write(Image image, String imageType, ImageOutputStream destImageStream, float quality, Color backgroundColor) throws IORuntimeException {
if (StrUtil.isBlank(imageType)) {
imageType = IMAGE_TYPE_JPG;
}
final BufferedImage bufferedImage = toBufferedImage(image, imageType);
final BufferedImage bufferedImage = toBufferedImage(image, imageType, backgroundColor);
final ImageWriter writer = getWriter(bufferedImage, imageType);
return write(bufferedImage, writer, destImageStream, quality);
}

View File

@ -26,6 +26,15 @@ public class ImgTest {
Img.from(FileUtil.file("f:/test/4347273249269e3fb272341acc42d4e.jpg")).setQuality(0.8).write(FileUtil.file("f:/test/test_dest.jpg"));
}
@Test
@Ignore
public void compressWithBackgroundColorTest() {
Img.from(FileUtil.file("D:/test/before_compress.png"))
.setBackgroundColor(Color.WHITE)
.setQuality(0.8)
.write(FileUtil.file("D:/test/after_compress.jpg"));
}
@Test
@Ignore
public void writeTest() {