如何裁剪一个文件夹中的所有图像?



我有一个项目,我需要裁剪文件夹中的所有图像。到目前为止,它可以裁剪并擦除周围的额外空白,但是,我需要将确切的文件名放在文件路径上。我已经附上了下面的代码,向您展示我的代码是做什么的。所以简而言之,我需要帮助,使我的代码裁切整个文件夹的图像,而不是只有一个。然后自动将裁剪后的图像保存在另一个文件夹中。谢谢你。

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class code {
private BufferedImage img;
public static void main(String[] args) throws IOException {
code trim = new code(new File("D:\eclipse-java-workspace\image_manipulation\sample_input_images\DP936BA.jpg"));
System.out.println("Starting first image..");
trim.trim();
trim.write(new File("D:\eclipse-java-workspace\image_manipulation\sample_out_images\output5.jpg"));

}

public code(File input) {
try {
img = ImageIO.read(input);
} catch (IOException e) {
throw new RuntimeException( "Problem reading image", e );
}
}
public void trim()
{
System.out.println(getTrimmedHeight2());
BufferedImage img2 = img.getSubimage(getTrimmedWidth2(), getTrimmedHeight2(), getTrimmedWidth()-getTrimmedWidth2(), getTrimmedHeight()-getTrimmedHeight2());
BufferedImage copyOfImage = new BufferedImage(img2.getWidth(), img2.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(img2, 0, 0, null);
img = copyOfImage;
}
private int getTrimmedWidth2() {
int height = this.img.getHeight();
int width  = this.img.getWidth();
int lowest = 99999;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width-1; j++) {
if(img.getRGB(j, i) != Color.WHITE.getRGB() ) {

if(j<lowest)
{
lowest = j;
}
break;
}
}
}
return lowest;
}
public void write(File f) {
try {
ImageIO.write(img, "jpg", f);
} catch (IOException e) {
throw new RuntimeException( "Problem writing image", e );
}
}
private int getTrimmedWidth() {
int height = this.img.getHeight();
int width  = this.img.getWidth();
int trimmedWidth = 0;
for(int i = 0; i < height; i++) {
for(int j = width - 1; j >= 0; j--) {
if(img.getRGB(j, i) != Color.WHITE.getRGB() &&
j > trimmedWidth) {
trimmedWidth = j;
break;
}
}
}
return trimmedWidth ;
}
private int getTrimmedHeight() {
int width = this.img.getWidth();
int height = this.img.getHeight();
int trimmedHeight = 0;
for(int i = 0; i < width; i++) {
for(int j = height - 1; j >= 0; j--) {
if(img.getRGB(i, j) != Color.WHITE.getRGB() &&
j > trimmedHeight) {
trimmedHeight = j;
break;
}
}
}
return trimmedHeight;
}
private int getTrimmedHeight2() {
int width = this.img.getWidth();
int height = this.img.getHeight();
int lowest = 99999;
for(int i = 0; i < width; i++) {
for(int j = 0; j < height-1; j++) {
if(img.getRGB(i, j) != Color.WHITE.getRGB() ) {
if(j<lowest)
{
System.out.println(j);
lowest = j;
}
break;
}
}
}
return lowest;
}

}

您可以通过使用文件夹路径来获取文件列表File[] fileList = directory.listFiles(),然后使用循环来遍历该列表并为每个文件创建一个代码对象并修剪它for (int i = 0; i < fileList.length; i++) {...}

下面是一个如何工作的例子。我添加了注释来解释这些步骤。注意,我还没有测试代码,但它应该可以工作:

//Path to your folder
final static String folderPath = "D:/your/directory/path/";
public static void main(String[] args) throws IOException {
//Get a list of all files in the folder
File[] fileList = new File(folderPath).listFiles();
System.out.println("Starting first image..");
//Loop through the above list of files
for (int i = 0; i < fileList.length; i++){
//For each file create a new code object and trim it
code trim = new code(fileList[i]);
trim.trim();
//Create a file path to save the trimmed file with the same name but add "_TRIMMED" to the end
//You will need to alter this line to put the file extension in the correct place
String newFileName = getPath() + "/" + fileList[i].getName() + "_TRIMMED"; 
//Finally save the updated file to the new path           
trim.write(new File(newFileName));
}    
System.out.println("All images trimmed..");
}

您还应该检查fileList中的文件不是文件夹,并且在您尝试修剪它们之前它们是图像,而不是仅仅抛出RuntimeException并停止整个应用程序。但我将把那部分留给你。