package com.img.cut.utils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import org.apache.commons.lang3.ObjectUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* 图片裁剪工具
*
* @author leemeea
* @date 2021/12/25
*/
public class ImageCroppingUtil {
public static void main(String[] args) {
cutImgByMaxWidth("E:\\data\\test\\123.jpg", null,"", 280);
}
/**
* 将图片按传入的最大宽度裁剪
* <p>targetName与suffixAdded二选一</p>
* @param sourceFilePath 文件名
* @param targetName 目标文件名(如果保留原始文件名的话,这里传null)
* @param suffixAdded 在目标文件名之后,且在后缀名之前添加的字符,如果不想添加这个字符,则不传
* @param maxWidth 图片最大宽度
* @return 切图之后的路径
*/
public static String cutImgByMaxWidth(String sourceFilePath, String targetName , String suffixAdded, int maxWidth){
if (ObjectUtils.isEmpty(targetName) && ObjectUtils.isEmpty(suffixAdded)) {
System.out.println("目标文件名与目标文件添加字符必传一个:" + sourceFilePath);
return null;
}
return cutImgByMaxWidth(sourceFilePath, targetName, suffixAdded, maxWidth, false);
}
/**
* 将原图切到以maxWidth的宽度进行保存(替换原图)
* @param sourceFilePath 原图
* @param maxWidth 图片最大宽度
* @return 图片地址
*/
public static String replaceImgByMaxWidth(String sourceFilePath, int maxWidth) {
return cutImgByMaxWidth(sourceFilePath, null, null, maxWidth, true);
}
/**
* 将图片按传入的最大宽度裁剪
* @param sourceFilePath 文件名
* @param targetName 目标文件名(如果保留原始文件名的话,这里传null)
* @param suffixAdded 在目标文件名之后,且在后缀名之前添加的字符,如果不想添加这个字符,则不传
* @param maxWidth 图片最大宽度
* @param replaceSource 是否替换原图
* @return 切图之后的路径
*/
private static String cutImgByMaxWidth(String sourceFilePath, String targetName , String suffixAdded, int maxWidth, boolean replaceSource) {
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
System.out.println("源文件不存在:" + sourceFilePath);
return null;
}
if (!sourceFile.getName().contains(".")) {
System.out.println("源文件不存在后缀名:" + sourceFilePath);
return null;
}
// 原始文件名
String sourceFileName = sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf("."));
// 后缀名
String suffix = sourceFile.getName().substring(sourceFile.getName().lastIndexOf(".") + 1);
// 目标文件
File targetFile;
if (ObjectUtils.isNotEmpty(targetName)) {
targetFile = new File(sourceFile.getParent(), targetName + suffixAdded + "." + suffix);
} else {
targetFile = new File(sourceFile.getParent(), sourceFileName + suffixAdded + "." + suffix);
}
// 不替换原图的时候
if (!replaceSource) {
if (targetFile.exists()) {
System.out.println("目标文件已存在:" + targetFile.getAbsolutePath());
return null;
}
}
try {
BufferedImage image = ImageIO.read(sourceFile);
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// 使用宽度为界限,计算宽度与高度
if (imageWidth > maxWidth) {
double b = (double) imageHeight / (double) imageWidth;
imageHeight = (int) (b * maxWidth);
imageWidth = maxWidth;
}
Builder<BufferedImage> builder = Thumbnails.of(image).size(imageWidth, imageHeight);
builder.outputFormat(suffix).toFile(targetFile);
return targetFile.getPath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
图片裁剪工具-将图片按指定宽度缩小剪切
最新推荐文章于 2022-10-27 11:56:10 发布