mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
指定压缩后图片的背景色
This commit is contained in:
parent
cb368e2b6e
commit
6c32295a37
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user