com.zcj.util.UtilImage Maven / Gradle / Ivy
package com.zcj.util;
import com.zcj.util.filenameutils.FilenameUtils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* 图片压缩/加水印工具
*
* 依赖第三方库:'net.coobird:thumbnailator:0.4.8'
*
* @author [email protected]
* @since 2019/6/13
*/
public class UtilImage {
public static BufferedImage initWaterImage(String waterText, Integer waterWidth) {
char[] data = waterText.toCharArray();
// 得到图片缓冲区
BufferedImage bi = new BufferedImage(waterWidth, 50, BufferedImage.TYPE_INT_RGB);
// 得到画笔
Graphics2D g = bi.createGraphics();
// 设置绘图区域透明
bi = g.getDeviceConfiguration().createCompatibleImage(waterWidth, 50, Transparency.TRANSLUCENT);
g.dispose();
// 字体、字体大小,透明度,旋转角度
g = bi.createGraphics();
g.setFont(new Font("微软雅黑", Font.PLAIN, 28));
g.setColor(Color.RED);
// 设置文本显示坐标
g.drawChars(data, 0, data.length, 10, 30);
return bi;
}
public static class ImageBuilder {
private static final Logger logger = LoggerFactory.getLogger(ImageBuilder.class);
private String srcPath;
private String waterText;
private String waterPath;
private Integer waterMinWidth;// 图片宽度达到指定值才添加水印
private Integer waterMinHeight;// 图片高度达到指定值才添加水印
private String outPath;
private String outFormat;
private Integer width;// 设置目标宽度
private Integer height;// 设置目标高度
private Integer maxWidth;// 设置目标最大宽度
public boolean go() {
if (UtilString.isBlank(srcPath)) {
return false;
}
try {
Builder> builder = Thumbnails.of(srcPath);
// 原始图片的宽和高(有必要时才获取)
Integer[] srcSize = new Integer[]{null, null};
if (maxWidth != null || (
(UtilString.isNotBlank(waterPath) || UtilString.isNotBlank(waterText))
&& (waterMinWidth != null || waterMinHeight != null)
)) {
srcSize = UtilImageBase.getWidthAndHeight(srcPath);
}
// 根据最大宽度设置目标宽度
if (maxWidth != null) {
Integer srcWidth = srcSize[0];
if (srcWidth == null || srcWidth <= 0 || srcWidth > maxWidth) {
width = maxWidth;
}
}
// 根据水印条件设置水印图片
if ((UtilString.isNotBlank(waterPath) || UtilString.isNotBlank(waterText))
&& (waterMinWidth != null || waterMinHeight != null)) {
if (width != null && waterMinWidth != null && width < waterMinWidth) {// 目标宽度达不到水印要求
waterPath = null;
waterText = null;
} else if (width == null && srcSize[0] != null && waterMinWidth != null && srcSize[0] < waterMinWidth) {// 原始宽度达不到水印要求
waterPath = null;
waterText = null;
} else if (height != null && waterMinHeight != null && height < waterMinHeight) {// 目标高度打不到水印要求
waterPath = null;
waterText = null;
} else if (height == null && srcSize[1] != null && waterMinHeight != null && srcSize[1] < waterMinHeight) {// 原始高度达不到水印要求
waterPath = null;
waterText = null;
}
}
if (width == null && height == null) {
builder.scale(1);
} else if (width == null) {
builder.height(height);
} else if (height == null) {
builder.width(width);
} else {
builder.size(width, height);
}
if (UtilString.isNotBlank(outFormat)) {
builder.outputFormat(outFormat);
}
if (UtilString.isNotBlank(waterPath)) {
builder.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(waterPath)), 1f);
} else if (UtilString.isNotBlank(waterText)) {
Integer waterImageWidth = UtilString.firstUnNull(width, srcSize[0], 300);
BufferedImage bi = UtilImage.initWaterImage(waterText, waterImageWidth);
builder.watermark(Positions.BOTTOM_LEFT, bi, 1f);
}
if (UtilString.isBlank(outPath)) {
builder.toFiles(Rename.NO_CHANGE);
} else {
File newFile = new File(FilenameUtils.getFullPath(outPath));
if (!newFile.exists()) {
newFile.mkdirs();
}
builder.toFile(outPath);
}
return true;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
}
}
/**
* 设置源图片地址
*
* @param srcPath 例:E://1.png
* @return {@link ImageBuilder}
*/
public ImageBuilder srcPath(String srcPath) {
this.srcPath = srcPath;
return this;
}
/**
* 设置文本水印
*
* @param waterText 文本内容
* @return {@link ImageBuilder}
*/
public ImageBuilder waterText(String waterText) {
this.waterText = waterText;
return this;
}
/**
* 设置水印地址
*
* @param waterPath 例:E://1.png
* @return {@link ImageBuilder}
*/
public ImageBuilder waterPath(String waterPath) {
this.waterPath = waterPath;
return this;
}
/**
* 图片宽度达到指定值才添加水印
*
* @param waterMinWidth 指定宽度
* @return {@link ImageBuilder}
*/
public ImageBuilder waterMinWidth(Integer waterMinWidth) {
this.waterMinWidth = waterMinWidth;
return this;
}
/**
* 图片高度达到指定值才添加水印
*
* @param waterMinHeight 指定高度
* @return {@link ImageBuilder}
*/
public ImageBuilder waterMinHeight(Integer waterMinHeight) {
this.waterMinHeight = waterMinHeight;
return this;
}
/**
* 设置处理后保存的路径
*
* @param outPath 例:E://2.png;NULL 表示覆盖源文件
* @return {@link ImageBuilder}
*/
public ImageBuilder outPath(String outPath) {
this.outPath = outPath;
return this;
}
/**
* 设置压缩格式
*
* @param outFormat NULL 表示按原格式压缩;例:jpg
* @return {@link ImageBuilder}
*/
public ImageBuilder outFormat(String outFormat) {
this.outFormat = outFormat;
return this;
}
/**
* 设置目标宽度
*
* @param width NULL 表示按目标高度等比缩放
* @return {@link ImageBuilder}
*/
public ImageBuilder width(Integer width) {
this.width = width;
return this;
}
/**
* 设置目标高度
*
* @param height NULL 表示按目标宽度等比缩放
* @return {@link ImageBuilder}
*/
public ImageBuilder height(Integer height) {
this.height = height;
return this;
}
/**
* 设置目标最大宽度
*
* @param maxWidth 最大宽度
* @return {@link ImageBuilder}
*/
public ImageBuilder maxWidth(Integer maxWidth) {
this.maxWidth = maxWidth;
return this;
}
}
/**
* 缩放图片
* 依赖JAR包:thumbnailator-0.4.8.jar
* 已废弃,用new ImageBuilder().xxx().go()替代
*
* @param filePath 源图片地址,例:E://1.png
* @param width 目标宽度;NULL 表示按目标高度等比缩放
* @param height 目标高度;NULL 表示按目标宽度等比缩放
* @param newFilePath 缩放后保存的路径,例:E://2.png;NULL 表示覆盖源文件
* @return 是否成功
*/
@Deprecated
public static boolean resize(String filePath, Integer width, Integer height, String newFilePath) {
return new ImageBuilder().srcPath(filePath).width(width).height(height).outPath(newFilePath).go();
}
/**
* 缩放图片
* 依赖JAR包:thumbnailator-0.4.8.jar
* 已废弃,用new ImageBuilder().xxx().go()替代
*
* @param filePath 源图片地址,例:E://1.png
* @param width 目标宽度;NULL 表示按目标高度等比缩放
* @param height 目标高度;NULL 表示按目标宽度等比缩放
* @param newFilePath 缩放后保存的路径,例:E://2.png;NULL 表示覆盖源文件
* @param outputFormat 压缩格式;NULL 表示按原格式压缩;例:jpg
* @return 是否成功
*/
@Deprecated
public static boolean resize(String filePath, Integer width, Integer height, String newFilePath, String outputFormat) {
return new ImageBuilder().srcPath(filePath).width(width).height(height).outPath(newFilePath)
.outFormat(outputFormat).go();
}
}