JAVA 处理目录下及子目录下图片压缩
压缩需要用到其他jar包
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
处理目录下及子目录下图片压缩
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageProcessor3 {
public static void main(String[] args) {
// 图片存放目录地址
String imgPath = "/test/inputImg";
// 输出目录
String outPath = "/test/outImg";
// 水印
String watermarkText = "水印内容";
File directory = new File(imgPath);
processFilesInDirectory(directory, outPath, imgPath, watermarkText);
}
private static void processFilesInDirectory(File directory, String outPath, String imgPath, String watermarkText) {
if (directory.isDirectory()) {
File[] files = directory.listFiles();
if (files != null) {
for (int i=0; i<files.length;i++) {
File file = files[i];
if (file.isDirectory()) {
// 递归处理子目录
processFilesInDirectory(file, outPath, imgPath, watermarkText);
} else if (isImageFile(file)) {
try {
// 压缩图片
String replaceOutPath = file.getPath().replace(imgPath, outPath);
replaceOutPath = replaceOutPath.substring(0,replaceOutPath.length()-file.getName().length());
compressImage(file, new File(replaceOutPath));
// 添加水印
String imagePath = addWatermark(new File(file.getPath().replace(imgPath, outPath)), new File(replaceOutPath), watermarkText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
private static boolean isImageFile(File file) {
String name = file.getName().toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png");
}
/**
* 添加水印
* @param imageFile
* @param outputDirectory
* @throws IOException
*/
private static String addWatermark(File imageFile, File outputDirectory, String watermarkText) throws IOException {
BufferedImage originalImage = ImageIO.read(imageFile);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
BufferedImage watermarkedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = watermarkedImage.createGraphics();
g2d.drawImage(originalImage, 0, 0, null);
// Add watermark text
Font font = new Font("Arial", Font.BOLD, 60);
g2d.setFont(font);
FontMetrics fontMetrics = g2d.getFontMetrics();
int textWidth = fontMetrics.stringWidth(watermarkText);
int textHeight = fontMetrics.getHeight();
// Calculate the position for the watermark
int x = (width - textWidth) / 2;
int y = (height + textHeight) / 2;
// Rotate the text
g2d.rotate(Math.toRadians(-45), x + textWidth / 2, y - textHeight / 2);
// 设置颜色为白色,透明度 100
g2d.setColor(new Color(255, 255, 255, 100));
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Draw the text
g2d.drawString(watermarkText, x, y);
g2d.dispose();
String path = outputDirectory.getPath() + File.separator + imageFile.getName();
File outputImageFile = new File(path);
ImageIO.write(watermarkedImage, "jpg", outputImageFile);
return path;
}
/**
* 图片压缩
* @param imageFile
* @param outputDirectory
* @throws IOException
*/
private static void compressImage(File imageFile, File outputDirectory) throws IOException {
if (!outputDirectory.isDirectory()) {
outputDirectory.mkdir();
}
File compressedImageFile = new File(outputDirectory.getPath() + File.separator + imageFile.getName());
OutputStream os = new FileOutputStream(compressedImageFile);
Thumbnails.of(imageFile)
.size(800, 600)
.outputQuality(0.8)
.toOutputStream(os);
os.close();
}
}
标签:JAVA,String,下及,imgPath,File,directory,import,目录,图片
From: https://blog.csdn.net/Ls66666Ls/article/details/137558961